Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trafikklys Programmet er hentet fra Kapittel 1 Embedded C and the Atmel AVR 2. Ed Barnett, Cox and O’Cull Thomson / Delmar Learning.

Similar presentations


Presentation on theme: "Trafikklys Programmet er hentet fra Kapittel 1 Embedded C and the Atmel AVR 2. Ed Barnett, Cox and O’Cull Thomson / Delmar Learning."— Presentation transcript:

1 Trafikklys Programmet er hentet fra Kapittel 1 Embedded C and the Atmel AVR 2. Ed Barnett, Cox and O’Cull Thomson / Delmar Learning

2 Trafikklys #define  #include  #defineEW_RED_LITEPORTB.0/* definitions to actual outputs */  #defineEW_YEL_LITEPORTB.1/* used to control the lights */  #defineEW_GRN_LITEPORTB.2  #defineNS_RED_LITEPORTB.3  #defineNS_YEL_LITEPORTB.4  #defineNS_GRN_LITEPORTB.5  #definePED_XING_EWPINA.0 /* pedestrian crossing push button */  #definePED_XING_NSPINA.1 /* pedestrian crossing push button */  #defineFOUR_WAY_STOP PINA.3/* switch input for 4-Way Stop */

3 Trafikklys enum  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 };

4 Trafikklys main  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_ms(1000);// 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  }

5 Trafikklys EW_MOVING  void Do_States(void)  {  switch(current_state)  {  caseEW_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

6 Trafikklys EW-WARNING  caseEW_WARNING:  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;// and east-west has the yellow  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

7 Lyskryss NS_MOVING  caseNS_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_NS || FOUR_WAY_STOP)  {// if a 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

8 Lyskryss NS_WARNING  caseNS_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

9 Lyskryss FLASHER  caseFLASHER:  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!!  }

10 Lyskryss tilstander Tilstandene er: EW_MOVING øst-vest trafikk EW_WARNING ØV gult NS rødt NS_MOVING nord-syd trafikk NS_WARNING ØV Rødt; NS gult FLASHING Blinker avvekslende rødt i begge retninger

11 Lyskryss input data  PED_XING_EW fotgjenger ønsker å krysse EW gaten  -- her skal det reageres hvis trafikk går i EW retning  PED_XING_NS fotgjenger ønsker å krysse NS gaten  Her skal det reageres hvis trafikk går i NS retning  FOUR_WAY_STOP for rødblink

12 Lyskryss tider  Time_left =30 vanlig tid  Time_left telles ned  I main ventes det 1 sekund før tilstandsmaskinen kalles.  Hvis en fotgjenger kommer og tiden er mer enn 10 settes den ned til 10 Ved overgang til WARNING settes Time_left til 5


Download ppt "Trafikklys Programmet er hentet fra Kapittel 1 Embedded C and the Atmel AVR 2. Ed Barnett, Cox and O’Cull Thomson / Delmar Learning."

Similar presentations


Ads by Google