Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <avr/sleep.h>
  5.  
  6. #define LED PB0
  7. #define LED_PORT PORTB
  8. #define LED_DDR DDRB
  9.  
  10. #define BUTTON PD2
  11. #define BUTTON_PIN PIND
  12. #define BUTTON_PORT PORTD
  13.  
  14.  
  15. #define LED_ON() do {(LED_PORT |= _BV(LED)); } while(0)
  16. #define LED_OFF() do {(LED_PORT &= ~_BV(LED)); } while(0)
  17. #define BUTTON_ON() bit_is_clear(BUTTON_PIN, BUTTON)
  18.  
  19.  
  20.  
  21. void initTimer(void) {
  22.   //timer1
  23.   TCCR1B |= (1 << WGM12); // TC1 CTC  top at  OCR1A
  24.   TCCR1B |= (1 << CS11) ; //cpu/8
  25.   // initialize counter
  26.   TCNT1 = 0;
  27.   // initialize compare value (10ms)
  28.   OCR1A = 19999;
  29.   // enable compare interrupt
  30.   TIMSK1 |= (1 << OCIE1A);
  31.   // enable global interrupts
  32.   sei();
  33. }
  34.  
  35. volatile uint8_t bufor[100] = {0};
  36. volatile uint8_t index = 0;
  37.  
  38. ISR (TIMER1_COMPA_vect)
  39. {
  40.     LED_OFF();  
  41.  
  42.     if (bufor[index])
  43.     {
  44.       LED_ON();
  45.       bufor[index] = 0;
  46.     }
  47.  
  48.     if (BUTTON_ON())
  49.       bufor[index] = 1;
  50.    
  51.     if (index == 99)
  52.       index = 0;
  53.  
  54.     index++;
  55. }
  56.  
  57. int main()
  58. {
  59.   //sleep mode select - IDLE
  60.   set_sleep_mode(SLEEP_MODE_IDLE);
  61.   //port setup
  62.   LED_DDR = (1 << LED);
  63.   BUTTON_PORT |= (1 << BUTTON);
  64.   initTimer();
  65.   while (1)
  66.   {
  67.     sleep_mode();
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement