Microcontroller Classic Experiment 12: 00-59 Seconds Timer (Using Software Delay)

1. Experiment Task

As shown in the figure below, two common cathode seven-segment displays are connected to the P0 and P2 ports of the AT89S51 microcontroller, where the P0 port drives the tens place of the seconds display, and the P2 port drives the units place of the seconds display.

2. Circuit Diagram

Microcontroller Classic Experiment 12: 00-59 Seconds Timer (Using Software Delay)

Figure 4.11.1

3. Hardware Connections on the System Board

(1. Connect the P0.0/AD0 to P0.7/AD7 ports in the “Microcontroller System” area to any of the a-h ports in the “Four-Way Static Digital Display Module” area using an 8-core ribbon cable; requirements: P0.0/AD0 corresponds to a, P0.1/AD1 corresponds to b, …, P0.7/AD7 corresponds to h.

(2. Connect the P2.0/A8 to P2.7/A15 ports in the “Microcontroller System” area to any of the a-h ports in the “Four-Way Static Digital Display Module” area using an 8-core ribbon cable; requirements: P2.0/A8 corresponds to a, P2.1/A9 corresponds to b, …, P2.7/A15 corresponds to h.

4. Program Design Content

(1. In the design process, we use a memory unit as the second counting unit. When a second passes, the second counting unit increments by 1. When the second count reaches 60, it automatically returns to 0 and restarts the second count.

(2. The data in the second counting unit needs to be separated into tens and units, using division by 10 and modulo 10.

(3. The display on the seven-segment display is still achieved through a lookup table.

(4. The generation of one second is achieved here using a software precise delay method, with the precise calculation yielding that 1 second is equivalent to 1.002 seconds.

DELY1S: MOV R5,#100
D2: MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
DJNZ R5,D2
RET

5. Program Flowchart

Microcontroller Classic Experiment 12: 00-59 Seconds Timer (Using Software Delay)

6. Assembly Source Code

Second EQU 30H

ORG 0

START: MOV Second,#00H

NEXT: 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

LCALL DELY1S

INC Second

MOV A,Second

CJNE A,#60,NEXT

LJMP START

DELY1S: MOV R5,#100

D2: MOV R6,#20

D1: MOV R7,#248

DJNZ R7,$

DJNZ R6,D1

DJNZ R5,D2

RET

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

END

7. C Language Source Code

#include <AT89X51.H>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Second;
void delay1s(void)
{
unsigned char i,j,k;
for(k=100;k>0;k--)
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main(void)
{
Second=0;
P0=table[Second/10];
P2=table[Second%10];
while(1)
{
delay1s();
Second++;
if(Second==60)
{
Second=0;
}
P0=table[Second/10];
P2=table[Second%10];
}
}

Microcontroller Classic Experiment 12: 00-59 Seconds Timer (Using Software Delay)Microcontroller Classic Experiment 12: 00-59 Seconds Timer (Using Software Delay)

Leave a Comment