Advertisement
czaru34

piecdwac

Jan 14th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. //Cezary Zaprawa
  2. //Jedna dioda swieci 500/2000ms
  3. //Co kliknięcie przycisku animacja jest wykonywana na kolejnych diodach
  4. //Częstotliwość: 1MHz
  5.  
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8.  
  9. #define BUTTON_MASK (1<<PD2)
  10. #define BUTTON_PIN PIND
  11. #define BUTTON_PORT PORTD
  12.  
  13. volatile uint8_t button_down;
  14.  
  15. volatile int int0Counter = 1;
  16. volatile uint8_t counterHolder = 1;
  17.  
  18. ISR(TIMER0_OVF_vect) {
  19.     static uint8_t count = 0;
  20.     static uint8_t button_state = 0;
  21.     uint8_t current_state = (~BUTTON_PIN & BUTTON_MASK) != 0;
  22.     if (current_state != button_state) {
  23.         count++;
  24.         if (count >= 4) {
  25.             button_state = current_state;
  26.             if (current_state != 0)
  27.             button_down = 1;
  28.             count = 0;
  29.         }
  30.         } else {
  31.         count = 0;
  32.     }
  33. }
  34.  
  35. ISR(INT0_vect) {
  36.     if(button_down) {
  37.         if(counterHolder != 255) {
  38.             int0Counter *= 2;
  39.         } else {
  40.             int0Counter = 1;
  41.             counterHolder = 1;
  42.         }
  43.         counterHolder |= int0Counter;
  44.     }
  45. }
  46.  
  47. ISR(TIMER1_COMPA_vect) {
  48.     PORTA = 0;
  49. }
  50.  
  51. ISR(TIMER1_OVF_vect) {
  52.     PORTA |= counterHolder;
  53. }
  54.  
  55. void init() {
  56.     cli();
  57.     //+++Konfiguracja wyświetlaczy+++
  58.     DDRA |= 0xFF;
  59.     DDRB |= 0xFF;
  60.     //+++Konfiguracja przerwania INT0+++
  61.     PORTD |= (1 << PD2);
  62.     MCUCR |= (1 << ISC01);
  63.     GICR |= (1 << INT0);
  64.     //+++Konfiguracja timera0+++
  65.     TCCR0 |= (1 << WGM01);
  66.     TIMSK |= (1 << TOIE0);
  67.     OCR0 = 98;
  68.     //+++Konfiguracja Timera1+++
  69.     OCR1A = 7813;
  70.     ICR1 = 31250;
  71.     TCCR1B |= (1 << WGM12) | (1 << WGM13);
  72.     TCCR1A |= (1 << WGM11);
  73.     TCCR1B |= (1 << CS11) | (1 << CS10);
  74.     TIMSK |= (1 << OCIE1A) | (1 << TOIE1);
  75.     sei();
  76. }
  77. int main(void) {
  78.     init();
  79.     while(1) {
  80.         PORTB = TCNT0;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement