Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 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 PD7
  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.         if (index == 99){
  42.             index = 0;
  43.         }
  44.         if (bufor[index]){
  45.             LED_ON();
  46.             bufor[index] = 0;
  47.         }
  48.  
  49.         if (BUTTON_ON()){
  50.             bufor[index] = 1;
  51.         }
  52.         index++;
  53. }
  54.  
  55. int main()
  56. {
  57.     //bufor setup
  58.     //sleep mode select - IDLE
  59.     set_sleep_mode(SLEEP_MODE_IDLE);
  60.     //port setup
  61.     LED_DDR = (1 << LED);
  62.     BUTTON_PORT |= (1 << BUTTON);
  63.     initTimer();
  64.     while (1)
  65.     {
  66.         sleep_mode();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement