2 8-bit modes. Mode 3 is only applicable to timer 0. If timer 1 is set to mode 3, timer 1 will be in the off state.
When T0 is in mode 3, THo and TL0 are divided into 2 independent 8-bit counters . Among them, TL0 can be used as both a timer and a counter, and uses all the control bits of the original T0 and its timer return to zero flag and interrupt source. TH0 can only be used as a timer, and uses the control bit TRl, return to zero flag TFl and interrupt source of T1, see the figure below.
Normally, T0 does not run in working mode 3. It can only be used when T1 is in working mode 2 and does not require interruption. At this time, T1 is often used as a serial port baud rate generator (see 1.4), TH0 is used as a timer, and TL0 is used as a timer or counter. Therefore, mode 3 is specially provided for applications where the microcontroller has an independent timer/counter, a timer, and a serial port baud rate generator. At this time, timer 1 can be used in working mode 2, and timer 0 can be used in working mode 3.
At this time, T1 is often used as a serial port baud rate generator, TH0 is used as a timer, and TL0 is used as a timer or counter. Therefore, mode 3 is specially provided for the application of the microcontroller with an independent timer/counter, a timer, and a serial port baud rate generator. At this time, timer 1 can be used in working mode 2, and timer 0 can be used in working mode 3.
Understanding the content
Timer/Counter and Interrupt Comprehensive Application Example
Example: Clock timing program design.
The so-called clock timing is the timing in seconds, minutes and hours. This program can be regarded as a typical representative of timer/counter and interrupt application, which can be regarded as a review of these two parts.
⑴ Basic method of realizing clock timing display with MCS-51 single chip microcomputer
① First, calculate the initial value of the count
The key issue of clock timing is the generation of seconds, because seconds are the smallest clock unit. However, when using the timer/counter of MMCS-51 for timing, even in working mode 1, its maximum timing time can only reach 131 milliseconds, which is far from 1 second. Therefore, we implement the second timing by combining hardware timing and software counting, that is, setting the timing time of the timer to 125 milliseconds, so that when the count overflows 8 times, 1 second can be obtained, and the 8-time count can be implemented by software methods.
To get 125ms timing, we can use timer/counter 0 and work in mode 1. When the microcontroller is set to a 6MHz crystal oscillator and the initial value of the design number is X, the following equation is obtained:
(216-X)×2us=125000us
The calculated initial count value X = 3036,
The binary representation is 0000101111001101 and the hexadecimal representation is 0BCDH.
② The timer timing is completed in an interrupt mode, so that the overflow times (125 milliseconds each time) can be accumulated through the interrupt service program. When 8 times are counted, the second timing is obtained.
③ Realize timing from seconds to minutes and from minutes to hours by accumulating and comparing values in the program
④ Set the clock display and display buffer
Assume that the clock time is displayed on a six-digit LED digital tube (LED5 to LED0) (hour, minute, and second each occupy two digits). To this end, a display buffer must be set in the internal RAM, with a total of 6 units (79H to 7EH), and the corresponding relationship with the digital tube is:
LED5→7EH, LED4→7DH, LED3→7CH, LED2→7BH, LED1→7AH, LED0→79H.
That is, the display buffer stores the values of hours, minutes, and seconds from left to right.
⑤ Assume that the LED display program is SMXS and can be called
⑵ Procedure flow
① Main program (MAIN)
The main function of the main program is to initialize the timer/counter and then wait for the 125ms timing interrupt by repeatedly calling the display subroutine.
② Interrupt service routine (PIT0)
The main function of the interrupt service program is to perform timing operations. The program starts by determining whether the count overflows for 8 times. If it is less than 8 times, it means that the minimum timing unit second has not been reached, and the interrupt returns; if it is full 8 times, it means that the minimum timing unit second has been reached, and the program continues to execute and perform timing operations.
③ Add 1 subroutine (DAAD1)
The add 1 subroutine is used to complete the operation of adding 1 to the second, minute and hour. There are three places in the interrupt service program to call this subroutine when adding 1 to the second, minute and hour. The add 1 operation includes the following three contents:
Composite number
Since each LED display corresponds to an 8-bit buffer unit, the time value represented by the two-bit BCD code occupies one buffer unit each, and only occupies the lower 4 bits. Therefore, before adding 1, the values stored in the two buffer units need to be combined to form a byte, and then the addition operation can be performed. This is where the word "combination" comes from.
Decimal Adjustment
Add 1 and make a decimal adjustment
Fraction
The time value after adding 1 is split into two bytes and sent back to their respective buffer units.
⑶ Program List
The entire reference program list is as follows:
ORG 8000H
START:AJMP MAIN
ORG 800BH
AJMP PITO
ORG 8100H
MAIN: MOV SP, #60H; establish the stack area
MOV R0, #79H ; Display buffer first address
MOV R7, #06H; Display digits
ML1: MOV @R0, #00H; clear the display buffer unit to 0
INC R0
DJNZ R7, ML1
MOV TMOD, #01H; Timer 0, working mode 1
MOV TL0, #0CDH; load the initial value of the counter
MOV TH0, #0BH
SETB 8CH; TR0 is set to 1, timing starts
SETB AFH; EA is set to 1, interrupts are always enabled
SETB A9H; ET0 is set to 1, timer 0 interrupt is enabled
MOV 30H, #08H; the required number of count overflows, i.e. the number of loops
ML0: LCALL SMXS ; Call display subroutine
SJMP ML0
PITO: PUSH PSW; interrupt service routine, context protection
PUSH ACC
SETB PSW.3; RS1RS0=01, select 1 group of general registers
MOV TL0, #0CDH; counter reload
MOV TH0, #0BH
MOV A, 30H; the number of cycles decreases by 1
DEC A
MOV 30H, A
JNZ RET0; less than 8 times, go to RET0 and return
MOV 30H, #08H; after 8 times, start timing operation
MOV R0, #7AH; Second display buffer unit address
ACALL DAAD1; add 1 per second
MOV A, R2; add 1 and the seconds value is in R2
XRL A, #60H; Check whether 60 seconds have passed
JNZ RET0 ; if not reached, go to RETO to return
ACALL CLR0; clear the display buffer unit to 0 after 60 seconds
MOV R0, #7CH; Display buffer unit address
ACALL DAAD1 ; points plus 1
MOV A, R2
XRL A, #60H; Determine whether it has reached 60 points
JNZR
ACALL CLR0; At 60 minutes, the minute display buffer unit is cleared to 0
MOV R0, #7EH ; displays the buffer unit address
ACALL DAAD1 ; add 1
MOV A,R2]
XRL A, #24H; Determine whether it is 24 hours
JNZR
ACALL CLR0; At 24 o'clock, the time display buffer unit is cleared to 0
RET0:POP ACC; scene recovery
POP PSW
RETI ;Interrupt return
DAAD1:MOV A, @R0; add 1 subroutine, send the tens digit to A
DEC R0
SWAP A; the tens digit occupies the upper 4 digits
ORL A, @R0; the unit digit occupies the lower 4 digits
ADD A, #01H; add 1
DA A ; Decimal adjustment
MOV R2, A; store the full value in R2 temporarily
ANL A, #0FH; shield the tens digit and extract the ones digit
MOV @R0, A ; Send the unit value to the display buffer unit
MOV A,R2
INC R0
ANL A, #F0H; shield the unit digit and extract the ten digit
SWAP A; make the tens digit occupy the lower 4 digits
MOV @R0, A ; Send the tens digit to the display buffer unit
RET ; Return
CLR0:CLR A; Clear buffer unit subroutine
MOV @R0, A ; clear the tens digit buffer unit to 0
DEC R0
MOV @R0, A ; clear the single-digit buffer unit to 0
Previous article:Programming of MCS-51 single chip microcomputer driving stepper motor
Next article:Write a single-chip counter program to count pulses
Recommended ReadingLatest update time:2024-11-15 17:51
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- In a single-chip computer, writing 1 clears it to 0, and writing 0 clears it to 0. What is the difference?
- How to monitor battery voltage with Arduino
- Image Algorithm Transplantation to DSP and Its Optimization Steps
- To advance in technology to a certain level, you just need to read more datasheets!
- Prize-winning live broadcast: Keysight’s Metaverse Test Series Seminar: Challenges in VR/AR Digital Interface Testing in Progress!
- 51 STM32 reference study manuals, one-click download without points!
- Microwave and RF Applications
- With the advent of 5G, what should we pay attention to when designing mobile phone antennas?
- 220V motor control board, discharges very slowly after power failure, and can still shock people after a long time
- Has anyone seen this chip?