/*
=== TIMER1 ===
1. ** Normal Mode **
2. CTC Mode
3. Fast PWM
4. Phase Correct PWM
5. Phase and Frequency Correct PWM
6. Using the Input Capture Pin
7. External clock
Timer/Counter1 Register TCNT1:
- TCNT1 is 16 bits. Counts corresponding to the system clock, can be slowed down with a prescaler.
Output Compare Registers OCR1A and OCR1B:
- Can define values that are compared with the TCNT1 register. (Compare Match) triggers certain actions.
Timer/Counter1 Control Registers TCCR1A, TCCR1B:
- Choice of Wave Form Generation Mode via the WGM13, WGM12, WGM11, WGM10 Bits
- Determination of what happens in case of a Compare Match COM1A0, COM1B0, COM1A1, COM1B1 Bits
- Prescaler or external clock via the Chip Select Bits CS12, CS11, CS10
Input Capture Register ICR1
- If there is an event on ICP1, the counter reading of TCNT1 is written to ICR1.
- ICES1 bit in register TCCR1B, (ICES1 = 1) rising edge, or (ICES1 = 0) falling edge.
- Like OCR1A, ICR1 is the Top value in some WGM1 modes.
In these cases, the Input Capture Register function is disabled. Unlike OCR1A,
ICR1 is not buffered, but is immediately overwritten.
Timer/Counter1 Interrupt Mask Register TIMSK1:
- In TIMSK1, you enable the interrupts for the Input Capture function (ICIE1),
the Output Compare Matches (OCIE1B, OCIE1A), and the Timer Overflow (TOIE1).
The “IE” stands for “Interrupt Enable”.
If interrupts are enabled:
TIMER1_CAPT_vect for Input Capture
TIMER1_COMPA_vect / TIMER1_COMPB_vect for Compare Match
TIMER1_OVF_vect for Timer Overflow
Timer/Counter1 Interrupt Flag Register TIFR1:
- If the interrupts are enabled, the corresponding bits are set in TIFR1.
Output Compare Pins OC1A and OC1B:
- Timer1 has two Output Compare pins
- The behavior of the Output Compare pins depends on the setting of the WGM1 bits
and the Compare Output bits in TCCR1A and TCCR1B.
Settings in TCCR1A and TCCR1B:
- Wave Form Generation Modes - 16 modes
- Clock Select Bits / Prescaler
- Compare Output Mode Bits
==== Review this (excellent) webpage for help ====
https://wolles-elektronikkiste.de/en/timer-and-pwm-part-2-16-bit-timer1$0
*/
/*
Starting at 0 there are 65536 timer ticks untill overflow.
Using https://eleccelerator.com/avr-timer-calculator/$0 this results in
an overflow time at 1.048576 seconds. If you enter '1' second as the 'Real Time'
the 'Total Timer Ticks' = 62500. 65536 - 3036 = 62500. So, setting counterStart
to 3036 = a true 1 second interval. 65536 - 49911 = 15625 or .25 second interval.
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned int counterStart = 49911; // alternative: 3036, 49911
ISR(TIMER1_OVF_vect){
TCNT1 = counterStart;
PORTD ^= (1 << PD7);
}
int main(void) {
Serial.begin(9600);
TCCR1A = 0x00; // OC2A and OC2B disconnected; Wave Form Generator: Normal Mode
TCCR1B = (1 << CS12); // prescaler = 256; alternative: 1024 (set CS12 and CS10)
TIMSK1 = (1 << TOIE1); // interrupt when TCNT1 is overflowed
TCNT1 = counterStart;
DDRD |= (1 << PD7);
sei();
while(1) {
// Do other stuff
Serial.println(TCNT1); // TCNT1
}
return 0;
}