What is a timer?
As the name implies, a timer is used for timing. In microcontroller applications, it is often used for various timings. For example, to make an LED light turn on once every 1 second. This 1 second is achieved by a timer.
Instruction cycle
The instruction cycle is the time it takes for the microcontroller to execute an instruction. This is also the minimum time unit for the timer. Clock frequency/4=instruction frequency. 1/instruction frequency=instruction cycle.
Assume that the current clock is 4MHZ, the 4MHz clock becomes 1MHz after being divided by 4, and its period is 0.0000001s, which is 1us. This 1us is the instruction cycle, and this 1us is also the minimum unit of timer timing.
Timer and prescaler
Assuming there is no prescaler, the timer is turned on and incremented by one every instruction cycle. Assuming the clock is 4MHz, the timer increments by one every 1us.
If there is a prescaler, assuming the prescaler is set to divide by 2, the timer will increase by 1 every 2 instruction cycles. If the prescaler is set to divide by 4, the timer will increase by 1 every 4 instruction cycles, and so on.
Timer interrupt flag
For example: TMR0 is an 8-bit timer, which is an 8-bit register. The 8-bit register can represent values from 0 to 255. That is to say, the timer can be incremented from 0 to 255. After reaching 255, it will become 0 again if it is incremented by one. At this time, the TMR0 timer interrupt flag (TMR0IF) becomes 1. (If the interrupt is not enabled, the interrupt program will not be executed.)
What is the relationship between the clock frequency and the timer interrupt overflow?
Below I have drawn a flow chart to understand all this in terms of frequency. Assume the clock frequency is 4MHz, the timer prescaler value is 2, and the timer initial value is 0.
1. First, the 4MHz clock is divided by 4 to become a 1MHz instruction frequency;
2. Then the prescaler divides the frequency by 2 to 0.5MHz and supplies it to the timer;
3. After the timer is divided by 256, the frequency becomes about 1952Hz and the frequency overflow interrupt occurs;
Then we can understand all this in terms of cycles.
1. First, the 0.25us clock cycle is divided by 4 to become a 1us instruction cycle;
2. Then the prescaler divides the frequency by 2 and turns it into a 2us period to supply the timer;
3. The timer increments by one every 2us, and when it reaches 256 times (256X2us=512us), an overflow interrupt occurs;
Hopefully the above flowchart will help you sort out the concepts.
Example description:
Assuming the clock cycle is 4MHZ, the LED is turned on every 50MS and off every 50MS. How can such a program be implemented?
How to achieve this 50ms?
1. Get the instruction cycle
4MHz/4=1MHz
1/1MHz=0.0000001s=1us
2. Get the pre-scaling
The maximum time of the timer should exceed 50mS, so the prescaler should be 256.
Pre-scaling X256 = maximum timing time. 256X256 = 65536us = 65.536ms greater than 50ms
3. Calculate the initial value of the timer
(Timer maximum value + 1) - (timing time / pre-division) = timer initial value.
255+1=256
50000/256=195.3125
256-195.3125=60.6875 rounded to the nearest integer, the initial value of the timer is 61.
Set the relevant registers. [page]
We generally need to set three places in the OPTION_REG register.
PS<2:0> is used to set the prescaler. The prescaler range is from 2 to 256.
PSA is set to 0 to assign the prescaler to the Timer0 module.
TMR0CS is set to 0 internal instruction cycle clock.
Example program:
/*Development environment MPLAB X IDE Model PIC16LF1823*/
#include
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_
OFF&CP_ON&CPD_OFF&BOREN_ON&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);
__CONFIG(PLLEN_OFF&LVP_OFF) ;
#define LED LATA5 /* You can also use #define LED RA5, but when PIC16LF1823 outputs the level, directly controlling LATA5 is faster, because the data sent to RA5 is eventually sent to LATA5 for execution*/
void init_fosc(void)
{
OSCCON = 0x68; //Clock is set to 4MHz
}
void init_gpio(void)
{
PORT = 0;
YEARS = 0;
ANSELA = 0;
TRISAbits.TRISA5=0; //Set RA5 port to output to control LED
}
void init_timer0(void)
{
OPTION_REG=0x87; //Prescaler is 256
}
int main(int argc, char** argv)
{
init_dark();
init_gpio();
init_timer0();
TMR0IF=0; //Clear TMR0 interrupt flag
TMR0=61; //Set the interrupt initial value to 61
while(1)
{
if(TMR0IF==1)//Is the timing time up?
{
LED = ~LED; //Change the state of LED
TMR0IF=0; //Clear TMR0 interrupt flag
TMR0=61; //Set the interrupt initial value 61 to prepare for the next 50ms timing
}
}
}
Previous article:Design of intelligent battery charging manager
Next article:PIC microcontroller I/O control
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Thank you + thank you EEWORLD community
- Medical electronics popular data download collection
- Tailing Micro B91 Development Kit --- Testing Power Consumption in BLE Connections
- GaN Power Devices
- Migrating between TMS320F28004x and TMS320F28002x
- K210 performance test again
- 5G high-frequency board material selection and multi-layer board design requirements
- EETALK: What changes has the epidemic brought about in technology?
- "Playing with the board" + Yu Zhennan's STM32 development board experiment 4
- TI engineers explain the new features of Bluetooth 5 and Zigbee 3.0