Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4.  
  5. typedef enum {A,B,C,D, screen} states_t;
  6. states_t stato = A;
  7. volatile uint8_t portbhistory = 0xFF;
  8.  
  9. uint16_t tasto0 =0;
  10. uint16_t tasto1 =0;
  11. uint16_t tasto2 =0;
  12. uint16_t tasto3 =0;
  13. uint16_t arrivo[4]={9,9,9,9};
  14. uint16_t indice=0;
  15.  
  16. uint16_t i=0;
  17.  
  18. int main (void) {
  19.  
  20. DDRD &= ~((1 << DDD4) | (1 << DDD5) | (1 << DDD6)|(1 << DDD2)|(1 << DDD3));
  21.  
  22. DDRD &= ~(1 << DDD2);
  23. DDRD &= ~(1 << DDD3);
  24. PORTD=0xff;
  25. DDRC=0xff;
  26. PCICR |= (1 << PCIE2);
  27. PCMSK2 |= (1 << PCINT18) | (1 << PCINT19) | (1 << PCINT20)| (1 << PCINT21)| (1 << PCINT22) |(1<<PCINT23);
  28. TCCR1B |= (1 << WGM12); // Set the Timer Mode to CTC (Mode 4)
  29.  
  30. // Set the value that you want to count to // [ (clock_speed / Prescaler_value) * Desired_interval_in_Seconds ] - 1 // [ (16000000 / 1024) * 0.2 ] -1 = 19953
  31. OCR1A = 1953;
  32.  
  33. // Set interrupt on compare match
  34.  
  35. TCCR1B |= (1 << CS12) | (1 << CS10); // starts the timer, set prescaler to 1024
  36.  
  37. TIMSK1 |= (1 << OCIE1A);
  38. sei();
  39.  
  40.  
  41. while (1) {
  42. switch (stato) {
  43.  
  44. case A : PORTC=0;
  45. tasto0=0;
  46. tasto1=0;
  47. tasto2=0;
  48. tasto3=0;
  49. arrivo[0]=9;
  50. arrivo[1]=9;
  51. arrivo[2]=9;
  52. arrivo[3]=9;
  53. indice=0;
  54.  
  55. break;
  56.  
  57. case B:
  58. PORTC|=(1<<PORTC4);
  59. if(tasto0==1){ PORTC|=(1<<PORTC0); if (arrivo[0]!=0 & arrivo[1]!=0 & arrivo[2]!=0 & arrivo[3]!=0)
  60. {arrivo[indice]=0; indice++;
  61. }
  62. }
  63. if(tasto1==1){ PORTC|=(1<<PORTC1); if (arrivo[0]!=1 & arrivo[1]!=1 & arrivo[2]!=1 & arrivo[3]!=1)
  64. {arrivo[indice]=1; indice++;
  65. }}
  66. if(tasto2==1){ PORTC|=(1<<PORTC2); if (arrivo[0]!=2 & arrivo[1]!=2 & arrivo[2]!=2 & arrivo[3]!=2)
  67. {arrivo[indice]=2; indice++;
  68. }}
  69. if(tasto3==1){ PORTC|=(1<<PORTC3); if (arrivo[0]!=3 & arrivo[1]!=3 & arrivo[2]!=3 & arrivo[3]!=3)
  70. {arrivo[indice]=3; indice++;
  71. } }
  72. if (tasto0==1 & tasto1==1 & tasto2==1 & tasto3==1)
  73. {
  74. stato=C;
  75. }
  76. if(i==80){ stato=A;}
  77. break;
  78.  
  79. case C: PORTC=0Xf;
  80. i=0;
  81. while(i!=1){}
  82. PORTC &= ~(1<<arrivo[0]);
  83. i=0;
  84. while(i!=1){}
  85. PORTC |=(1<<arrivo[0]);
  86. PORTC &= ~(1<<arrivo[1]);
  87. i=0;
  88. while(i!=1){}
  89. PORTC &= ~(1<<arrivo[0]);
  90. PORTC &= ~(1<<arrivo[1]);
  91.  
  92. i=0;
  93. while(i!=1){}
  94. PORTC |=(1<<arrivo[0]);
  95. PORTC |=(1<<arrivo[1]);
  96. PORTC &= ~(1<<arrivo[2]);
  97. i=0;
  98. while(i!=1){}
  99. PORTC &= ~(1<<arrivo[0]);
  100. i=0;
  101. while(i!=1){}
  102. PORTC |=(1<<arrivo[0]);
  103. PORTC &= ~(1<<arrivo[1]);
  104. i=0;
  105. while(i!=1){}
  106. PORTC &= ~(1<<arrivo[0]);
  107. i=0;
  108. while(i!=1){}
  109. PORTC |=(1<<arrivo[0]);
  110. PORTC |=(1<<arrivo[1]);
  111. PORTC |=(1<<arrivo[2]);
  112. PORTC &= ~(1<<arrivo[3]);
  113. i=0;
  114. while(i!=1){}
  115. PORTC &= ~(1<<arrivo[0]);
  116. i=0;
  117. while(i!=1){}
  118. PORTC |=(1<<arrivo[0]);
  119. PORTC &= ~(1<<arrivo[1]);
  120. i=0;
  121. while(i!=1){}
  122. PORTC &= ~(1<<arrivo[0]);
  123. PORTC &= ~(1<<arrivo[1]);
  124.  
  125. i=0;
  126. while(i!=1){}
  127. PORTC |=(1<<arrivo[0]);
  128. PORTC |=(1<<arrivo[1]);
  129. PORTC &= ~(1<<arrivo[2]);
  130. i=0;
  131. while(i!=1){}
  132. PORTC &= ~(1<<arrivo[0]);
  133. i=0;
  134. while(i!=1){}
  135. PORTC |=(1<<arrivo[0]);
  136. PORTC &= ~(1<<arrivo[1]);
  137. i=0;
  138. while(i!=1){}
  139. PORTC &= ~(1<<arrivo[0]);
  140. i=0;
  141. while(i!=1){}
  142. PORTC |=(1<<arrivo[0]);
  143. PORTC |=(1<<arrivo[1]);
  144. PORTC |=(1<<arrivo[2]);
  145. PORTC |=(1<<arrivo[3]);
  146.  
  147.  
  148.  
  149.  
  150. break;
  151.  
  152. case D:
  153.  
  154. break;
  155.  
  156.  
  157.  
  158.  
  159. }
  160. }
  161. }
  162.  
  163. ISR(TIMER1_COMPA_vect)
  164. {i++;
  165. }
  166.  
  167. ISR(PCINT2_vect)
  168. {
  169. uint8_t changedbits;
  170. changedbits = PIND ^ portbhistory;
  171. portbhistory = PIND;
  172.  
  173. if(changedbits & (1 << PIND2))
  174. {
  175. /* PCINT0 changed */
  176. if( (PIND & (1 << PIND2)) == 1 )
  177. { /* LOW to HIGH pin change */ }
  178. else {tasto0=1;}
  179. }
  180.  
  181. if(changedbits & (1 << PIND3))
  182. {
  183. /* PCINT0 changed */
  184. if( (PIND & (1 << PIND3)) == 1 )
  185. { /* LOW to HIGH pin change */ }
  186. else{tasto1=1;}
  187.  
  188. }
  189.  
  190. if(changedbits & (1 << PIND5))
  191. {
  192. /* PCINT0 changed */
  193. if( (PIND & (1 << PIND5)) == 1 )
  194. { /* LOW to HIGH pin change */ }
  195. else
  196. { tasto3=1; }
  197. }
  198.  
  199. if(changedbits & (1 << PIND4))
  200. {
  201. /* PCINT0 changed */
  202. if( (PIND & (1 << PIND4)) == 1 )
  203. { /* LOW to HIGH pin change */ }
  204. else
  205. { tasto2=1; }
  206. }
  207.  
  208. if(changedbits & (1 << PIND6))
  209. {
  210. /* PCINT0 changed */
  211. if( (PIND & (1 << PIND6)) == 1 )
  212. { /* LOW to HIGH pin change */ }
  213. else
  214. { stato=B; i=0;}
  215. }
  216. if(changedbits & (1 << PIND7))
  217. {
  218. /* PCINT0 changed */
  219. if( (PIND & (1 << PIND7)) == 1 )
  220. { /* LOW to HIGH pin change */ }
  221. else
  222. { stato=A; }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement