With the continuous adjustment of industrial structure, the rapid development of production technology, the continuous improvement of people’s living standards and the gradual popularization of household appliances, the market demand for timing control system is increasing. For example, timing automatic alarm, timing automatic ringing, timing switch oven, timing on-off power equipment and timing start of various electrical belong to timing control system [1]. There are many ways to realize the timing control system. This paper mainly introduces the design and implementation of the intelligent timing control system based on AT89C51 in 80C51 series single chip microcomputer. 80C51 series single chip microcomputer entered the market early, the bus is open, there are many simulation development equipment, the chip and its development price are low, the speed is fast, and the electromagnetic compatibility is good. The intelligent clock control system described in this paper mainly includes clock display, time correction, alarm setting and various equipment timing switch (expandable function) and other functions. The design process of real-time calendar and clock display is synchronized in hardware and software. The hardware part is mainly composed of AT89S52 single chip microcomputer, LED display circuit and time adjustment key circuit. The system displays data through LED, so it has humanized operation and intuitive display effect. Software mainly includes clock program, keyboard program, display program, etc.

In our daily life, we often encounter some things that need timing, such as printing or enlarging photos, which need to be set at a few tenths of a second, washing clothes by washing machine needs to be set at a few minutes to tens of minutes, and electric fan needs to be set at tens of minutes. There are many kinds of timers to complete this timing. The mechanical timer used in household appliances is designed according to the principle of general winding clocks. Although this timer has simple structure, low cost and convenient maintenance, its contacts are frequently contacted and disconnected, which greatly reduces its service life and is not conducive to further full automation. With the rapid development of electronic technology, it is self-evident that electronic timers will gradually replace them.

Application of timer

Timers are often used for delayed automatic shutdown and timing in household appliances. Delayed automatic shutdown can be used for delayed power failure and delayed automatic shutdown power supply of radio, television, tape recorder, hypnotist, door lamp, street lamp, car headlamp, cornering lamp and other electrical appliances. Timing can be used for: camera timing exposure, timing flash, timing amplification, timing speed regulation, timing oven, refrigerator door opening timing alarm, water level timing alarm, delay hypnotist, delay bell, delay electronic lock, touch timing switch, etc. For example, the timer in the air conditioner can automatically cut off the power supply and stop working after working for a period of time. It is used at night in summer. Make sure the time before going to sleep. When the scheduled time is reached, the air conditioner will turn off automatically. Convenient and energy-saving. Timers are not only used in household appliances, but also widely used in industrial, agricultural production and service facilities, even military and so on.

Internal mechanism of 51 single chip microcomputer

51 single chip microcomputer has an 8-bit CPU. At the same time, the CPU contains arithmetic unit, controller and several registers, as shown in figure (Figure 1).

Timer of 51 single chip microcomputer

Counting principle

There are two 16 bit programmable timers / counters in STC MCU. Programmable means that its functions (such as working mode, timing time, measuring range, starting mode, etc.) can be determined and changed by instructions. On timer / counter

In addition to two 16 bit counters, there are also two special function registers (control register and mode register).

From the above structure diagram of timer / counter, we can see that the 16 bit timer / counter is composed of two 8-bit special registers respectively, that is, t0 is composed of Th0 and tl0; T1 consists of Th1 and TL1. The access addresses are 8ah-8dh. Each register can be accessed separately. These registers are used to store timing or counting initial values. In addition, there is an 8-bit timer mode register tmod and an 8-bit timing control register TCON. These registers are connected by internal bus and control logic circuit. Tmod is mainly used to select the working mode of timer; TCON is mainly used to control the start and stop of the timer. In addition, TCON can also save the overflow and interrupt flags of T0 and T1. When the timer operates in counting mode, external events are input through pins t0 (p3.4) and T1 (p3.5).

Timer based on 51 single chip microcomputer

Here, Xiaobian will take you to learn 51 single chip microcomputer timer from scratch. The circuit schematic diagram of timer based on single chip microcomputer is as follows:

