21 interrupt sources of the AVR Microcontroller
I/O port second function
There is a single AVR Microcontroller timer. The timer/Counter 1 is 16bit, and the rest of the timer/Counter 0 and Timer/Counter 2 are 8bit timers.
Timer Working Mode
- Normal Mode
- CTC Mode
- Fast PWM mode
- Phase Correction PWM mode
- Phase frequency correction PWM mode
- Input Capture Mode
Similar to the timer counter of 51 single-chip microcomputer, the Count register value is increased by 1 every time a clock exceeded according to the set clock frequency.
Registers used
T/C1 control register B: (controls the source and frequency of the clock)
Set the last three digits. 1 indicates that the timer is stopped, 2 ~ 6 indicates the internal clock of different frequencies, 7 ~ 8 indicates the external driver.
Count register
Calculate a pre-installed value based on the time of each interruption, and then start to count until 65535, resulting in overflow interruption, which is exactly the time we need. This time is the counting time from the pre-installed value to 65536.
Specific calculation:
Assume that the interrupt time is 1 second, that is to say, register overflow is required once every 1 second to generate an interrupt. Because the frequency of the AVR microcontroller is 8 MHz, that is, there are 8 m clock cycles per second, that is, the number of 8 m times per second. Let's assume that the first step we select is 001, that is, the selected clock frequency is 8 m/256 = 31250, that is, there are 31250 clock cycles per second, each clock cycle uses (1/31250) s, that is, every (1/31250) s counter plus 1. Therefore, the pre-installed value of the Register is 31250-65535 = 31250. convert it to hexadecimal notation 0x85edh.
Then, load the high and low bits of this number to the high and low ends of the register tcnt1.
Interrupt shield register and total interrupt
Toie1 = 1 is to enable t/C1 interrupt shielding.
When using an interrupt, you must first enable the total interrupt sreg | = bit (7)
[007] avr mcu Interrupt System