Knowledge about 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;
How does the timer/counter work?
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 counter value is the value of the counter when it overflows minus the initial count value plus 1.
51 MCU timer 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. 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 of T0 and T1.
Timer/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.
1. 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.
2. 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
1. 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.
In timer mode: N = t/ Tcy
The formula for calculating the initial value of the count is: X=213-N.
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.
The gate bit GATE has a special function. When GATE=0, the inverted
Then the output of the OR gate is 1. At this time, only TR0 controls the opening of the AND gate, and the output of the AND gate is
When GATE=1, the control switch is turned on and counting starts; when GATE=1, the external interrupt
The pin signal controls the output of the OR gate. At this time, the opening of the AND gate is controlled by the external interrupt pin.
The signal is controlled together with TR0. When TR0=1, the external interrupt pin signal pin
A high level starts counting, and a low level of the external interrupt pin signal pin stops counting.
This method is often used to measure the width of the positive pulse on the external interrupt pin.
How the timer/counter works
2. 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
How the timer/counter works
3. 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-N
Working mode 2 is particularly suitable for use as a more accurate pulse signal generator
4. 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.
What to do when using a timer
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.
In interrupt mode, assign values to EA and enable timer interrupt.
Set TR0 or TR1 to start the timer/counter timing or counting
Calculation of initial value of counter
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)
Experimental phenomenon: D2 lights up once every 1s.
By calling the timing interrupt, an interrupt is requested from the CPU every 1ms, i is increased by 1, and the cumulative increase is 1000. After 1s, the LED state is reversed.
#include "reg52.h" //This file defines some special function registers of the microcontroller
typedef unsigned int u16; //declare and define the data type
typedef unsigned char u8;
sbit led=P2^0;
void Timer0Init()
{
//TMOD high four bits T1, low four bits T0 0000 0001 0X01
TMOD |= 0x01;
//Initial value = 65535-1000+1 = 64536 = FC18H timing 1ms
TH0=0XFC;
TL0=0X18;
ET0=1; //Turn on T0 timer interrupt
EA=1; // Total interrupt
TR0=1;
}
void Timer1Init()
{
//TMOD high four bits T1, low four bits T0 0000 0001 0X01
TMOD |= 0x10;
//Initial value = 65535-1000+1 = 64536 = FC18H timing 1ms
TH0=0XFC;
TL0=0X18;
ET1=1; //Turn on T1 timer interrupt
EA=1; // Total interrupt
TR1=1;
}
void main()
{
Timer1Init();
while(1);
}
void Time0() interrupt 1
{
static u16 i ;
TH1=0XFC;
TL1=0X18;
i++;
if(i==1000)//1s
{
i = 0;
led=~led;
}
}
void Time1() interrupt 3
{
static u16 i ;
TH1=0XFC;
TL1=0X18;
i++;
if(i==1000)//1s
{
i = 0;
led=~led;
}
}
Previous article:[51 MCU learning process record] 7 Interrupt timer counter 0 understanding before operation
Next article:51 single chip microcomputer (twenty-one) - timer counting function
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- The power supply cannot be turned on when the USB flash drive is plugged in. What is the reason?
- Understanding of a curve graph in Chapter 4 of "High Frequency Electronic Circuits. Zeng Xingwen Edition"
- About 5v problem
- GD32 ADC pin setting problem
- Which BLE chip do you use for your project development?
- EEWORLD University Hall----Live Interview: TI C2000 Piccolo Single Chip - Realize Dual-Axis Servo Motor and Motor Control
- Software industry transforms to the Internet
- Cigarette lighter passed automotive electronics 7637 certification
- EEWorld 15th Anniversary "Easter Egg" - Pay postage to get a development board and books
- Regarding the problem of battery catching fire under stress, can someone explain it?