How to set the timer?
For example:
Timing 50 microseconds
First of all, 50000 ÷ 1.085 = 46,082.94930875576. Starting from 0 and adding 19,453 times will overflow. This calculation will have errors, but the impact is relatively small and can be ignored.
TH0 = (65535 - 46082) / 256; // Place the quotient in the upper eight bits
TL0 = (65535 - 46082) % 256; // Put the remainder in the lower eight bits
Or 65535 - 46082 = 19453
In hexadecimal we get: 4BFD
Binary gets: 0100 1011 1111 1101
Put the obtained hexadecimal numbers into TH0 and TL0 respectively;
TH0 = 0x4b;
TL0 = 0xfd; //Timing 50ms
timer
Timer 0 working mode 1 16-bit timing mode, digital tube dynamically displays 0-10 stopwatch.
#include #include #define uint unsigned int #define uchar unsigned char sbit DU = P2^6;//digital tube segment selection sbit WE = P2^7;//Nigital tube segment selection //Common cathode digital tube segment selection table 0-9 uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,}; /*==================================== Function : delay(uint z) Parameter : z delay millisecond setting, value range 0-65535 Return value : None Description : 12T/Fosc11.0592M millisecond delay ====================================*/ void delay(uint z) { uint x,y; for(x = z; x > 0; x--) for(y = 114; y > 0; y--); } /*==================================== Function : display(uchar i) Parameter : i displays the numerical value, the value range is 0-255 Return value : None Description : Three-digit common cathode digital tube dynamic display ====================================*/ void display(uchar i) { uchar bai, shi, ge; bai = i / 100; //236 / 100 = 2 shi = i % 100 / 10; //236 % 100 / 10 = 3 ge = i % 10;//236 % 10 =6 //The first digital tube P0 = 0XFF;//Clear broken code WE = 1;//Open bit selection latch P0 = 0XFE; //1111 1110 WE = 0; //Latch bit selection data DU = 1; //Open segment selection latch P0 = tabel[bai];// DU = 0; //Latch segment selection data delay(5); //The second digital tube P0 = 0XFF;//Clear broken code WE = 1;//Open bit selection latch P0 = 0XFD; //1111 1101 WE = 0; //Latch bit selection data DU = 1; //Open segment selection latch P0 = tabel[shi];// DU = 0; //Latch segment selection data delay(5); //The third digital tube P0 = 0XFF;//Clear broken code WE = 1;//Open bit selection latch P0 = 0XFB; //1111 1011 WE = 0; //Latch bit selection data DU = 1; //Open segment selection latch P0 = tabel[ge];// DU = 0; //Latch segment selection data delay(5); } //Timer 0 initialization void timer0Init() { TR0 = 1; //Start timer 0 TMOD = 0X01; //Timer operating mode 1, 16-bit timer counting mode TH0 = 0x4b; TL0 = 0xfd; //Timing 50ms /* How to set the time? For example, the timer is 50 microseconds First of all, 50000 ÷ 1.085 = 46,082.94930875576. Starting from 0 and adding 19,453 times will overflow. This calculation will have errors, but the impact is relatively small and can be ignored. TH0 = (65535 - 46082) / 256; // Place the quotient in the upper eight bits TL0 = (65535 - 46082) % 256; // Put the remainder in the lower eight bits Or 65535 - 46082 = 19453 In hexadecimal we get: 4BFD Binary gets: 0100 1011 1111 1101 Put the obtained hexadecimal numbers into TH0 and TL0 respectively; TH0 = 0x4b; TL0 = 0xfd; //Timing 50ms */ } void main(){//The main function itself will loop uchar mSec, Sec; //Milliseconds and seconds storage variables timer0Init();//Timer 0 initialization while(1) { if(TF0 == 1)//Determine whether it overflows { TF0 = 0;//Software clears the overflow flag bit TH0 = 0x4b; TL0 = 0xfd; //Timing 50ms mSec++;//50ms to if(mSec == 20) { mSec = 0; Sec++; //1 second is up } } display(Sec); //Nigital tube display function if(Sec > 59) Sec = 0; // Clear seconds } }
Previous article:STC89C52 microcontroller serial communication
Next article:STC89C52 microcontroller matrix keyboard
Recommended ReadingLatest update time:2024-11-16 10:34
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
- Servo drive
- C2000 for Sensing and Signal Processing
- μCOS-II Programming Basics
- We spent 2 days to make a small 104 capacitor
- Understanding of Hyperlink Interface in TMS320C6678
- Sell various protocols car ecu
- For this four-tube synchronous boost topology, under what input voltage conditions is the input current the largest?
- Training materials from Huawei - very practical - practice needs theoretical support - that's what it says
- Does anyone know the official website of Bluetooth Nordic? Please send WeChat or SMS: 13570814011. Thank you!
- CH224K evaluation and solution application