Lesson 25: Microcontroller keyboard interface programming

Publisher:asd999dddLatest update time:2020-07-03 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The keyboard is a switch matrix composed of several buttons. It is the most commonly used input device in the single-chip microcomputer system. Users can input instructions, addresses and data to the computer through the keyboard. Generally, a non-encoded keyboard is used in the single-chip microcomputer system. The non-encoded keyboard uses software to identify the closed keys on the keyboard. It has the characteristics of simple structure and flexible use, so it is widely used in single-chip microcomputer systems.


Push button switch jitter problem

The buttons that make up the keyboard are of two types: contact and non-contact. The ones used in microcontrollers are generally composed of mechanical contacts.

figure 1

figure 2

When the switch S is not pressed, the P1.0 input is high level. After S is closed, the P1.0 input is low level. Since the button is a mechanical contact, there will be jitter when the mechanical contact is opened and closed. The waveform of the P1.0 input is shown in Figure 2. This jitter is imperceptible to humans, but it is completely sensible to computers, because the processing speed of computers is in microseconds, and the mechanical jitter time is at least in milliseconds. For computers, this is a "long" time. Earlier, we talked about a problem when we talked about interrupts, that is, the button sometimes works and sometimes doesn't. In fact, this is the reason. You only pressed the button once, but the computer has executed multiple interrupt processes. If the number of executions is exactly an odd number, then the result is as you expected. If the number of executions is an even number, then it is wrong.


In order to enable the CPU to correctly read the state of P1 port and respond only once to each button press, it is necessary to consider how to remove jitter. There are two common ways to remove jitter: hardware method and software method. The software method is commonly used in single-chip microcomputers, so we will not introduce the hardware method. The software method is actually very simple. After the single-chip microcomputer obtains the information that P1.0 port is low, it does not immediately determine that S1 has been pressed, but delays 10 milliseconds or longer to detect P1.0 port again. If it is still low, it means that S1 is indeed pressed. This actually avoids the jitter time when the button is pressed. After detecting that the button is released (P1.0 is high), it delays another 5-10 milliseconds to eliminate the jitter of the trailing edge, and then processes the key value. However, in general, we often do not process the trailing edge of the button release. Practice has proved that it can also meet certain requirements. Of course, in actual applications, the requirements for buttons are also very different, and the processing program should be compiled according to different needs, but the above is the principle of eliminating key jitter.


Connection between keyboard and microcontroller


image 3


Figure 4

1. Connect through the 1/0 port. Connect one end of each button to the I/O port of the microcontroller and the other end to the ground. This is the simplest way. Figure 3 shows the connection method of the buttons on the experimental board. The four buttons are connected to P3.2, P3.3, P3.4 and P3.5 respectively. For this kind of key, each program can use the method of continuous query. The function is to detect whether there is a key closed. If there is a key closed, remove the key jitter, determine the key number and transfer to the corresponding key processing. A routine is given below. Its function is very simple. The four keys are defined as follows:

P3.2: Start, press this button and the lights will start to flow (from top to bottom)

P3.3: Stop. Press this button to stop the flow and all lights will be off.

P3.4: Up, press this button and the light will flow from top to bottom

P3.5: Down, press this button and the light will flow from bottom to top

UpDown EQU 00H ; Up and down flag

StartEnd EQU 01H ; Start and stop flag

LAMPCODE EQU 21H ;Store the flow data code

ORG 0000H

AJMP MAIN

ORG 30H

MAIN:

MOV SP,#5FH

MOV P1,#0FFH

CLR UpDown ; Starts in the upward state

CLR StartEnd ;Start in stopped state

MOV LAMPCODE,#0FEH ; Single lamp flow code

LOOP:

ACALL KEY ;Call keyboard program

JNB F0,LNEXT ;If no key is pressed, continue

ACALL KEYPROC ; otherwise call the keyboard handler

LNEXT:

ACALL LAMP; Call the lamp display program

AJMP LOOP; Repeated loop, the main program ends here

DELAY:

MOV R7,#100

D1: MOV R6,#100

DJNZ R6,$

DJNZ R7,D1

RET

;----------------------------------------Delay program, called during keyboard processing

KEYPROC:

MOV A,B ; Get the key value from register B

JB ACC.2,KeyStart; Analyze the key code. If a bit is pressed, the bit is 1 (because it has been inverted in the keyboard program)

JB ACC.3,KeyOver

JB ACC.4,KeyUp

JB ACC.5,KeyDown

AJMP KEY_RET

KeyStart:

SETB StartEnd ; Processing after the first key is pressed

AJMP KEY_RET

KeyOver:

CLR StartEnd ; Processing after the second key is pressed

AJMP KEY_RET

KeyUp: SETB UpDown ; Processing after the third key is pressed

AJMP KEY_RET

KeyDown:

CLR UpDown ; Processing after the fourth key is pressed

KEY_RET:RET

KEY:

CLR F0; Clear F0, indicating that no key is pressed.

ORL P3,#00111100B ; Set the four bits connected to the key of port P3 to 1

MOV A,P3; Get the value of P3

ORL A,#11000011B ; Set the remaining 4 positions to 1

CPL A ; Negate

JZ K_RET ; if it is 0, no key is pressed

ACALL DELAY; otherwise delay to remove key jitter

ORL P3,#00111100B

MOV A,P3

ORL A,#11000011B

CPL A

JZ K_RET

