Timing counter principle
CPU Timing
Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)
State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.
Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.
Instruction cycle: The total time taken to complete one instruction, which is measured in machine cycles.
For example: When the external crystal oscillator is 12MHz, the specific value of the relevant cycle of the 51 microcontroller is:
Oscillation period = 1/12us;
Status cycle = 1/6us;
Machine cycle = 1us;
Instruction cycle = 1~4us;
Timing Counter Introduction
Basic Features
The 51 single-chip microcomputer has two sets of timers/counters, because they can be used for timing,
It can also count, so it is called a timer/counter.
The timer/counter and the microcontroller's CPU are independent of each other. The timer/counter works automatically without the CPU's involvement.
The timer/counter in the 51 single-chip microcomputer adds 1 to the data in the register based on the internal clock of the machine or the external pulse signal.
With the timer/counter, the efficiency of the microcontroller can be increased. Some simple repeated additions can be handled by the timer/counter. The CPU can handle more complex tasks. At the same time, precise timing can be achieved.
working principle
The timer/counter is essentially an add-1 counter. It increments by 1 as the input pulse of the counter, that is, the counter automatically increments by 1 for each pulse. When the counter is full of 1, another pulse will return the counter to zero, and the overflow of the counter will set the corresponding interrupt flag to 1, sending an interrupt request to the CPU (when the timer/counter interrupt is allowed). If the timer/counter works in the timing mode, it means that the timing time has expired; if it works in the counting mode, it means that the count value is full.
It can be seen that the count value of the add-1 counter is obtained by subtracting the initial count value from the value of the counter when it overflows.
51 single chip microcomputer timing counter structure
The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with the upper 8 bits and the lower 8 bits, respectively. TMOD is the working mode register of the timer/counter, which determines the working mode and function; TCON is the control register, which controls the start and stop of T0 and T1 and sets the overflow flag.
Timing counter control
The operation of the 51 MCU timer/counter is controlled by two special function registers: TMOD is used to set its working mode; TCON is used to control its startup and interrupt request.
Working mode register TMOD
The working mode register TMOD is used to set the working mode of the timer/counter. The lower four bits are used for T0 and the upper four bits are used for T1. Its format is as follows:
GATE is the gate bit. When GATE=0, it is used to control whether the start of the timer is affected by the external interrupt source signal. As long as TR0 or TR1 in TCON is 1 by software, the timer/counter can be started; when GATA=1, TR0 or TR1 must be 1 by software, and the external interrupt pin INT0/1 must also be high to start the timer/counter. That is, the start condition of the timer at this time adds the condition that the INT0/1 pin is high.
C/T: Timing/counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.
M1M0: Working mode setting bit. The timer/counter has four working modes.
Control register TCON
The lower 4 bits of TCON are used to control external interrupts, which have been introduced earlier.
Set the start and interrupt request of the timer/counter. The format is as follows:
TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.
TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software.
TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.
TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.
How the timer counter works
Method 0
Mode 0 is a 13-bit count, consisting of the lower 5 bits of TL0 (the upper 3 bits are unused) and the 8 bits of TH0. When the lower 5 bits of TL0 overflow, they carry to TH0. When TH0 overflows, the TF0 flag in TCON is set and an interrupt request is sent to the CPU.
The calculation formula for the initial count value is: X = 213-N. N is the number of counts.
The initial value of the timer can also be obtained by directly complementing the number of counts.
In counting mode, the counting pulse is an external pulse on the T0 pin.
Method 1
The number of count bits in mode 1 is 16 bits, with TL0 as the lower 8 bits and TH0 as the lower 8 bits.
As the high 8 bits, a 16-bit plus 1 counter is formed.
The relationship between the number of counts and the initial value of the count is: X = 216-N, N is the number of counts
Method 2
Mode 2 is an 8-bit counting mode with automatic reload of initial value.
The relationship between the number of counts and the initial value of the count is: X = 28 - NN is the number of counts
Working mode 2 is particularly suitable for use as a more accurate pulse signal generator.
Method 3
Mode 3 is only applicable to timer/counter T0. When timer T1 is in mode 3, it is equivalent to TR1=0 and counting stops.
Working mode 3 divides T0 into two independent 8-bit counters TL0 and TH0
Timing counter usage
The initialization procedure should complete the following tasks:
Assign values to TMOD to determine how T0 and T1 work.
Calculate the initial value and write it into TH0, TL0 or TH1, TL1.
How to calculate the initial value?
A machine cycle is the time it takes for the CPU to complete a basic operation.
Machine cycle = 1/microcontroller clock frequency.
The internal clock frequency of the 51 microcontroller is 12 times the external clock frequency. That is to say, when the frequency of the external crystal oscillator is input into the microcontroller, it must be divided by 12. For example, if you use a 12MHZ crystal oscillator, the internal clock frequency of the microcontroller is 12/12MHZ. When you use a 12MHZ external crystal oscillator, the machine cycle = 1/1M = 1us.
What is the initial value of our timing 1ms? 1ms/1us=1000. That is, to count 1000 numbers, the initial value = 65535-1000+1 (because in fact the counter overflows when it counts to 66636) = 64536 = FC18H
In interrupt mode, assign values to EA and enable timer interrupt.
Set TR0 or TR1 to start the timer/counter timing or counting.
Interrupt service function
Because the timer counter overflow time interval is relatively short, we use the accumulated variable to count or time a relatively long time here, but it should be noted that the statements and functions here should be as few as possible to reduce errors. If it is not automatically reloaded, the timer needs to be re-initialized.
Timer Counter Programming
D1 small light flashes every 1s
#include typedef unsigned char u8; typedef unsigned int u16; sbit led=P2^0; static i=0; //i is a global variable void time0() { EA=1; //Turn on the interrupt master switch ET0=1; //Turn on the timer counter 0 switch TR0=1; //Turn on the timer/counter to start working TMOD|=0x01; //Set the timer/counter working mode GATE=0 C/T=0 Select working mode 01 16-bit timer/counter TL0=0x18; //Set the initial value. We have a 1ms timing and need to count 1000 times, so the initial value is 65536-1000=55536, which is converted to hexadecimal, that is FC18 TH0=0xFC; //high four bits } void main() { time0(); while(1) { if(i==1000)//timing 1s { i=0; //initialize i and continue timing led=~led; //Reverse the LED state to make the LED flash every 1s } } } void timezhongduan0() interrupt 1 { TH0=0XFC; //Assign initial value to the timer and continue timing for 1ms TL0=0X18; i++; //accumulate i to achieve timing of 1s }
Previous article:51 MCU Learning——9--Temperature Sensor DS18B20
Next article:51 MCU Learning——8.1 External Interrupt 0 and 1
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
- GaN RF Circuits and Applications
- How to quickly learn to use a chip?
- Lei Jun lost the 1 billion bet with Dong Mingzhu. It seems that the typhoon still can't blow up the pigs.
- The pad is 0.3mm/center distance is 0.5mm and the wire cannot come out
- C2000 Code Generation Tools - Compiler
- I want to use an I/O port of the STM32F1 chip to connect an infrared sensor at a distance of 3 meters. I am worried that the transmission distance is too long. How can I achieve this?
- Op amp transfer function solves the problem
- [Repost] This article tells you the difference between Ethernet and broadband
- Passive buzzer driver
- [Reference Solution] Nuvoton Bluetooth Low Energy Microcontroller M031BT for Personal Electronic Massager