Application of Timer/Counter T0 of AT89S51 Microcontroller

Publisher:EtherealBeautyLatest update time:2023-05-19 Source: elecfansKeywords:AT89S51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Experimental tasks

Use the timer/counter T0 of the AT89S51 microcontroller to generate a one-second timing time as the second counting time. When one second is generated, the second counting increases by 1. When the second counting reaches 60, it automatically starts from 0. The hardware circuit is shown in the figure below


2. Circuit schematic

Application of Timer/Counter T0 of AT89S51 Microcontroller

Figure 4.15.1

3. Hardware wiring on the system board

(1). Connect the P0.0/AD0-P0.7/AD7 ports in the "SCM System" area to any a-h port in the "Four Static Digital Display Module" area with an 8-core cable; Requirement: P0. 0/AD0 corresponds to a, P0.1/AD1 corresponds to b,..., P0.7/AD7 corresponds to h.

(2). Connect the P2.0/A8-P2.7/A15 ports in the "SCM System" area to any a-h port in the "Four Static Digital Display Module" area with an 8-core cable; Requirements: P2. 0/A8 corresponds to a, P2.1/A9 corresponds to b,..., P2.7/A15 corresponds to h.

4. Programming content

The internal 16-bit timer/counter of the AT89S51 microcontroller is a programmable timer/counter. It can work in 13-bit timing mode, 16-bit timing mode and 8-bit timing mode. Just by setting the special function register TMOD, it can be completed. When the timer/counter works is also accomplished by setting the TCON special function register through software.

Now we choose the 16-bit timing working mode. For T0, the maximum timing is only 65536us, which is 65.536ms, which cannot achieve the 1 second timing we need. Therefore, we must deal with this problem through software. Suppose we take T0 The maximum timing is 50ms, that is, 20 times of 50ms are required to time for 1 second. For these 20 times, we can use software to count them.

Therefore, we set TMOD=00000001B, that is, TMOD=01H

Next we need to load the preset initial values ​​for TH0 and TL0 of the T0 timer/counter, which can be calculated by the following formula

TH0=(216-50000) / 256

TL0=(216-50000) MOD 256

When T0 is working, how do we know that the 50ms timing time has expired? This time we detect the TF0 flag in the TCON special function register. If TF0 = 1, it means that the timing time has expired.

5. program

Application of Timer/Counter T0 of AT89S51 Microcontroller

Figure 4.15.2

6. Assembly source program (query method)

SECOND EQU 30H

TCOUNT EQU 31H

ORG 00H

START: MOV SECOND,#00H

MOV TCOUNT,#00H

MOV TMOD,#01H

MOV TH0,#(65536-50000) / 256

MOV TL0,#(65536-50000) MOD 256

SETB TR0

DISP: MOV A,SECOND

MOV B,#10

DIV AB

MOV DPTR,#TABLE

MOVC A,@A+DPTR

MOV P0,A

MOV A,B

MOVC A,@A+DPTR

MOV P2,A

WAIT: JNB TF0,WAIT

CLR TF0

MOV TH0,#(65536-50000) / 256

MOV TL0,#(65536-50000) MOD 256

INC TCOUNT

MOV A,TCOUNT

CJNE A,#20,NEXT

MOV TCOUNT,#00H

INC SECOND

MOV A,SECOND

CJNE A,#60,NEX

MOV SECOND,#00H

NEX: LJMP DISP

NEXT: LJMP WAIT

TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH

END

7. C language source program (query method)

#include

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsigned char second;

unsigned char tcount;

void main(void)

{

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%6;

TR0=1;

tcount=0;

second=0;

P0=dispcode[second/10];

P2=dispcode[second];

while(1)

{

if(TF0==1)

{

tcount++;

if(tcount==20)

{

tcount=0;

second++;

if(second==60)

{

second=0;

}

P0=dispcode[second/10];

P2=dispcode[second];

}

TF0=0;

TH0=(65536-50000)/256;

TL0=(65536-50000)%6;

}

}

}

8. Assembling source program (interrupt method)

SECOND EQU 30H

TCOUNT EQU 31H

ORG 00H

LJMP START

ORG 0BH

LJMP INT0X

START: MOV SECOND,#00H

MOV A,SECOND

MOV B,#10

DIV AB

MOV DPTR,#TABLE

MOVC A,@A+DPTR

MOV P0,A

MOV A,B

MOVC A,@A+DPTR

MOV P2,A

MOV TCOUNT,#00H

MOV TMOD,#01H

MOV TH0,#(65536-50000) / 256

MOV TL0,#(65536-50000) MOD 256

SETB TR0

SETB ET0

SETB OF

SJMP $

INT0X:

MOV TH0,#(65536-50000) / 256

MOV TL0,#(65536-50000) MOD 256

INC TCOUNT

MOV A,TCOUNT

CJNE A,#20,NEXT

MOV TCOUNT,#00H

INC SECOND

MOV A,SECOND

CJNE A,#60,NEX

MOV SECOND,#00H

NEX: MOV A,SECOND

MOV B,#10

DIV AB

MOV DPTR,#TABLE

MOVC A,@A+DPTR

MOV P0,A

MOV A,B

MOVC A,@A+DPTR

MOV P2,A

NEXT: RETI

TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH

END

9. C language source program (interruption method)

#include

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsigned char second;

unsigned char tcount;

void main(void)

{

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%6;

TR0=1;

ET0=1;

EA=1;

tcount=0;

second=0;

P0=dispcode[second/10];

P2=dispcode[second];

while(1);

}

void t0(void) interrupt 1 using 0

{

tcount++;

if(tcount==20)

{

tcount=0;

second++;

if(second==60)

{

second=0;

}

P0=dispcode[second/10];

P2=dispcode[second];

}

TH0=(65536-50000)/256;

TL0=(65536-50000)%6;

}


Keywords:AT89S51 Reference address:Application of Timer/Counter T0 of AT89S51 Microcontroller

Previous article:AT89S51 microcontroller realizes the design of presetable and reversible 4-bit counter
Next article:Introduction to the internal structure and functions of the AT89S51 microcontroller serial port

Recommended ReadingLatest update time:2024-11-16 09:40

What is the difference between AT89C51 and AT89S51_The difference between AT89C51 and AT89S51
The two microcontrollers AT89C51 and AT89S51 are very close in name. What is the difference between them? This article introduces the differences between AT89C51 and AT89S51 in detail. What is the difference between AT89C51 and AT89S51----AT89C51 introduction AT89C51 is a low-voltage, high-performance CMOS 8-bit mic
[Microcontroller]
What is the difference between AT89C51 and AT89S51_The difference between AT89C51 and AT89S51
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号