1. Capture high level time
We use the counting function of timer 0 to capture the high level time of the external pin. Timers are called differently in different usages. For example, if we want to know how long a process lasts, the counting method of the timer is called a counter.
The external pin we choose this time is still P1.6, and initially let this pin output a low level.
We have not mentioned before what happens when the third bit GATE in the lower four bits of TMOD is 1. Here is an explanation. When this bit is set to 1, if "TR0=1;" and P3.2 must be high, the count of timer 0 will be triggered (it will not be triggered when P3.2 is low), that is, TL0 will increase by 1 every (12/11059200) seconds, and after adding to 256 and becoming 0, TH0 will increase by 1. When it continues to add to 65535, there will be "TL0=255;" and "TH0=255;", and adding 1 will turn all to 0. Here we review the knowledge we have learned before.
So we connect P1.6 and P3.2. When TR0 is set to 1, the counting function will be turned on as long as P1.6 outputs a high level, and the counting function will be stopped when P1.6 outputs a low level. We then use the three digital tubes on the left to display the number of TH0, and the three on the right to display the number of TL0.
So we're going to do this
P1.6=0;
TR0=1;
P1.6=1; //Start counting
delay_ms(30); //delay for a while
P1.6=0; //Stop counting
Code First use software to test how much time "delay_ms(30);" actually takes.
Therefore, P1.6 maintains a high level time of 41.784ms.
After connecting P1.6 and P3.2 with Dupont wire, please note that our delay time should not exceed 71ms, that is, the duration of high level should not exceed 71ms, which is the same as the timing time cannot be set to 71ms at a time. Because the maximum count can only reach 65535.
2. Code
Download the code and observe the value displayed on the digital tube.
#include #include //Please use Dupont wire to connect P1.6 and P3.2 void main() { LED_Init(); //Initialize LED hardware module TMOD=0x09; //lower four bits 1001 BEEP=0; //Let P1.6 output low level first TR0=1; BEEP=1; //Start counting delay_ms(30); BEEP=0; //Stop counting LedBuff[0]=LedChar[TL0%10]; LedBuff[1]=LedChar[(TL0/10)%10]; LedBuff[2]=LedChar[(TL0/100)%10]; LedBuff[3]=0x7F&LedChar[TH0%10]; //Add decimal point for easy distinction LedBuff[4]=LedChar[(TH0/10)%10]; LedBuff[5]=LedChar[(TH0/100)%10]; while(1) { SEG_Scan(); //Read the values of TH0 and TL0 } } The digital tube shows 150.109, so (150*256+109)*(12/11059200)= 0.0417849s The captured high-level time is 41.7849ms, which can be said to be quite accurate. If you have forgotten why it is “150*256”, please go back and review the previous content of this chapter. The function of the seventh bit GATE of TMOD is the same as that of the third bit GATE, except that it uses timer 1 to count, and the pin that triggers the counting is P3.3. For details, please refer to section 5.2.2 of the document "Teaching You to Learn 51 Microcontrollers Step by Step"
Previous article:51 MCU-separate file package
Next article:51 MCU-Data Type Forced Conversion
Recommended ReadingLatest update time:2024-11-16 19:37
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
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
- 37 "Ten Thousand Miles" Raspberry Pi Car——ROS Learning (VSCode Debugging ROS)
- How to pack .mpy files into esp8266 micro python firmware package?
- Beidou short message development board based on STM32F103RET6
- Want to know more about UWB? Just read this article.
- When downloading a program to a PCB board in SWD mode, what will happen if the PCB board's own power is not disconnected?
- I would like to ask you guys, what is the value of the bypass capacitor for the LT1763 chip?
- Many people ask whether the soft board can run at high speed...
- 82 AD conversion design experience summary!
- stm32 camera experiment
- Control high power electromagnetic valve 24V. Is there anything wrong with the use of thyristor?