Introduction to 51 MCU timer settings (STC89C52RC)

Publisher:等风来88888Latest update time:2016-05-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STC MCU timer setting


The use of STC microcontroller timer can be said to be very simple, as long as you understand the principle and have a basic understanding of C language. The key points are as follows:

1. You must know the original form of the English abbreviation, so you don't have to remember the name of the register.

Understanding is the best way to remember. A good textbook will give the original forms of all English abbreviations.

2. Try to use visual methods to remember

For example, the functions of each bit of the two registers TCON and TMOD are usually shown in a diagram in the tutorial. You can keep recalling the image of the diagram while studying.


TMOD: Timer/Counter Mode Control Register (TIMER/COUNTER MODE CONTROL REGISTER) The
                       
timer/counter mode control register TMOD is an 8-bit register defined bit by bit, but can only be addressed using bytes, and its byte address is 89H.
Its format is:

the lower four bits define the timer/counter C/T0, and the upper four bits define the timer/counter C/T1. Explanation of each bit:

GATE - gate control.
When GATE=1, the external interrupt pins INT0 and INT1 start timers T0 and T1.
When the INT0 pin is high, TR0 is set to start timer T0;
when the INT1 pin is high, TR1 is set to start timer T1.
When GATE=0, only TR0 and TR1 are set to start timers T0 and T1 respectively.

C/T - function selection bit
C/T=0 is the timing function, and C/T=1 is the counting function.
When set, select the counting function, and when cleared, select the timing function.

M0, M1——Mode selection function
Since there are 2 bits, there are 4 working modes:
M1M0 Working mode Counter mode TMOD (set timer mode)
0 0 Mode 0 13-bit counter TMOD=0x00
0 1 Mode 1 16-bit counter TMOD=0x01
1 0 Mode 2 Automatic reload 8-bit counter TMOD=0x02
1 1 Mode 3 T0 is divided into 2 8-bit independent counters, T1 is non-interrupt reload 8-bit counter TMOD=0x03
MCU timer 0 is set to working mode 1 as TMOD=0x01                   

Here we must know that the T of TMOD means TIMER/COUNTER, and MOD means MODE. As for the function of each bit, you only need to remember the chart and know the prototype of each English abbreviation. When
using TMOD in the program, first recall the chart immediately, and sort out the meaning of each bit according to the original form of the abbreviation. If the meaning is not very clear, check the manual. After a few times, the chart of TMOD will be in your mind.

8-bit GATE bit, which means gate.
7-bit C/T Counter/Timer
6-bit M1 Mode 1
5-bit M0 Mode 0


TCON: Timer/Counter Control Register (TIMER/COUNTER CONTROL REGISTER)

TMOD is divided into 2 sections, TCON control is more precise, divided into four sections, in this article only the upper four sections are used.

TF0 (TF1) - count overflow flag, when the counter count overflows, this position is 1.
TR0 (TR1) - timer operation control bit
When TR0 (TR1) = 0, stop the timer/counter
When TR0 (TR1) = 1, start the timer/counter
IE0 (IE1) - external interrupt request flag

When the CPU samples P3.2 (P3.3) and a valid interrupt request appears, this bit is set to 1 by hardware. After the interrupt response is completed and the interrupt service is turned to, it is automatically cleared to 0 by hardware.

IT0 (IT1) - external interrupt request signal mode control bit
When IT0 (IT1) = 1 pulse mode (negative jump on the trailing edge is valid)
When IT0 (IT1) = 0 level mode (low level is valid) This bit is set to 1 or cleared to 0 by software.
TF0 (TF1) - Count overflow flag

When the counter overflows, this bit is set to 1 by hardware. When the interrupt service is turned, the hardware automatically clears it to 0. There are two cases for the use of the count overflow flag: when the interrupt mode is used, it is used as an interrupt request flag; when the query mode is used, it is used as a query status bit.

Pay attention to the memory method and understand the original form of the word, so that you will never confuse TF and TR. The F in TF is the F in Overflow. There is a famous IT knowledge Q&A community abroad, the website is http://stackoverflow.com/
The R in TR is Run. The default is 0 not to run, of course it must be set to 1 to run.


Calculation of the delay time of the STC microcontroller STC89C52RC timer

The delay time should be calculated according to the crystal frequency, and different boards may be different.

Clock cycle:
1/clock source. On my current board, the crystal frequency is 11.0592M, which means the clock cycle is 1/11059200 seconds.

Machine cycle:
Generally, the 51 microcontroller has 12 clock cycles, and my board is 12/11059200 seconds .

The longest time for a single timing:
If it is a 16-bit counter, the maximum value of 16 bits is 65535, and a total of 65536 times can be counted. The basic constants must be remembered, and the maximum value of 8 bits is 255, which can be counted 256 times. Also remember the value represented by each bit on the 8-bit.
12 * 65536/11059200 = 0.0711 s, that is, the timing within 71 ms can be completed with a single timing. If the timing time exceeds 71 ​​ms, it will be looped.

How many machine cycles are needed for one timing:
Calculation formula: Timing seconds/machine cycle
For example, if I want to time 1 second, 1/(12/11059200) = 921600 times. The maximum count of a 16-bit counter is 65536 times, and 921600 times has long been exceeded. We can time 10 ms each time, and we can time 1 second by looping 100 times. 1 s is reduced by 100 times to 10 ms, which means we need to count 9216 times each time.

The initial value of the counter:
When timing 10 ms, if the counter starts counting from 0, we don’t know when it reaches 9216 times. So it should count 9216 times. The maximum count of a 16-bit counter is 95536 times, and then it overflows. Once it overflows, the TF bit of TCON will be set to 1. As long as we frequently check the TF bit, we can know when the 10ms timing is completed.
Calculation formula: Counter initial value = maximum count times - required count times
If the timing is 10 ms, the initial value of the counter is 65536 - 9216.

Calculate the high and low bits of the counter:
A 16-bit counter is composed of two 8-bit bits, and the maximum count times of 8 bits is 256. So:
Counter high bit = initial value/256
Counter low bit = initial value%256

Reference address:Introduction to 51 MCU timer settings (STC89C52RC)

Previous article:51 microcontroller timer interrupt control light 1S program
Next article:Hardware expansion method of 51 single chip microcomputer timer range

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号