Experiment 8. Multi-way switch status indication
[Copy link]
1. Experimental tasks As shown in the figure below, P1.0 - P1.3 of the microcontroller are connected to four light-emitting diodes L1 - L4 , and P0.4 - P0.7 are connected to four switches K1 - K4 . The programming reflects the state of the switch on the light-emitting diode. (When the switch is closed, the corresponding light is on, and when the switch is open, the corresponding light is off). 2. Circuit diagram Figure 4.3.1 3. Hardware connection on the system board 1. Remove the four short-circuit caps from the P0.0 to P0.3 pins. 2. Use a double-ended connection line to connect the common line of the four keys at the bottom to the negative pole of the power supply . 3. The four keys at the bottom are effective. 4. Programming content (1. Switch status detection For switch status detection, relative to the single-chip microcomputer, it is an input relationship. We can detect the status of each switch in turn, and let the corresponding light-emitting diode indicate according to the status of each switch. This can be accomplished by using JB P1.X , REL or JNB P1.X , REL instructions. We can also detect the status of four switches at a time and then let it indicate. We can use MOV A , P1 instruction to read all the status of P1 port at once, and then take the status of the upper 4 bits to indicate. (2. Output control According to the state of the switch, indicated by the LEDs L1 - L4 , we can use the SETB P1.X and CLR P1.X instructions to complete it, or we can use the MOV P1 , # 1111XXXXB method to indicate it once. 5. Flowchart Figure 4.3.2 6. Method 1 (Assembly source program) ORG 00H START: JB P0.4,NEXT1 ; If the direct bit is 1, transfer to NEXT1, otherwise, execute below CLR P1.0 SJMP NEX1 NEXT1: SETB P1.0 NEX1: JB P0.5,NEXT2 CLR P1.1 SJMP NEX2 NEXT2: SETB P1.1 NEX2: JB P0.6,NEXT3 CLR P1.2 SJMP NEX3 NEXT3: SETB P1.2 NEX3: JB P0.7,NEXT4 CLR P1.3 SJMP NEX4 NEXT4: SETB P1.3 NEX4: SJMP START END 7. Method 2 (Assembly source program) ORG 00H START: MOV A,P1 ANL A,#0F0H; The immediate number 0F0H is ANDed with the accumulator A,The result is stored in the accumulator. R R; A right ring shift R R R R R R XOR A,#0F0H; XOR the immediate value with A MOV P1,A SJMP START END
|