In the MCU (M16), the timer is a separate module. M16 has three independent timer modules, namely T/C0, T/C1 and T/C2; T/C0 and T/C2 are both 8-bit. Timer, while T/C1 is a 16-bit timer. The operation of the timer is a hardware module that runs independently of the CPU.
1. When does the timer start working (or counting)?
When TCCR0! =0x00 In any mode, as soon as the MCU is powered up, the T/C starts timing. In fact, TCCR0 is mainly the prescaler and waveform mode of the timer, and the setting of the comparison matching mode. When it comes to prescaling, you have to mention this module. This module is a module shared by T/C0 and T/C1, but it can be There are different crossover settings.
2. How does the timer work?
When it comes to the operation of the timer, there are three important parameters that must be said: TCNT0, OCR0, TIMSK, TCNT0 are the initial values ​​of the timer. When the timer starts working, it is accumulated from TCNT0 to 0XFF immediately. The accumulation process consumes Time is the timing time we need; OCR0 is a comparison setting value. When the value of TCNT0 is accumulated to OCR0 (TNCT0==OCR0), if the compare match interrupt function is enabled, then a comparison interrupt will be generated at this time. The value of OCR0 is generally set between the initial value of TCNT0 and 0XFF, and no value other than the comparison interrupt will be generated.
TIMSK is an interrupt enable bit setting, that is, we need to set the corresponding register bits of TIMSK when we need a timer overflow interrupt or a compare match interrupt function or both.
3. Interrupt use of the timer
A timer can have two interrupt resources available, one for overflow interrupts and the other for compare match interrupts, as described in 2 above. The overflow interrupt subroutine to be described generally has to be overloaded with the initial value of TCNT0. Otherwise, TCNT0 will count up from 0X00 to 0XFF, and the time taken is not the time we want. The comparison interrupt is when TCNT0==OCR0, the compare match interrupt occurs; therefore, only a small amount of processing code is inserted in the interrupt subroutine. Otherwise, the so-called interrupt nesting phenomenon occurs, because M16 does not support interrupt nesting, so Some of the code in the interrupt subroutine will not be executed, and in serious cases, the system will crash.
4, TCNT0 and OCR0 value conversion:
For an 8-bit timer, TCNT0 can generally be converted by the following formula:
TCNT0=256-(TV*F)/N; TV: Timing time to be set, unit, usF: Crystal frequency (MHz)N: Dividing factor
The timer runs independently. It does not take up CPU time, does not require instructions, and only needs to participate when calling the corresponding register.
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.
The timer T0 is used to briefly introduce the operation method of the timer. T0 has three registers that can be accessed by the CPU, TCCR0, TCNT0, OCR0. Let's look at the 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 for controlling the working mode details of the timer;
TCNT0 is the T/C register, its value is incremented or decremented by one in each duty cycle of the timer to realize the timing operation, and the CPU can read and write TCNT0 at any time;
OCR0: Output compare register, which contains an 8-bit data and is continuously compared with the counter value TCNT0. Match events can be used to generate an output compare interrupt or to generate a waveform on the OC0 pin.
Here is the simplest mode, TCNT has been incremented by one, reaches the maximum value of 0xFF and then cleared to the next count, in the above program.
TCCR0 = 0x00; the clock source of T0 is turned off and the timer stops working.
TCNT0 = 0x83; set the initial value of the T/C register, and let the timer start or count 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, divide from the clock by 8, and set the timer to start working.
After the initialization, the timer starts to work. TCNT0 is incremented by one every timer. When TCNT0 is equal to the value of OCR0, the T/C interrupt flag register - OCF0 in TIFR is set. If OCIE0 is 1 in TIMSK at this time (that is, it is allowed). T0 compare match interrupt), and global interrupt enable, compare match interrupt is running. TCNT0 and 0CR0 can be operated in the interrupt program to adjust the timer.
TCNT0 continues to increment by one. When it reaches 0xFF, the T/C interrupt flag register - TOV0 in TIFR is set. If TOIE0 is 1 in TIMSK at this time (that is, T0 overflow interrupt is enabled), and the global interrupt is enabled, the overflow interrupt will run. TCNT0 and 0CR0 can be operated in the interrupt program to adjust the timer.
The timer-related registers are also SREG and TIMSK. The former bit 1 controls the global mid-range enable, and the latter bit 1 (OCIE0) and bit 0 (TOIE0) control the compare match interrupt and the overflow compare match interrupt enable, respectively.
In the actual process, the operation of the timer-related registers is very flexible. The value of TCNT0 can be modified in the overflow interrupt, and the value of OCR0 can be modified in the interrupt. In the following experiments, the method of modifying OCR1A with timer 1 will be implemented. 1S precise timing.
The correct harness can improve the overall performance of the different headlight parts and increase the lighting output of the headlights. Some harnesses are designed to complement higher performance bulbs. So if you have recently upgraded your headlights, you might need a new harness to keep the new lights performing efficiently. When looking for a new harness, you need to be sure that all the parts of the headlight wire harness kit are of the finest quality-not only the wires but as well as the relays, sockets, and terminals. The parts should be tough and must not be susceptible to burning. Also, check whether the harness is compatible with your vehicle.
car headlight harness
Dongguan YAC Electric Co,. LTD. , https://www.yacentercns.com