1. Experimental Task Each time the switch SP1 is pressed, the count value increases by 1, and its binary count value is displayed through P1.0 to P1.3 of the P1 port of the AT89S51 microcontroller. 2. Circuit diagram 3. Hardware connection on the system board We only need to remove 7 of the 8 short-circuit caps in the 4*4 button, because we only use one button. Then use a double-ended connecting wire to connect the pin of this button to the negative node. The LED part does not need to be changed. Just like our previous running light experiment 4. Programming method (1. In fact, as a key, from not being pressed to being pressed and released is a complete process. That is to say, when we press a key, we always hope that a certain command will be executed only once, and in the process of pressing the key, no interference should come in, because, in the process of pressing, once there is interference, it may cause a false trigger process, which is not what we want. Therefore, when the key is pressed, Figure 4.8.2 should filter out the interference signals on our hands and the mechanical contact of the key. In general, we can use capacitors to filter out these interference signals, but in fact, it will increase the hardware cost and the volume of the hardware circuit, which is what we don’t want. There must be a way to solve this problem, so we can use software filtering to remove these interference signals. In general, when a key is pressed, there is always a certain interference signal at the moment of pressing, and it basically enters a stable state after being pressed. The signal diagram of the whole process of a key from being pressed to being released is shown in the figure above: As can be seen from the figure, when we design the program, from the time the key is recognized and pressed, After that, delay more than 5ms to avoid the interference signal area. We will detect again to see if the key has been pressed. If it has been pressed, the output will be low level. If it is high level, it proves that it was a false trigger caused by the interference signal. The CPU will think it is a false trigger signal and abandon the key recognition process. This improves the reliability of the system. Since it is required that the command is executed once each time the key is pressed, and the command will be executed again when it is pressed again next time, we can execute the command after the key is recognized, so there must be a process of waiting for the key to be released. Obviously, the release process is to restore it to a high level state. (1. For the key recognition instructions, we still choose the following instructions JB BIT, REL instruction is used to detect whether BIT is high level, if BIT = 1, the program will turn to REL to execute the program, otherwise it will continue to execute the program. Or JNB BIT, REL instruction is used to detect whether BIT is low level, if BIT = 0, the program will turn to REL to execute the program, otherwise it will continue to execute the program. (2. However, the block diagram of the key recognition process in the program design process is shown in the following figure: 5. Flowchart 6. Assembly source program
ORG 0 START: MOV R1,#00H ; Initialize R7 to 0, indicating that counting starts from 0 MOV A,R1 ; CPL A ; Take the inverse instruction MOV P1,A ; Send to P1 port for display by LED REL: JNB P3.7,REL ; Determine whether SP1 is pressed LCALL DELAY10MS ; If pressed, delay about 10ms JNB P3.7,REL ; Then determine whether SP1 is really pressed INC R7 ; If really pressed, perform key processing, MOV A,R7 ; Add 1 to the count content, and send to P1 port for display by CPL A ; LED MOV P1,A ; JNB P3.7,$ ; Wait for SP1 to release SJMP REL ; Continue to scan the K1 key DELAY10MS: MOV R6,#20 ; Delay 10ms Subroutine L1: MOV R7,#248 DJNZ R7,$ DJNZ R6,L1 RET END
|