Microcontroller keyboard scanning program (assembly)

Publisher:buzzedyLatest update time:2016-09-13 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
; This is relatively difficult. If you don’t understand it, you can (return to the log directory, or the upper right corner of this log, you can also see it) read the previous article (Course Design: Matrix Keyboard, which is relatively simple)

;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

Keywords:MCU Reference address:Microcontroller keyboard scanning program (assembly)

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

The construction of intelligent industrial sensor nodes requires sensors to share tasks with MCUs
As an important part of the perception layer in industrial control, industrial sensors not only improve industrial processes and provide asset monitoring and protection, but also make the visualization of industrial control clearer. In today's trend of industrial intelligent manufacturing, in order to create intelli
[Embedded]
The driving principle and design of buzzer in single chip microcomputer
A buzzer is an electronic sounder with an integrated structure. This article introduces how to use a single-chip microcomputer to drive a buzzer. It is widely used as a sound-generating device in electronic products such as computers, printers, copiers, alarms, and telephones.   Buzzers are mainly divided into two typ
[Microcontroller]
51 MCU 4-bit digital tube electronic clock source program
/****************************************************** *************************** Title: Timer interrupt 24-hour clock program Effect: Digital tube displays 24-hour clock (with time adjustment function) Note: Use 12M crystal oscillator, key1=P3^7; key2=P3^6; key3=P3^5; key4=P3^4; Digital tube position s
[Microcontroller]
MCU serial port sending and receiving program
include "config.h" /******************************Baud rate of serial port 1********************************/ //T1 is used as baud rate generator //When the baud rate is doubled  #define BAUD_57600 256 - (OSC_FREQ/192L)/57600L // 254 FF #define BAUD_28800 256 - (OSC_FREQ/192L)/28800L // 254 FE #define BAUD_19200 256
[Microcontroller]
PIC16-bit microcontroller CAN (3) Serial port detailed explanation (232)
--------------------------------- Pin definition of serial port: 9-pin signal direction from abbreviation description 1 Modem CD Carrier detection 2 Modem RXD Receive data 3 PC TXD Send data 4 PC DTR Data terminal ready 5  GND Signal ground 6 Modem DSR Communication device ready 7 PC RTS Request to send 8 Modem CT
[Microcontroller]
PIC16-bit microcontroller CAN (3) Serial port detailed explanation (232)
AVR MCU ADC conversion program
//Benchmark definition #defineAD_ref_vref0x00 //reference Vref #defineAD_ref_avcc0x40 //reference AVCC #defineAD_ref_vref_in0xc0 //reference internal Vref #defineAD_refAD_ref_vref //Constant definition //Single-ended channel, no amplification #define AD_SE_ADC0 0x00//ADC0 #define AD_SE_ADC1 0x01//ADC1
[Microcontroller]
51 MCU Study Notes——10. Interrupt Mechanism
Interrupt The interrupt system of the microcontroller is the most basic and important part. The blogger encountered many pitfalls when learning this, so he wrote this note to organize his thoughts. The interruption involves: Interrupt sources: The C51 microcontroller has 5 interrupt sources and the C52 microcontro
[Microcontroller]
51 MCU Study Notes——10. Interrupt Mechanism
AVR microcontroller water light program
/******************************************************************************/ /*【Written on】: 2011.07.07 * 【Author】: Yanling Electronics * 【Version】: V1.0 * 【Compilation environment】: ICCAVR   * 【Function】: Flowing light experiment * 【Crystal】: 8M                 * 【Chip】: ATMEGA16A          * 【Hardware connection】
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号