1. Use of working mode 2
Here we will explain the working mode 2 of timer 0, which is to use the "small test tube" method. The initialization of TMOD is "TMOD=0x02;".
We know that the longest overflow time of working mode 2 is only 256*(12/11059200)=0.000277 seconds=277 microseconds.
We want to implement a running light experiment with an interval of 51ms. First, we define a variable cnt to record the number of overflows each time. Then our timing is 51 microseconds (because the maximum timing is only 278 microseconds). When the value recorded by cnt is equal to 1000, it proves that 51ms has passed, so we can execute the running light task again.
We already know that working mode 2 does not need to fill the initial value after the overflow. For example, if our timing time is 51 microseconds, then the calculated "distance overflow scale" is 47 (the actual calculated value is 47.0016, so the timing is 51 microseconds and the error is not large).
So the initial value of TL0 is 256-47=209=0xD1. Then the initial value of TH0 is also 0xD1, which ensures that the new initial value is equal to the value of TH0 after each TL0 overflow. Therefore, timer working mode 2 is also called 8-bit automatic reload mode. Readers need to learn these theoretical knowledge separately for related concepts.
2. Code for working mode 2
#include #include void main() { u8 i; u16 cnt; LED_Init(); //Initialize LED hardware module TMOD = 0x02; //Set timer 0 to working mode 2 TH0 = 0xD1; TL0 = 0xD1; //timing 51us TR0 = 1; //Start timer 0 while(1) { if (TF0 == 1) //This if statement can be entered every 51us { TF0 = 0; //After timer 0 overflows, clear the interrupt flag cnt++; //Record overflow times } if(cnt>=1000) //51ms has passed { cnt=0; P0=~(0x01< i++; } if(i>=8)i=0; } } If you compare the code of working mode 1 in the previous lecture, you will find that TH0 and TL0 no longer appear in the while loop of working mode 2. After each overflow of TL0, the initial value becomes the value of TH0 at the beginning, so the timing time has been determined before the while loop. You can try changing the 1000 in "if(cnt>1000)" to 10000, which will be a running light with an interval of 510ms. In the future, the timing time of the questions we do will generally be at least 1 millisecond, so there will be many opportunities to use working mode 1, while working mode 2 will be rarely used in our previous tutorials. We will review this knowledge point when we learn about serial communication.
Previous article:51 single chip microcomputer-simple application of timer
Next article:51 MCU-Use of timer interrupt function
Recommended posts
- Information Theory Foundations
- Thisbookisaconciseandeasy-to-understandtextbookinthefieldofinformationtheory.Themaincontentsinclude:introductiontoentropy,informationsource,channelcapacity,ratedistortion,datacompressionandcodingtheory,andcomplexity
- arui1999 Download Centre
- [MPS Mall Big Discount Experience Season] MPM54304 Unboxing
- ThankstoMPSandEEWorldforparticipatingintheMPSMPM54304evaluationboardorderrebateexperienceevent. IhaveseenMPSproductunboxingsonlineandwasparticularlyimpressedbythehugedifferencebetweenthelargeboxesandthesmall-si
- kit7828 Power technology
- Calculation of thermal resistance of MOS
- CalculationofthermalresistanceofMOS Thedefinitionsofthesetwothermalresistancesaredifferent. ThefirstisthethermalresistancefromthediePNjunctiontothecase,whichistheratioofthetemperaturedifferencebetweenthediea
- QWE4562009 Discrete Device
- Could you please tell me what are the four input modes of STM32 port used for? Please give some examples. Thank you.
- IwouldliketoaskwhatarethefourinputmodesoftheSTM32portusedfor?Pleasegiveexamples.Thankyou.Itwouldbebestifyoucanexplainitwithadiagram. IwouldliketoaskwhatsituationsthefouroutputmodesoftheS
- 深圳小花 MCU
- MSP430 MCU Example 18 - Timer A generates 4-channel periodic signals
- 1.TaskRequirements TheMSP30F247microcontrollerisusedasthecontroller,andthetimerTAisusedtogenerate4periodicsignalswithperiodsof4S,2S,1S,and0.25Srespectively.Thefourperiodicsignalsareoutputfromtheportscorrespon
- 火辣西米秀 Microcontroller MCU
- 【2024 DigiKey Creative Competition】3D Printing Monitoring and Failure Detection Based on Raspberry Pi5
- 3DPrintingMonitoringandFailureDetectionBasedonRaspberryPi5 Author:bzhou830 Introduction Inrecentyears,FDM3Dprintingtechnologyhasdevelopedveryrapidly,andmoreandmorefamilieshavebeguntousedesktop
- bzhou830 DigiKey Technology Zone
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Recruitment position in Xiamen IC design company: MCU product development engineer
- mpy implements partial PEP-498 (f-string) support
- 【LAUNCHXL-CC2650】+Building the development environment
- Collection of popular MCU data downloads
- DC Motor Control Circuit Collection
- [Me and Yatli] + First acquaintance
- TI's new automotive linear LED driver TPS92613-Q1 with enhanced heat dissipation
- Do you need to persist?
- Need help choosing safer lithium batteries?
- STM32H743IIT6 core board schematic diagram