Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. // global variable to count the number of overflows
  4. volatile uint8_t tot_overflow;
  5. // initialize timer, interrupt and variable
  6. volatile bool is_interupt;
  7.  
  8. volatile uint8_t counter;
  9. volatile uint8_t tab_size;
  10. volatile uint32_t tab[26] = { 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00};
  11.  
  12. void timer1_init()
  13. {
  14. sei();
  15.  
  16. // set up timer with prescaler = 8
  17. TCCR1B |= (1 << CS10);
  18. // initialize counter
  19. TCNT1 = 0;
  20. // enable overflow interrupt
  21. TIMSK1 |= (1 << TOIE1);
  22. // enable global interrupts
  23. sei();
  24. // initialize overflow counter variable
  25. tot_overflow = 0;
  26. is_interupt = false;
  27. }
  28. // TIMER1 overflow interrupt service routine
  29. // called whenever TCNT1 overflows
  30. ISR(TIMER1_OVF_vect)
  31. {
  32. // keep track of the number of overflows
  33. tot_overflow++;
  34. // check for number of overflows here itself
  35. // 61 overflows = 2 seconds delay (approx.)
  36. if(tot_overflow >= 61) // NOTE: '>=' used instead of '=='
  37. {
  38. is_interupt = true;
  39. tot_overflow = 0;
  40. // reset overflow counter
  41. }
  42. }
  43.  
  44. volatile bool is_on;
  45.  
  46. int main(void)
  47. {
  48.  
  49.  
  50. DDRB=0xFF;
  51.  
  52. is_on = false;
  53. tab_size = 26;
  54. counter = 0;
  55. //PORTB = 0x80;
  56.  
  57. // initialize timer
  58. timer1_init();
  59. // loop forever
  60. while(1)
  61. {
  62. if(is_interupt){
  63. if(counter >= tab_size){
  64. counter = 0;
  65. }
  66. PORTB = tab[counter];
  67. counter++;
  68. is_interupt = false;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement