Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. typedef enum {A,B,C,D, screen} states_t;
  4. states_t stato = A;
  5. volatile uint8_t portbhistory = 0xFF;
  6.  
  7. uint16_t c=0;
  8. uint16_t b=0;
  9. uint16_t d=0;
  10. uint16_t i=0;
  11. int main (void) {
  12. DDRD &= ~((1 << DDD4) | (1 << DDD5) | (1 << DDD6)|(1 << DDD2)|(1 << DDD3));
  13.  
  14. DDRD &= ~(1 << DDD2);
  15. DDRD &= ~(1 << DDD3);
  16. PORTD=0xff;
  17. DDRC=0xff;
  18. PCICR |= (1 << PCIE2);
  19. PCMSK2 |= (1 << PCINT18) | (1 << PCINT19) | (1 << PCINT20)| (1 << PCINT21)| (1 << PCINT22);
  20. TCCR1B |= (1 << WGM12); // Set the Timer Mode to CTC (Mode 4)
  21.  
  22. // Set the value that you want to count to // [ (clock_speed / Prescaler_value) * Desired_interval_in_Seconds ] - 1 // [ (16000000 / 1024) * 0.2 ] -1 = 3124
  23. OCR1A = 3124;
  24.  
  25. // Set interrupt on compare match
  26.  
  27. TCCR1B |= (1 << CS12) | (1 << CS10); // starts the timer, set prescaler to 1024
  28.  
  29. TIMSK1 |= (1 << OCIE1A);
  30. sei();
  31.  
  32. while (1) {
  33. switch (stato) {
  34. case A :
  35. c=0;
  36. if(i==5){ PORTC=1;}
  37. if(i==10){ PORTC=3;}
  38. while(i>=5 & i<10){if(c==1){stato=B;
  39. PORTC=0;}
  40. if(i==15){ PORTC=7;}
  41. if(i==20){ PORTC=0xf;}
  42. if(i==25){PORTC=0;}
  43. break;
  44.  
  45. case B: c=0;
  46. if(i==5){ PORTC=1;}
  47. if(i==10){ PORTC=3;}
  48. while(i>=15 & i<20){if(c==1){stato=C; PORTC=0;}
  49. if(i==15){ PORTC=7;}
  50. if(i==20){ PORTC=0xf;}
  51. if(i==25){PORTC=0;}
  52. break;
  53. case C : b=0; if(i==5){ PORTC=1;}
  54. if(i==10){ PORTC=3;}
  55. while(i>=10 & i<15){if(b==1){stato=D; PORTC=0;}
  56. if(i==15){ PORTC=7;}
  57. if(i==20){ PORTC=0xf;}
  58. if(i==25){PORTC=0;}
  59. break;
  60. case D : d=0; if(i==5){ PORTC=1;}
  61. if(i==10){ PORTC=3;}
  62. while(i>=20 & i<25){if(d==1){stato=screen; PORTC=0;}
  63. if(i==15){ PORTC=7;}
  64. if(i==20){ PORTC=0xf;}
  65. if(i==25){PORTC=0;}
  66. break;
  67. case screen : if(i==5){ PORTC=0xf;}
  68. if(i==10){ PORTC=0; i=0;}
  69.  
  70. break;
  71.  
  72.  
  73.  
  74.  
  75. }
  76. }
  77. }
  78.  
  79. ISR (TIMER1_COMPA_vect){i++;
  80. if(i==26){i=0;}}
  81. ISR (PCINT2_vect) { uint8_t changedbits;
  82.  
  83. changedbits = PIND ^ portbhistory; portbhistory = PIND;
  84.  
  85. if(changedbits & (1 << PIND2)) { /* PCINT0 changed */ if( (PIND & (1 << PIND2)) == 1 ) { /* LOW to HIGH pin change */ } else { d=1;} }
  86. if(changedbits & (1 << PIND3)) { /* PCINT0 changed */ if( (PIND & (1 << PIND3)) == 1 ) { /* LOW to HIGH pin change */ } else {c=1; } }
  87. if(changedbits & (1 << PIND5)) { /* PCINT0 changed */ if( (PIND & (1 << PIND5)) == 1 ) { /* LOW to HIGH pin change */ } else { b=1;} }
  88. if(changedbits & (1 << PIND4)) { /* PCINT0 changed */ if( (PIND & (1 << PIND4)) == 1 ) { /* LOW to HIGH pin change */ } else { /* HIGH to LOW pin change */ } }
  89.  
  90. if(changedbits & (1 << PIND6)) { /* PCINT0 changed */ if( (PIND & (1 << PIND6)) == 1 ) { /* LOW to HIGH pin change */ } else { /* HIGH to LOW pin change */ } }
  91.  
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement