There are usually two requirements for counting at work: first, display the count value, and second, interrupt and alarm when the count value reaches a certain level. The first type is like various counters and odometers, and the second type is like the counting on the production line mentioned in the previous example. Let's look at the first type first. Our hardware is connected like this: the oscillator composed of 324 is connected to the external pin T1 of the timer/counter 1. We use this to do a counting experiment. To display the count value, of course, it is best to use a digital tube, but we haven't talked about this part yet. In order to avoid complicating the problem, we use the 8 LEDs of the P1 port to display the counted data.
The procedure is as follows:
ORG 0000H
AJMP START
ORG 30H
START:
MOV SP,#5FH
MOV TMOD,#01000000B; Timer/Counter 1 is used for counting, no need to set all 0s
SETB TR1 ;Start counter 1 to start running.
LOOP: MOV A,TL0
MOV P1,A
AJMP LOOP
END
Connect the output of 324 to T1 with a wire (there are pads on the printed board) and run this program. Pay attention to placing the board in the correct position (LM324 is placed on the left hand side, and the LEDs are arranged from high to low). What do you see? As the LED connected to 324 flashes, the 8 LEDs of the microcontroller are also changing. Pay attention to observe whether they are in binary:
00000000
00000001
00000010
00000011
.
.
.
Is this order changing? That's right, this is the data in TL0.
Procedure 2:
ORG 0000H
AJMP START
ORG 001BH
AJMP TIMER1 ; Timer 1 interrupt processing
ORG 30H
START: MOV SP,#5FH
MOV TMOD,#01010000B; Timer/Counter 1 is used for counting, mode 1, 0 is not used, all bits are set to 0
MOV TH1,#0FFH
MOV TL1,#0FAH; preset value, requiring every 6 pulses counted to be an event
SETB EA
SETB ET1; Enable general interrupt and timer 1 interrupt
SETB TR1 ;Start counter 1 to start running.
AJMP$
TIMER1:
PUSH ACC
PUSH PSW
CPL P1.0; when the count value reaches, P1.0 is inverted
MOV TH1,#0FFH
MOV TL1,#0FAH ; Reset the initial value of the count
POP PSW
POP ACC
RETI
END
The program above does a very simple job, which is to invert P1.0 after every 6 pulses. Therefore, the result of the experiment should be: if the LED connected to LM324 turns on and off 6 times, the LED connected to P1.0 turns on or off once. This is actually the second application of the counter we mentioned above.