Timer of 51 single chip microcomputer

The first thing we learn about MCU is LED flashing, which is done with a delay program. In retrospect, this is not very appropriate. Why? Our main program does the flashing of the light, so we can’t do other things. Can the single chip microcomputer only work like this? Of course not. We can use a timer to realize the flashing function of the light.

Example 1: query method

ORG 0000H

AJMP START

ORG 30H

START:

MOV P1,#0FFH ; Turn off the lights

MOV TMOD,#00000001B ; Timer / counter 0 works in mode 1

MOV TH0,#15H

MOV TL0,#0A0H ; That is 5536

SETB TR0 ; Timer / counter 0 starts running

LOOP:JBC TF0,NEXT ; If TF0 is equal to 1, clear TF0 and turn to next

AJMP LOOP ; Otherwise, jump to loop for operation

NEXT:CPL P1.0

MOV TH0,#15H

MOV TL0,#9FH; Reset the initial value of timing / counter

AJMP LOOP

END AJMP LOOP

END

Type in the program. What do you see? The light is flashing. This is done with a timer. It is no longer the cycle of the main program. Simply analyze the program, why use JBC? TF0 is the overflow flag bit of timer / counter 0. When the timer generates overflow, this bit changes from 0 to 1. Therefore, query this bit to see whether the time has expired. After the bit is 1, the software shall be used to clear the mark bit to 0, so that the bit will change from 0 to 1 when the next timing is due. Therefore, JBC instruction is used. While judging the transfer of 1, the finger bit shall also clear the bit to 0.

The above program can realize the flashing of the light, but the main program can’t do anything else except flashing the light! No, no, we can insert some instructions between loop:… And ajmp loop instructions to do other things, as long as we ensure that the execution time of these instructions is less than the timing time. Can’t we also use some instructions to replace djnz when using software delay program? Yes, but it requires you to accurately calculate the time of the instruction used, and then subtract the corresponding djnz cycle times. It is very inconvenient. Now it only requires that the time of the instruction used is less than the timing time. Obviously, the requirement is low. Of course, this method is still not good, so we often use the following methods to achieve it.

Program 2: implemented with interrupt

ORG 0000H

AJMP START

ORG 000BH ; Interrupt vector address of timer 0

AJMP TIME0 ; Jump to the real timer program

ORG 30H

START:

MOV P1,#0FFH ; Turn off the lights

MOV TMOD,#00000001B ; Timer / counter 0 works in mode 1

MOV TH0,#15H

MOV TL0,#0A0H ; That is 5536

SETB EA ; Open total interrupt allowed

SETB ET0 ; On timing / counter 0 allowed

SETB TR0 ; Timer / counter 0 starts running

LOOP: AJMP LOOP ; In real work, any program can be written here

TIME0: ; Interrupt handler for timer 0

PUSH ACC

PUSH PSW ; Push PSW and ACC into stack protection

CPL P1.0

MOV TH0,#15H

MOV TL0,#0A0H ; Reset timing constant

POP PSW

POP ACC

RETI

END

In the above routine, as soon as the timing time comes, TF0 changes from 0 to 1, it will cause an interrupt, and the CPU will automatically go to 000b to find and execute the program. Since the space for timer interrupt is only 8 bytes, it is obviously not enough to write all interrupt handling programs, so a jump instruction is arranged at 000b to go to the program that actually handles the interrupt, Interrupt programs can be written anywhere and of any length. After entering the timing interrupt, first save some current states. The program only demonstrates saving ACC and PSW. In actual work, the values of units that may change should be pushed into the stack for protection as needed (in this program, it is not necessary to save any values, but only for demonstration here).

After the above two MCU programs were run, we found that the light flickered very fast and could not be distinguished at all. We just felt some shaking of the light visually. Why? We can calculate that the preset number in the timer is 5536, so every 60000 pulses is the timing time. What is the time of 60000 pulses? Our crystal oscillator is 12M, so it is 60000 microseconds, or 60 milliseconds, so the speed is very fast. What should I do if I want to achieve a 1s timing? At the near rate of the crystal oscillator, the longest timing is 65. 536 milliseconds! A routine is given above.

ORG 0000H

AJMP START

ORG 000BH ; Interrupt vector address of timer 0

AJMP TIME0 ; Jump to the real timer program

ORG 30H

START:

MOV P1,#0FFH ; Turn off the lights

MOV 30H,#00H ; Software counter pre clearing 0

MOV TMOD,#00000001B ; Timer / counter 0 works in mode 1

MOV TH0,#3CH

MOV TL0,#0B0H ; That is 15536

SETB EA ; Open total interrupt allowed

SETB ET0 ; On timing / counter 0 allowed

SETB TR0 ; Timer / counter 0 starts running

LOOP: AJMP LOOP ; In real work, any program can be written here

TIME0: ; Interrupt handler for timer 0

PUSH ACC

PUSH PSW ; Push PSW and ACC into stack protection

INC 30H

MOV A,30H

CJNE A,#20,T_ RET ; Has the value in unit 30h reached 20?

T_ L1: CPL P1.0 ; Here we go. Reverse P10

MOV 30H,#0 ; Clear software counter

T_ RET:

MOV TH0,#15H

MOV TL0,#9FH ; Reset timing constant

POP PSW

POP ACC

RETI

END

First analyze it yourself and see how it is realized? The concept of software counter is adopted here. The idea is as follows: first, use the timing / counter 0 as a 50 millisecond timer. When the timing comes, it does not immediately reverse P10, but increase the value in the software counter by 1. If the software counter counts 20, reverse P10 and clear the value in the software counter, otherwise it will return directly, It becomes that P10 is reversed only after 20 timing interrupts, so the timing time is extended to 20 * 50, that is, 1000 milliseconds.

This idea is very useful in engineering. Sometimes we need several timers, but there are only two in 51. What should we do? In fact, as long as these timing times have a certain common divisor, we can use the software timer to realize it. For example, if I want to realize that the light received at port P10 flashes every 1s and the light received at port P11 flashes every 2S, how can I realize it? By the way, we use two counters. When one counts to 20, take the reverse P10 and clear it to zero, as shown above. When the other counts to 40, take the reverse P11 and then clear it to zero. Isn’t that all right? The procedure of this part is as follows

ORG 0000H

AJMP START

ORG 000BH ; Interrupt vector address of timer 0

AJMP TIME0 ; Jump to the real timer program

ORG 30H

START:

MOV P1,#0FFH ; Turn off the lights

MOV 30H,#00H ; Software counter pre clearing 0

MOV TMOD,#00000001B ; Timer / counter 0 works in mode 1

MOV TH0,#3CH

MOV TL0,#0B0H ; That is 15536

SETB EA ; Open total interrupt allowed

SETB ET0 ; On timing / counter 0 allowed

SETB TR0 ; Timer / counter 0 starts running

LOOP: AJMP LOOP ; In real work, any program can be written here

TIME0: ; Interrupt handler for timer 0

PUSH ACC

PUSH PSW ; Push PSW and ACC into stack protection

INC 30H

INC 31H ; Both counters are incremented by 1

MOV A,30H

CJNE A,#20,T_ NEXT ; Has the value in unit 30h reached 20?

T_ L1: CPL P1.0 ; Here we go. Reverse P10

MOV 30H,#0 ; Clear software counter

T_ NEXT:

MOV A,31H

CJNE A,#40,T_ RET ; Has the value in unit 31h reached 40?

T_ L2:

CPL P1.1

MOV 31H,#0 ; Here, reverse P11, clear the counter and return

T_ RET:

MOV TH0,#15H

MOV TL0,#9FH ; Reset timing constant

POP PSW

POP ACC

RETI

END

Leave a Reply

Your email address will not be published. Required fields are marked *