Application of timer in single chip microcomputer

Publisher:psi33Latest update time:2016-12-23 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Now that we know the registers related to the timer, let's make a timer program to consolidate what we have learned. Our program in this class will first use timer 0. When using a timer, the following steps are required:

Step 1: Set the special function register TMOD and configure the working mode.

Step 2: Set the initial value of counting registers TH0 and TL0.

Step 3: Set TCON and set TR0 to 1 to start the timer counting.

Step 4: Check the TF0 bit of the TCON register to monitor the timer overflow.


Before writing a program, we must first learn how to calculate how to use a timer to set time. Our crystal oscillator is 11.0592M, the clock cycle is 1/11059200, and the machine cycle is 12/11059200. If we want to set a time of 20ms, that is 0.02 seconds, we need x machine cycles to get 0.02 seconds. Let's calculate x*12/11059200=0.02, and get x= 18432. The overflow value of a 16-bit timer is 65536 (because 65535 plus 1 is an overflow), so we can do this, first give TH0 and TL0 an initial value, let them reach 65536 after 18432 machine cycles, that is, overflow, and after overflow, we can know by detecting the value of TF0, which is exactly 0.02 seconds. Then the initial value y = 65536 - 18432 = 47104, which is converted into hexadecimal as 0xB800, that is, TH0 = 0xB8, TL0 = 0x00.


In this way, we have made a 0.02 second timer. Careful students will find that if the initial value is directly 0x0000, until 65536 overflows, the maximum timer value is about 71ms. So what should we do if we want to time longer? The logic you learned in elementary school and the relationship between multiples can solve this problem.


Okay, let's use a program to implement this function.

#include

sbit LED = P0^0;

sbit ADDR0 = P1^0;

sbit ADDR1 = P1^1;

sbit ADDR2 = P1^2;

sbit ADDR3 = P1^3;

sbit ENLED = P1^4;

void main(){

    unsigned char cnt = 0; //Define a counting variable to record the number of T0 overflows

   

    ENLED = 0; // Enable U3 and select independent LED

    ADDR3 = 1;

    ADDR2 = 1;

    ADDR1 = 1;

    ADDR0 = 0;

    TMOD = 0x01; //Set T0 to mode 1

    TH0 = 0xB8; //Assign the initial value 0xB800 to T0

    TL0 = 0x00;

    TR0 = 1; //Start T0

    while (1){

        if (TF0 == 1){ //Judge whether T0 overflows

            TF0 = 0; //After T0 overflows, clear the interrupt flag

            TH0 = 0xB8; // and re-initialize

            TL0 = 0x00;

            cnt++; //Count value increases by 1

            if (cnt >= 50){ //Judge whether T0 overflows 50 times

                cnt = 0; //The count value is cleared after reaching 50 times

                LED = ~LED; //LED inversion: 0-->1, 1-->0

            }

        }

    }

}

There are comments in the program. Combined with the content learned in the previous chapters, it is not difficult to understand it by analyzing it yourself. The result achieved by this program is that the small light on the far right of the development board lights up for one second and turns off for one second, that is, it flashes at a frequency of 0.5Hz.


Keywords:MCU Reference address:Application of timer in single chip microcomputer

Previous article:MCU timer register
Next article:The truth table of the single chip digital tube

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号