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 count is increased by 1. When the second count reaches 60, it automatically starts from 0. Now I will start to introduce how to make this microcontroller timer. The hardware circuit is shown in the figure below
2. Circuit diagram
Figure 4.15.1
3. Hardware connections on the system board
(1. Connect the P0.0/AD0-P0.7/AD7 ports in the "MCU System" area to any a-h ports in the "Four-way Static Digital Display Module" area using an 8-core cable; requirements: P0.0/AD0 corresponds to a, P0.1/AD1 corresponds to b, ..., P0.7/AD7 corresponds to h.
(2. Use an 8-core cable to connect the P2.0/A8-P2.7/A15 ports in the "MCU System" area to any a-h ports in the "Four-way Static Digital Display Module" area; 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. This can be done by setting the special function register TMOD. The timer/counter works by setting the TCON special function register through software.
Now we choose the 16-bit timing mode. For T0, the maximum timing is only 65536us, that is, 65.536ms, which cannot reach the 1-second timing we need. Therefore, we must use software to handle this problem. Suppose we take the maximum timing of T0 as 50ms, that is, to achieve 1-second timing, 20 times of 50ms timing are required. For these 20 times, we can use software methods to count.
Therefore, we set TMOD = 00000001B, that is, TMOD = 01H
Next, we need to load the preset initial values into TH0 and TL0 of T0 timer/counter. The following formula can be used to calculate
TH0 = (216 - 50000) / 256
TL0 = (216 - 50000) MOD 256
When T0 is working, how do we know that the 50ms timing time has arrived? This time we detect the TF0 flag bit in the TCON special function register. If TF0=1, it means that the timing time has arrived.
5. Flowchart
Figure 4.15.2
6. Assemble the 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 《AT89X51.H》
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;
}
}
}
1. Assemble 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 EA
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:RT
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END
2. C language source program (interrupt method)
#include 《AT89X51.H》
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;
}
Previous article:Keil5 installation tutorial (including C51 and MDK coexistence) WIN10 tested and available
Next article:Design of C51 single chip microcomputer in motor speed measurement simulation system
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Pingtouge RVB2601 board-IIC bus test
- EEWORLD University ---- Deep Learning Course (NYU, Spring 2020) Yann Lecun
- Great God Tesla ~AC~
- [TI recommended course] #Engineer It series course #Lesson 1 Loop bandwidth in phase-locked loop applications
- EEWORLD University ---- RS-485 Overview
- PicoVGA (1) VGA/TV driver library for Pico
- How to set up the serial port of 51 microcontroller to communicate with PC
- Step-by-step considerations for designing wide-bandwidth, multichannel systems
- DSP Digital Image Design Report
- Digital Integrated Circuits—Circuits, Systems, and Design (Second Edition)