Introduction to the basic knowledge of 51 single-chip microcomputer timer/counter

Publisher:luanzgcLatest update time:2020-07-13 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Basic Concepts

1. 89C51 has two counters T0 and T1, and 89C52 has one T2. Each counter is composed of two 8-bit RAM units, that is, each counter is a 16-bit counter, and the maximum counting capacity is 216=65536, remember that it is from 0-65535.

2. Where does the counting source provided to the timer come from? It is a pulse source obtained by dividing the crystal oscillator of the microcontroller by 12. A 12M crystal oscillator provides a pulse time interval of 1us to the counter.

3. Preset number counting method. If each pulse is 1 microsecond, it takes 65.536 milliseconds to count 65536 pulses. But if only 10 milliseconds are needed, what should we do? Just put 55536 in the counter in advance, so we only need to count 65536-55536=10000 times, which is 10 milliseconds.

 

2. Related registers

Special function register TMOD (89H)
is used for T1 and T0.

GATE   C/T   M1   M0   GATE  C/T   M1   M0

GATE: 0 is for internal pulse; 1 is for external pulse.

C/T: 0 for timer; 1 for counter.

M1M0: How it works

 

M1,M0 Range Characteristics

0,0 working mode 0 13 bits, 8192 times For compatibility with 51's predecessor 48 series.

0,1 working mode 1 16 bits, 65536 times 16 bits, other characteristics are the same as working mode 0, more commonly used.

1,0 Working mode 2 8 bits, 256 times Automatically reload the preset number, the preset number is placed in T0 (or T1)

                     Of the high 8 bits, only the low 8 bits participate in counting, usually used for

                     Baud rate generator.

 

1,1 working mode 3 8 bits, 256 times TH0 can only be used as a timer, TL0 can be used as a timer or counter

                     Overflow flag: TL0 still uses the original T0 flag.

                     TH0 borrows the mark of T1, so it can only be used in T1.

                     Only when T0 is operating in mode 2, T0 can operate in mode 3.

 

Special function register TCON (88H)

For timer/counter For interrupt

TF1    TR1   TF0    TR0    IE1   IT1    IE0    IT0

TR0/TR1: Timer switch.

TF0/TF1: changes from 0 to 1 after the count overflows

IT0/IT1: Timer/Counter interrupt enable bit.


In today's sharing, we will learn how to use the microcontroller/counter. In the previous sharing, we have come into contact with the concepts of microcontroller clock cycle, machine cycle and instruction cycle. Let's review:


 1 clock cycle = 1/crystal frequency M

 

 1 machine cycle = 12 (51 series has 12 clock cycles, some improved MCUs have 1 clock cycle) × clock cycle = 12 (1) × (1/crystal frequency M) = 1us (1/12us, if a 12M crystal is used).


 

 If the 12MHZ crystal oscillator works in the 12T mode of the 51 microcontroller, a timer is needed to perform a 50ms timing operation, and the timer works in mode 1, then how should we write the program?

  we know:

  A 12M crystal oscillator can generate 1M (106us) machine cycles per second (s).

 

  50ms requires 50×1000us×1us (1 machine cycle) machine cycles = 50,000 machine cycles.


  If the timer works in mode 1, it is a 16-bit counter with a maximum value of 65536 (216), which means it can count up to 65536 times. A 50ms timing operation requires 50,000 machine cycles, and 1 machine cycle = 1us, so 50,000 counting operations are required.


In order for the counter timer to work, the timer initial value needs to be pre-filled (initial value = maximum number of timer counts - required number of timer counts). Therefore, the timer initial value needs to be set to 15536 ​​= 65536-50000, that is, 3CB0H (decimal 15536 ​​converted to hexadecimal 3CB0), so TH0 = 0x3c, TL0 = 0xb0, the high bit is the value of TH0, and the low bit is the value of TL0.


 

  If a 11.0592MHZ crystal is used, and other conditions remain unchanged, a 11.0592M crystal can generate 0.9216M machine cycles per second, and 50ms requires 46080 machine cycles. The timer works in mode 1 and is a 16-bit counter with a maximum value of 65536, so the initial value needs to be set to 19456=65536-46080, that is, 4C00H, so TH0=0x4c, TL0=0x00.


  From the above analysis, we can derive the formula for setting the initial value of the 16-bit timer/counter, the high 8 bits TH0 and the low TL0:

  TH0=(65536-required number of counts)/256=initial value/256;

 

  TL0=(65536-required number of techniques)%6=initial value%6;


 

  What does 256 mean in the formula? Our timer is composed of two 8-bits, so we need to put the difference into these two 8-bits, and count from the lower 8-bit. The lower 8-bit can hold up to 256 machine cycles. When the 256 count is full, the higher 8-bit is used. That is, the higher 8-bit is added by 1 every time the lower 8-bit is full. The lower 8-bit is full as many times as the higher 8-bit is filled. The algorithm is to divide the difference by 256 and take the integer. The rest, which is the number of times less than 256, is placed in the lower 8-bit, which is the remainder of the difference.


  Next, let's look at the routine for timer/counter 0 to generate a timer interrupt.

 

  The crystal frequency is 12M, and the procedure is as follows:


#include

 

void timer0_init()

{

 TMOD = 0x01; //The timer works in mode 1, which is a 16-bit counter with a maximum value of 65536

 TL0 = 0xb0; //Assign the initial value 0xb0 to TL0

 TH0 = 0x3c; //Assign the initial value 0x3c to TH0

 TR0 = 1; //Timer starts counting

 ET0 = 1; //Timer interrupt enabled

 EA = 1; // Enable general interrupt

}

 

void main()

{

 timer0_init();

 while(1);

}

 

void timer0() interrupt 1

{

 TH0=(65536-50000)/256; //In the interrupt processing function, TH0 needs to be re-initialized.

 TL0=(65536-50000)%6; //TL0 needs to be re-initialized in the interrupt handling function.

 

Add the statement we want.

 

}


The above example shows how to use timer/counter 0 to implement interrupts. The microcontroller usually has a timer/counter 1, and its programming is similar to that of timer/counter 0.


In addition, the use of microcontroller timers/counters is not only reflected in the timer interrupt, but their use is always the same. Once you master one usage, it will not be difficult to master other applications.

Reference address:Introduction to the basic knowledge of 51 single-chip microcomputer timer/counter

Previous article:Introduction to the basic knowledge of IO port operation of 51 single-chip microcomputer
Next article:51 single chip microcomputer digital tube dynamic cycle left shift display mobile phone number

Recommended ReadingLatest update time:2024-11-16 14:33

C51中断(void timer1(void) interrupt 3 using 3)
interrupt indicates the interrupt priority, and using indicates the working register group used. interrupt x using y        The xx value after interrupt is the interrupt number, that is, which interrupt port this function corresponds to. Generally in 51,        0 is external interrupt 0          1 is timer 0        2
[Microcontroller]
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号