51 MCU - Timer working mode 2

Publisher:哈哈哈33Latest update time:2021-10-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 //See Chapter 6, Lecture 8 for details

 

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.


Reference address:51 MCU - Timer working mode 2

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
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号