Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #define F_CPU 7372800UL
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6.  
  7. static uint8_t br[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
  8. static uint8_t sec, msec;
  9. static uint8_t seg[4] = {_BV(4), _BV(5), _BV(6), _BV(7)};
  10. static uint8_t play;
  11.  
  12. static uint8_t state = 0x00;
  13.  
  14. ISR(TIMER1_COMPA_vect)
  15. {
  16. if(play == 1) {
  17. state ^= 0x80;
  18. }
  19. }
  20.  
  21. ISR(TIMER0_COMP_vect)
  22. {
  23. //za brojeve
  24. if(play == 1){
  25. msec++;
  26. if(msec == 100)
  27. {
  28. msec = 0;
  29. sec++;
  30.  
  31. if(sec == 60)
  32. {
  33. sec = 0;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. void debounce()
  40. {
  41. _delay_ms(400);
  42. }
  43.  
  44. int main(void)
  45. {
  46. DDRA = 0xff;
  47. PORTA = 0xff;
  48.  
  49. PORTB = _BV(0) | _BV(1); //moramo dignuti pull-up zbog key-eva
  50. DDRB = 0xf0;
  51.  
  52. sec = 0;
  53. msec = 0;
  54. play = 1;
  55. //timer 1
  56.  
  57. TCCR1B = _BV(WGM12) | _BV(CS12);
  58.  
  59. OCR1A = 14399; //za tockicu,imamo formulu s ctcom s 2 zato sto je pola sekunde upaljena,pola ugasena
  60.  
  61. //timer 0
  62.  
  63. TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00);
  64.  
  65. OCR0 = 71; //bez 2 jer se racuna vrijeme i nema blinkanja nikakvog
  66.  
  67. TIMSK = _BV(OCIE1A) | _BV(OCIE0);
  68.  
  69. sei();
  70.  
  71.  
  72. while (1)
  73. {
  74. PORTB = seg[0];
  75. PORTA = br[sec/10];
  76. _delay_ms(1);
  77.  
  78. PORTB = seg[1];
  79. PORTA = br[sec%10];
  80. PORTA ^= state;
  81. _delay_ms(1);
  82.  
  83. PORTB = seg[2];
  84. PORTA = br[msec/10];
  85. _delay_ms(1);
  86.  
  87. PORTB = seg[3];
  88. PORTA = br[msec%10];
  89. _delay_ms(1);
  90.  
  91. PORTB |= 0x0f; //zato sto gore mijenjamo PORTB,pa PINB kod key-a ne zna u kojem stanju je PORTB trenutno(izgubio je stanje 0x0f of gore)
  92.  
  93. //ovo za key-eve mora biti u while-u
  94. if(bit_is_clear(PINB,0)) {
  95. play ^= 1;
  96. }
  97. if(bit_is_clear(PINB,1)) {
  98. sec = 0;
  99. msec = 0;
  100. }
  101.  
  102.  
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement