;Keyboard scanning program;This program is more complicated, but if you insist on understanding it, you can still understand it. It is quite classic
;Finally, the key value is output to the digital tube (connected to port P2)
ORG 0000H
START: MOV R0,#00H ;Initialize the program. The delay at the beginning is to make the hardware ready
DJNZ R0,$
LOOP: MOV SP,#60H
CALL KEY
DISPLAY:
MOV A,R4
MOV DPTR,#TABLE ;Define the starting address of the font table
MOVC A,@A+DPTR ;TABLE is the starting address of the table
MOV P2,A
SJMP LOOP
;Subroutine content, the fourth bit of P1 port is the row line, and the upper four bits are the column lines
KEY: PUSH PSW
PUSH ACC
MOV P1,#0F0H ;Set all lines to low level, full scan word-P1.0-P1.3, column as input mode
; This section only verifies that a key is pressed, and cannot determine which row it is
MOV R7,#0FFH ;Set the count constant as a delay
KEY1: DJNZ R7,KEY1
MOV A,P1; read the column value of P1 port
ANL A,#0F0H; determine whether a key is pressed (when a key is pressed, the upper four bits of P1 port are not all 1, and the bottom four bits are still all 0)
; the reason for performing phase OR here is to change the bottom four bits of 0000 to 1111, so as to perform negation in the next step
ORL A,#0FH // This place is not in the original version, it is added again, if it is not added, it is wrong *******
CPL A; after negation, if there is a high level, the key is pressed
JZ EKEY; transfer if the accumulator is 0 (meaning that after negation, all are 0, if a key is pressed, the upper four bits will be 1 after negation), exit
LCALL DEL20ms; if a key is pressed, process
;Scan row by row, 1 row at a time
SKEY: MOV A,#00H
MOV R0,A ;R0 is used as a row counter, with an initial value of 0
MOV R1,A ;R1 is used as a column counter, with an initial value of 0
MOV R2,#0FEH ;R2 is used as a scan temporary word, with an initial value of 1111 1110 (the fourth bit is used as a row scan word)
SKEY2: MOV A,R2
MOV P1,A ;Output row scan word, 1111 1110
NOP
NOP
NOP ;3 NOP operations make the output of port P1 stable
MOV A,P1 ;Read column value (same as the beginning)
MOV R1,A ;Temporarily store column value (the first time is **** 1110, that is, one of the upper four bits "may" be 0)
ANL A,#0F0H ;Get the upper four bits,
ORL A,#0FH ;Set all fourth bits to 1
CPL A
BIAOZHI:JNZ SKEY3 ;If the accumulator is not 0, transfer the instruction (which means that it is determined that the key is in this row), and transfer to process
INC R0 ;If the key is not in this row, the row counter is increased by 1
SETB C ;The carry flag is increased by 1, so that the low bit 0 at the beginning will not appear in the low position when shifting left (after one cycle)
MOV A,R2
RLC A ;Shift left 1 bit with carry (form the scan word of the next row, and scan again)
MOV R2,A
MOV A,R0
;Compare the row counter R0 after adding 1 with the total number of scans (4 comparisons)
CJNE A,#04H,SKEY2 ;(Has the scan been completed?) There is also an error in this place in the book. The book says: SKEY1
AJMP EKEY ;If not, exit
;After a key is pressed, the row is scanned, this is the confirmed row
SKEY3: MOV A,R1 ;
JNB ACC.4,SKEY5 ;Direct addressing bit is 0, how to transfer instructions
JNB ACC.5,SKEY6
JNB ACC.6,SKEY7
JNB ACC.7,SKEY8
AJMP EKEY //I feel that this command is useless
SKEY5: MOV A,#00H ; save column number 0
MOV R3,A
AJMP DKEY
SKEY6: MOV A,#01H ; store 1 column number
MOV R3,A
AJMP DKEY
SKEY7: MOV A,#02H ; store column number 2
MOV R3,A
AJMP DKEY
SKEY8: MOV A,#03H ; store column number 3
MOV R3,A
AJMP DKEY
;Take out the specific row number, add the column number, and finally confirm the key number
DKEY: //MOV R4,#00H
MOV A,R0
MOV B,#04H
MUL AB ;Let the row number * 4, and put the fourth digit in A (there are 4 rows in total, and the multiplication must be <16, that is, only the fourth digit has a value)
ADD A,R3 ;Add the row number and column number to finally confirm the specific number of any keyMOV
R4,A
EKEY: POP ACC
POP PSW
RET ;Key scan processing function
DEL20ms:
MOV R7,#2
DL2: MOV R6,#18
DL1: MOV R5,#255
DJNZ R5,$
DJNZ R6,DL1
DJNZ R7,DL2
RET
;This is the digital table of common cathode digital tube
TABLE: DB 3FH ;0
DB 06H ;1
DB 5BH ;2
DB 4FH ;3
DB 66H ;4
DB 6DH ;5
DB 7DH ;6
DB 27H ;7
DB 7FH ;8
DB 6FH ;9
DB 77H
DB 7CH
DB 39H
DB 5EH
DB 79H
DB 71H
END
Previous article:Course Design: Matrix Keyboard Scanning (Assembly)
Next article:Use P1 port as input and output port
Recommended ReadingLatest update time:2024-11-15 17:58
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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)
- 【Repost】The definition and difference between passive and active signals of sensors
- TI C64x+ DSP CACHE consistency analysis and maintenance
- Some basic knowledge about SPI-Flash
- Is your phone ready for 5G?
- [MSP430] Practical ADC use, internal temperature measurement use
- CC2640R2F ADC
- [RVB2601 Creative Application Development] Practice 5-FOTA upgrade display download progress
- PCB design information sharing
- Help! I'm looking for an ultra-low power MCU. I hope you can recommend one.
- The output voltage of this power chip TPS62742 is only 3.21V, is it normal?