From: Ivan Stefanov Date: Wed, 28 Oct 2020 19:34:21 +0000 (+0200) Subject: Timer 1 ISR and Registers X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=49f397589ae51df44a9a0ce051186635ac6e6d01;p=vmks.git Timer 1 ISR and Registers Set Timer 1 Output Compare Match Interrupt with frequency 1Hz. Setup with direct access to registers. --- diff --git a/Timer_ISR/Timer_ISR.ino b/Timer_ISR/Timer_ISR.ino new file mode 100644 index 0000000..1517d2e --- /dev/null +++ b/Timer_ISR/Timer_ISR.ino @@ -0,0 +1,38 @@ +boolean toggle1 = 0; + +void setup() { + pinMode(13, OUTPUT); + noInterrupts(); //cli(); // Stop reception of interrupts + + // Set Timer 1 Control registers and counter to 0 + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + + // Set compare match value = Fclk_io / (Fout * Prescaler) - 1 + OCR1A = 15624; // = (16MHz) / (1 * 1024) - 1 + TIMSK1 |= (1 << OCIE1A); // Enable compare match interrupt + + // Set Timer 1 Mode of operation and clock prescalers + TCCR1B |= (1 << WGM12); // Mode 4 + TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024 (=> enable the clock) + + + interrupts(); //sei(); +} + +void loop() { + // put your main code here, to run repeatedly: +} + +ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED) +//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low) + if (toggle1){ + digitalWrite(13,HIGH); + toggle1 = 0; + } + else{ + digitalWrite(13,LOW); + toggle1 = 1; + } +}