To realize the timing function of the 80C51 microcontroller, a more convenient way is to use the timer/counter inside the microcontroller. Below we will explain the 80C51 microcontroller timer/counter in detail and analyze the working mode of the timer/counter.
-------, invalid bit.
PS---Serial port interrupt priority control bit.
PS=1, the serial port interrupt is defined as a high priority interrupt.
PS=0, the serial port interrupt is defined as a low priority interrupt.
PT1---Timer/Counter 1 interrupt priority control bit.
PT1=1, the timer/counter 1 interrupt is defined as a high priority interrupt.
PT1=0, the timer/counter 1 interrupt is defined as a low priority interrupt.
PX1---External interrupt 1 interrupt priority control bit.
PX1=1, the external interrupt 1 interrupt is defined as a high-priority interrupt.
PX1=0, external interrupt 1 interrupt is defined as a low priority interrupt.
PT0---Timer/Counter 0 interrupt priority control bit.
PT0=1, the timer/counter 0 interrupt is defined as a high priority interrupt.
PT0=0, the timer/counter 0 interrupt is defined as a low priority interrupt.
PX0---External interrupt 0 interrupt priority control bit.
PX0=1, external interrupt 0 interrupt is defined as a high priority interrupt.
PX0=0, external interrupt 0 interrupt is defined as a low priority interrupt.
Timer/counter operating mode register TMOD
TF1---Timer 1 overflow flag.
When timer 1 is full and overflows, TF1 is set to 1 by hardware and an interrupt is requested. After entering the interrupt service routine, it is automatically cleared to 0 by the hardware. It should be noted that if a timer interrupt is used, this bit does not need to be manually operated at all. However, if a software query method is used, when the bit is found to be 1, software needs to be cleared to 0.
TR1---Timer 1 operation control bit.
Cleared by software to turn off Timer 1. When GATE=1 and INIT is high level, TR1 is set to 1 to start timer 1; when GATE=0, TR1 is set to 1 to start timer 1.
TF0---Timer 0 overflow flag, its function and operation method are the same as TF1.
TR0---Timer 0 operation control bit, its function and operation method are the same as TR1.
IE1---External interrupt 1 request flag.
When IT1=0, bit level trigger mode, S5P2 samples the INT1 pin in each machine cycle. If the NIT1 pin is a constant level, it is set to 1, otherwise IE1 is cleared to 0.
When IT1=1, INT1 is an edge trigger mode. When INIT1 is low level during the first machine cycle sampling, IE1 is set to 1. IE1=1 means that external interrupt 1 is requesting an interrupt from the CPU. When the CPU responds to an interrupt and switches to the interrupt service routine, this bit is cleared by hardware.
IT1 external interrupt 1 trigger mode selection bit.
IT1=0, which is the level trigger mode, and the low level on the pin INT1 is active.
IT1=1, which is the edge trigger mode. The negative transition of the level on pin INT1 from high to low is effective.
IE0---External interrupt 0 request flag, its function and operation method are the same as IE1.
IT0---external interrupt 0 trigger mode selection bit, its function and operation method are the same as IT1.
From the above knowledge points, we can know that each timer has 4 working modes, and the working mode can be selected by setting the M1M0 bit in the TMOD register.
The counting number of mode 1 is 16 bits. For T0, the TL0 register serves as the low 8 bits and the TH0 register serves as the high 8 bits, forming a 16-bit plus 1 counter.
Calculation of initial value of timer T0
Once the timer is started, it starts counting by 1 on the original value. If we do not set TH0 and TL0 at the beginning of the program, their default values are both 0. Assume that the clock frequency is 12MHz and 12 clock cycles are One machine cycle, then the machine cycle at this time is 1us. It takes 216 -1 numbers to fully record TH0 and TL0. Then a pulse counter overflows, and then an interrupt is applied to the CPU. Therefore, it takes a total of 65536us to overflow once, which is approximately equal to 65.6ms. If we want to time 50ms, then we need to install an initial value for TH0 and TL0 first. After recording 50,000 numbers based on this initial value, the timer overflows. It happens to be an interrupt every 50ms. When we need to time it for 1s, when we write the program, after 20 50ms timer interrupts are generated, it is considered to be 1s, so that we can accurately control the timing time. When counting 50,000, the total number that should be loaded into TH0 and TL0 is 65536-50000=15536. Find the modulo of 15536 to 256: 15536/256=60 and load it into TH0. Find the remainder of 15536 to 256: 15536/ 256=176 is loaded into TL0.
The program code is as follows:
#include《reg52.h》
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
uchar num;
void main()
{
TMOD=0x01; //Set timer 0 bit working mode 1 (M1, M0 bit 0, 1)
TH0=(65536-45872)/256; //The initial value of 11.0592M crystal oscillator timing 50ms is 45872
TL0=(65536-45872)%256;
EA=1; //Enable total interrupt
ET0=1; //Enable timer 0 interrupt
TR0=1; //Start timer 0
while(1)
{
if (num==20) //If it reaches 20 times, indicate 1 second
{
led1=~led1; //Invert the status of the light-emitting tube
num=0;
}
}
}
void T0_TIme()interrupt 1
{
TH0=(65536-45872)/256; //Reload the initial value
TL0=(65536-45872)%256;
num++;
}
80C51 microcontroller timer working mode
Timer/counter for external interrupt expansion
The expansion method is to set the timer/counter to counter mode, set the initial counting value to full range, and connect the external interrupt source to be expanded to the external counting pin of the timer/counter. A falling edge signal is input from this pin, and a timer/counter overflow interrupt is generated after the counter is incremented by 1.
For example, use T0 to expand an external interrupt source. Set T0 to counter mode and work in mode 2. The initial values of TH0 and TL0 are both 0FFH. T0 allows interrupts and the CPU enables interrupts. Its initialization procedure is as follows:
MOV TMOD, #06H; Set T0 to counter mode 2
MOV TL0, #0FFH; Set initial counting value
MOV TH0, #0FFH
SETB TR0; start T0 work
SETB EA; CPU turns on interrupt
SETB ET0; enable T0 interrupt
Previous article:Design of temperature/humidity monitoring system based on SHTl5 smart sensor
Next article:Design of automotive MP3 wireless transmitter based on single chip AT89C52 and MC145152 chips
Recommended ReadingLatest update time:2024-11-23 07:36
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 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
- I want the model of two power chips. One is printed with 3YH 993 and the other is printed with 0B=R36.
- When attaching strain gauges, is it necessary to apply pressure before the glue is fully cured?
- New Industry, New Smart Manufacturing——STMicroelectronics Industrial Tour 2019 Chengdu invites you to participate!
- Apply for a trial of a new Tektronix oscilloscope and win a custom T-shirt!
- What is the difference between UWB and traditional communication technology? What is UWB ultra-wideband positioning
- The professional classification of buzzers is as follows
- Does anyone know how to decompile HEX files into C language?
- Help with a question about thermocouples
- TI invites you to fill out the questionnaire to win gifts | Personally customize your 2019 industrial application solution
- In what situations is serpentine routing suitable for high-speed applications? What are its disadvantages?