In MCU (M16), the timer is an independent module, M16 has three independent timer modules, namely T/C0, T/C1 and T/C2; among them, T/C0 and T/C2 are both 8-bit Timer, and T/C1 is a 16-bit timer. The work of the timer is independent of the hardware module that runs on its own outside the CPU.
1. When did the timer start working (or counting)?
Be TCCR0! =0x00 In any mode, as long as the MCU is powered on, the T/C will start timing work. In fact, TCCR0 is mainly the timer's prescaler, waveform mode, and comparison match mode settings. Speaking of prescaler, I have to mention this module. This module is a module shared by T/C0 and T/C1, but it can There are different crossover settings.
2. How does the timer work?
When it comes to the work of the timer, I have to say three important parameters: TCNT0, OCR0, TIMSK, TCNT0 is the initial value of the timer to set the timing, immediately after the timer starts to work, it will be accumulated from TCNT0 to 0XFF, the accumulation process consumed Time is the timing time we need; OCR0 is a comparison setting value. When the value of TCNT0 accumulates to OCR0 (TNCT0==OCR0), if the comparison match interrupt function is turned on, then a comparison interrupt will be generated at this time, so, The value of OCR0 is generally set between the initial value of TCNT0 and 0XFF, and any value other than that will not generate a comparison interrupt.
TIMSK is an interrupt enable bit setting, that is, when we need timer overflow interrupt or compare match interrupt function or both, we set the corresponding register bit of TIMSK.
3. Interrupt use of timer
A timer can have two interrupt resources available, one is only overflow interrupt, the other is compare match interrupt, as mentioned in 2 above. The overflow interrupt subroutine that I want to explain generally needs to reload the initial value of TCNT0, otherwise, TCNT0 will count up from 0X00 to 0XFF, and the time consumed is not the time we want. The comparison interrupt is when TCNT0==OCR0, a comparison match interrupt occurs; therefore, generally only a small amount of processing code is inserted in the interrupt subroutine, otherwise, the so-called interrupt nesting phenomenon will occur, because M16 does not support interrupt nesting, so It will make part of the code in the interrupt subroutine impossible to execute, and will cause the system to crash in severe cases.
4. Value conversion of TCNT0 and OCR0:
For 8-bit timers, TCNT0 can generally be converted by the following formula:
TCNT0=256-(TV*F)/N; TV: the timing time you want to set, unit, usF: crystal frequency (MHz) N: division factor
The timer runs independently, it does not take up CPU time, does not require instructions, and only needs to participate when the corresponding register is called.
Take AVR mega16 as an example. It has three registers, TImer0, TImer1 and timer2. T0 and T2 are 8-bit timers, T1 is a 16-bit register, and T2 is an asynchronous timer. All three timers can be used to generate PWM.
Take the timer T0 to briefly introduce the operation method of the timer. T0 has three registers that can be accessed by the CPU, TCCR0, TCNT0, and OCR0. Let's look at a section of timer initialization program generated by ICC.
//TIMER0 initialize-prescale:8 // WGM: Normal // desired value: 1KHz // actual value: 1.000KHz (0.0%) void timer0_init(void) {TCCR0 = 0x00; //stop TCNT0 = 0x83; //set count OCR0 = 0x7D; //set compare TCCR0 = 0x02; //start timer}
TCCR0 is a control register, used to control the details of the timer's working mode;
TCNT0 is a T/C register. Its value is added or subtracted by one in each working cycle of the timer to realize timing operation. CPU can read and write TCNT0 at any time;
OCR0: Output compare register, it contains an 8-bit data, which is continuously compared with the counter value TCNT0. The match event can be used to generate an output compare interrupt, or to generate a waveform on the OC0 pin.
Here is the simplest mode, TCNT keeps increasing by one, reaching the maximum value of 0xFF and then clearing it to enter the next count, in the above program.
TCCR0 = 0x00; Turn off the clock source of T0, and the timer stops working.
TCNT0=0x83; Set the initial value of the T/C register, and let the timer start timing or counting from TCNT0 from 0x83.
OCR0 = 0x7D; Set the value of the compare match register, which is not used in this program.
TCCR0 = 0x02; Select the clock source, which comes from the clock divided by 8. After setting, the timer will start to work.
After initialization, the timer starts to work. TCNT0 adds one to each timer clock. When TCNT0 is equal to the value of OCR0, the T/C interrupt flag register-OCF0 in TIFR is set. At this time, OCIE0 in TIMSK is 1 (that is, it allows T0 compare match interrupt), and the global interrupt is enabled, the compare match interrupt runs immediately. TCNT0 and 0CR0 can be operated in the interrupt program, and the timer can be adjusted.
TCNT0 continues to increase by one. When it reaches 0xFF, TOV0 in the T/C interrupt flag register-TIFR is set. If TOIE0 in TIMSK is 1 at this time (that is, T0 overflow interrupt is allowed), and the global interrupt is enabled, the overflow interrupt runs. TCNT0 and 0CR0 can be operated in the interrupt program, and the timer can be adjusted.
The timer-related registers also include SREG and TIMSK. The former bit 1 controls the global mid-segment enable, while the latter bit 1 (OCIE0) and bit 0 (TOIE0) respectively control the compare match interrupt and overflow compare match interrupt enable.
In the actual process, the operation of timer-related registers is very flexible. The value of TCNT0 can be modified in the overflow interrupt, and the value of OCR0 can also be modified in the interrupt. The following experiment will talk about using timer 1 to modify OCR1A. 1S precise timing.
4G Module,4G Router Module,4G Wifi Module,4G Lte Wifi Module
Shenzhen MovingComm Technology Co., Ltd. , https://www.movingcommtech.com