MOV B,A ; If a key is pressed, store the key value in B

SETB F0 ; Set the key pressed flag

K_RET:

ORL P3,#00111100B ; This loop waits for the key to be released

MOV A,P3

ORL A,#11000011B

CPL A

JZ K_RET1 ; Return from the keyboard processing program until the read data is inverted and becomes 0, indicating that the key has been released

AJMP K_RET

K_RET1:

RET

D500MS: ;Delay time of running light

PUSH PSW

SETB RS0

MOV R7,#200

D51: MOV R6,#250

D52: NOP

NOP

NOP

NOP

DJNZ R6,D52

DJNZ R7,D51

POP PSW

RET

LAMP:

JB StartEnd, LampStart ; If StartEnd=1, start

MOV P1,#0FFH

AJMP LAMPRET ; otherwise close all displays and return

LampStart:

JB UpDown, LAMPUP; If UpDown=1, flow upward

MOV A,LAMPCODE

RL A; Actually it is just a left shift

MOV LAMPCODE,A

MOV P1,A

LCALL D500MS

AJMP LAMPRET

LAMPUP:

MOV A,LAMPCODE

RR A; Flowing downward is actually moving right

MOV LAMPCODE,A

MOV P1,A

LCALL D500MS

LAMPRET:

RET

END


The above program function is very simple, but it demonstrates the basic idea of ​​a single-chip keyboard processing program. The program itself is very simple and not very practical. There are many factors to consider in actual work. For example, the main loop calls the light loop program every time, which will cause the button to respond "sluggishly". If you keep pressing the key, the light will not flow until you release it, and so on. You can consider these issues carefully and think about whether there is a good solution.


2. Use interrupt mode: as shown in Figure 4. Each button is connected to an AND gate. When any button is pressed, the AND gate output is low, causing the microcontroller to interrupt. Its advantage is that there is no need to continuously query in the main program. If a key is pressed, the microcontroller will do the corresponding processing.

Keywords:MCU Reference address:Lesson 25: Microcontroller keyboard interface programming

Previous article:Lesson 24: Dynamic scanning display interface circuit and program
Next article:Lesson 26: Matrix keyboard interface technology and program design

Recommended ReadingLatest update time:2024-11-16 14:43

AVR MCU on-chip AD digital to analog converter program
/*AD converter program in AVR chip. This test program sets the internal AD converter to continuous conversion mode. For more modes, see pages 191-207 of the manual*/ #include iom16v.h #define uchar unsigned char #define uint unsigned int #define set_bit(a,b) a|=(1 b) #define clr_bit(a,b) a&=~(1 b) #define get_bit(a
[Microcontroller]
51 single chip microcomputer DC motor experiment
1. Basic model of DC motor The figure below is a simple two-pole DC motor model. Its fixed part (stator) is two stationary magnetic poles N and S; the rotating part (rotor) is the armature coil abcd, and the beginning and end of the coil are respectively connected to two mutually insulated arc-shaped commutator segm
[Microcontroller]
51 single chip microcomputer DC motor experiment
I2C communication timing problem of 89 and 12 microcontrollers
I accidentally discovered this problem while using ADC today and would like to share it with all my friends. I2CDelay of 89: #define I2CDelay() {_nop_();_nop_();_nop_();_nop_();} I2CDelay of 12: void Delay_us(unsigned char us) {     do {         _nop_();         _nop_();         _nop_();         _nop_();      
[Microcontroller]
51 MCU - Memory
1. Memory Overview         Memory is a collection of many storage units. Memory units are actually a type of sequential logic circuit (latch), arranged in order of unit number. Each unit is composed of several binary bits to represent the value stored in the storage unit. This structure is very similar to the structur
[Microcontroller]
51 MCU - Memory
Three functions of 51 single chip microcomputer P0 port
1. When the external memory is expanded, it is used as a data bus (D0~D7 in Figure 1 is the data bus interface) 2. When the external memory is expanded, it is used as the address bus (A0~A7 in Figure 1 is the address bus interface) 3. When not expanded, it can be used as a general I/O, but there is no internal pull-u
[Microcontroller]
Three functions of 51 single chip microcomputer P0 port
51 MCU H-bridge circuit controls motor forward and reverse rotation and PWM speed regulation
I built an H-bridge circuit to control the forward and reverse rotation and PWM speed regulation of the motor. The program is available on the Internet. You can use it by changing the pins. The circuit and source program are as follows:      Function: P1.1 button to stop, P1.2 to turn left, P1.3 to turn right, P1.
[Microcontroller]
51 MCU H-bridge circuit controls motor forward and reverse rotation and PWM speed regulation
Design of Blood Glucose Meter Based on C8051F410 Single Chip Microcomputer
The electro-biochemical principle of blood glucose measurement is that when a certain voltage is applied to the blood after the enzyme reaction, the current generated will increase as the blood glucose concentration in the blood increases. By accurately measuring these weak currents and calculating the corresponding c
[Microcontroller]
Design of Blood Glucose Meter Based on C8051F410 Single Chip Microcomputer
Utilizing 16C554 to realize master-slave microcontroller long-distance communication expansion
    Abstract: The upper host computer controls multiple MODEMs through 16C554, and the back-to-back connection is used to realize long-distance communication between the host computer and the lower computer. This system has been successfully used for information transmission at urban traffic intersections.     Ke
[Industrial Control]
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号