⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exp1.c

📁 Proteus 的学习资料,对AVR的开发新手有一定的帮助.
💻 C
字号:
#include <io8535.h>
#include <intrinsics.h>

#define EW_RED_LITE PORTB_Bit0      /*dedinitions to actual outputs*/
#define EW_YEL_LITE PORTB_Bit1      /*used to control the lights*/
#define EW_GRN_LITE PORTB_Bit2
#define NS_RED_LITE PORTB_Bit3
#define NS_YEL_LITE PORTB_Bit4
#define NS_GRN_LITE PORTB_Bit5

#define PED_XING_EW PINA_Bit0       /*pedestrian crossing push button*/
#define PED_XING_NS PINA_Bit1       /*used to control the lights*/
#define FOUR_WAY_STOP PINA_Bit3     /*switch input for 4-Way Stop*/

char  time_left;                    //time in seconds spent in each state
int   current_state;                //current state of the lights
char  flash_toggle;                 //toggle used for FLASHER state
                                    //this enumeration creates a simple way to add states to the machine
                                    //by name. Enumerations generate an integer value for each name
                                    //automatically, making the code easier to maintain.
enum  {EW_MOVING, EW_WARNING, NS_MOVING, NS_WARNING, FLASHER};
                                    //The actual state machine is here..
void  Do_States(void)
{
  switch(current_state)
  {
  case  EW_MOVING:                  //east-west has the green!!
        EW_GRN_LITE=1;
        NS_GRN_LITE=0;
        NS_RED_LITE=1;              //north-south has the red!!
        EW_RED_LITE=0;
        EW_YEL_LITE=0;
        NS_YEL_LITE=0;
  
        if(PED_XING_EW||FOUR_WAY_STOP)
        {                           //pedestrian wishes to cross, or a 4-Way stop is required
            if(time_left>10)
                time_left=10;       //shorten the time
        }
        if(time_left!=0)            //count down the time
        {
            --time_left;
            return;                 //return to main
        }                           //time expired, so..
        time_left=5;                //give 5 seconds to WARNING
        current_state=EW_WARNING;   //time expired, move
        break;                      //to the next state
  
  case  EW_WARNING:                  //east-west has the green!!
        EW_GRN_LITE=0;
        NS_GRN_LITE=0;
        NS_RED_LITE=1;              //north-south has the red!!
        EW_RED_LITE=0;
        EW_YEL_LITE=1;
        NS_YEL_LITE=0;
  
        if(time_left!=0)            //count down the time
        {
            --time_left;
            return;                 //return to main
        }                           //time expired, so..
        if(FOUR_WAY_STOP)           //if 4-way requested then start
            current_state=FLASHER;  //the flasher
        else                        //otherwise..
        {
            time_left=30;           //give 30 seconds to MOVING
            current_state=NS_MOVING;
        }                           //time expired, move
        break;                      //to the next state
  
  case  NS_MOVING:
        EW_GRN_LITE=0;
        NS_GRN_LITE=1;
        NS_RED_LITE=0;              //north-south has the green!!
        EW_RED_LITE=1;              //east-west has the red!!
        EW_YEL_LITE=0;
        NS_YEL_LITE=0;
  
        if(PED_XING_EW||FOUR_WAY_STOP)
        {                           //pedestrian wishes to cross, or a 4-Way stop is required
            if(time_left>10)
                time_left=10;       //shorten the time
        }
        if(time_left!=0)            //count down the time
        {
            --time_left;
            return;                 //return to main
        }                           //time expired, so..
        time_left=5;                //give 5 seconds to WARNING
        current_state=NS_WARNING;   //time expired, move
        break;                      //to the next state
  
  case  NS_WARNING:
        EW_GRN_LITE=0;
        NS_GRN_LITE=0;
        NS_RED_LITE=0;              //north-south has the yellow..
        EW_RED_LITE=1;
        EW_YEL_LITE=0;              //and east-west has the red..
        NS_YEL_LITE=1;
  
        if(time_left!=0)            //count down the time
        {
            --time_left;
            return;                 //return to main
        }                           //time expired, so..
        if(FOUR_WAY_STOP)           //if 4-way requested then start
            current_state=FLASHER;  //the flasher
        else                        //otherwise..
        {
            time_left=30;           //give 30 seconds to MOVING
            current_state=EW_MOVING;
        }                           //time expired, move
        break;                      //to the next state
  
  case  FLASHER:
        EW_GRN_LITE=0;              //all yellow and
        NS_GRN_LITE=0;              //green lites off
        EW_YEL_LITE=0;
        NS_YEL_LITE=0;
  
        flash_toggle^=1;            //toggle LSB..
        if(flash_toggle&1)
        {
            NS_RED_LITE=1;          //blink red lights
            EW_RED_LITE=0;
        }
        else
        {
            NS_RED_LITE=0;          //alternately
            EW_RED_LITE=1;
        }
        if(!FOUR_WAY_STOP)          //if no longer a 4-way stop
            current_state=EW_WARNING;
        break;                      //then return to normal
  default:
            current_state=NS_WARNING;
        break;                      //set any unknown state to a good one!!
  }
}

void  main(void)
{
      DDRB=0xFF;                    //portb all out
      DDRA=0x00;                    //porta all in

      current_state=NS_WARNING;     //initialize to a good starting
                                    //state (as safe as possible)
      while(1)
      {
          __delay_cycles(100000);     //1 second delay.., this time could
                                    //be used for other needed processes
          Do_States();              //call the state machine, it knows
                                    //where it is and what to do next
      }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -