From 037b9f392e4587aaa06dbe3437ae9cc0939a1143 Mon Sep 17 00:00:00 2001 From: Ivan Stefanov Date: Tue, 12 Jan 2021 19:18:55 +0200 Subject: [PATCH] Upload Timer1 Configuration example Example for configuring Timer 1 with Output Compare Match A Interrupt --- .../Timer_1_CompMatchA_Interrupt.ino | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino diff --git a/Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino b/Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino new file mode 100644 index 0000000..99a5bfc --- /dev/null +++ b/Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino @@ -0,0 +1,52 @@ +volatile uint16_t var = 0; + +void setup() +{ + noInterrupts(); // Забраняваме глобално прекъсванията //cli(); + + // Зануляване на конфигурацията + TCCR1A = 0; + TCCR1B = 0; + TCCR1C = 0; + TCNT1 = 0; + OCR1A = 0; + OCR1B = 0; + ICR1 = 0; + TIMSK1 = 0; + TIFR1 = 0; + + // Режим на таймера - 14 Fast-PWM + TCCR1A = TCCR1A | (1 << WGM11); + TCCR1B = TCCR1B | (1 << WGM12); + TCCR1B = TCCR1B | (1 << WGM13); + // TCCR1A |= (1 << WGM11); + // TCCR1B = TCCR1B | (1 << WGM12) | (1 << WGM13); + + // Режим на изводите - неинв. + TCCR1A = TCCR1A | (1 << COM1A1) | (1 << COM1B1); + + // Честота - 500Hz + // Избираме Prescaler = 1 + ICR1 = 31999; // TOP = 16MHZ/(1*500Hz)-1 = 31999 + + // Коеф. на запълване - 75% + OCR1A = 23999; // DUTY = TOP * 75% = 31999*75% = 23999 + + // Прекъсване - CM A + TIMSK1 |= (1 << OCIE1A); + + // Пускане на таймера + TCCR1B |= (1 << CS10); + + interrupts(); // Разрешаваме глобално прекъсванията //sei(); +} + +ISR(TIMER1_COMPA_vect) +{ + var++; +} + +void loop() +{ + +} -- 2.39.5