<> Difficult things can't be learned because simple knowledge isn't learned well

<> The foundation is not solid , the earth trembled and the mountains swayed

<> Key

<>1.1 Basic review

(1) Physical structure of keys
(2) There are two states of MCU pin (I/O) input 【 read 】 Or output 【 write 】
(3) Function of pull up resistor . Pull the pin up to a certain high level through the resistance , But the pull-up energy can't hold it . The pull-down force is relatively strong , The power of grounding is infinite !
(4) The meaning of keys : A button is an input device ,CPU Connected by a key IO The level state of the pin can judge what we have done to the button , Press or pop .
(5)CPU How to detect keys : Polling and interrupting
polled :CPU Check to see if the key is pressed after a period of time , If the key is pressed, the key is handled , If not, check again when the next polling time is up .
Interrupted :
(6) Classification and connection of keys :
Matrix button
Independent key

<>1.2 Independent key explanation
eg :sbit key = P0^0;
(1) Give variable key assignment , It's like asking this IO Port output , Direct use 【 read 】 The value of this variable , It's like starting from this IO Pin in .
(2) Independent buttons can press multiple buttons at the same time , The matrix button is not allowed .
(3) You can have a bit variable to control the key , It can also be controlled by port .

<>1.3 Key value detection and display

(1) What is a key value : There are many buttons in a product , The program encodes the keys , Each key corresponds to a code value , The code value is the key value .
(2) The key detection part and the key processing part are connected by the key value .

<>1.4 shake

(1) What is jitter : The level change of uncertainty at the moment when the key is pressed and bounced .
(2) The harm of jitter : Judgment of interference normal level .
(3) Eliminate the scuttle : Software and hardware chattering elimination

<>1.5 Complete key detection

<>1.5.1 A complete key event

(1) Key events are different states of the key process ( Press and pop ) Switch of
(2) A complete button state includes press event and pop-up event ( Change and switch of level )
(3) It is generally believed that a complete key event is a real key , The program will handle the key , Therefore, the general press and lift after the button is an effective button .

<>1.5.2 Code practice
#include <reg51.h> /* connection : P1 Port connection key key1-key8 key1:P1^0 …… key8:P1^7 P0 Port connected to independent digital tube .
function : It is divided into two parts : Key detection and key processing . Key detection --- Get the corresponding key value --- Do different things according to different key values . */ sbit key1 = P1^0; void
delay(unsigned char t); void display(void); void delay10ms(void); // Display of independent digital tube 0-F
unsigned char varry[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xA1,0x86,0x8e}; unsigned char keynum = 0; void
main(void) { unsigned char flag = 0; // Flag bit , It's when it's not pressed 0, It's when you press it 1 // realization key1 Click the number plus 1
, And it's after pressing plus immediately plus 1, I can't say anything after I lift it up while(1) { if(key1 == 0) { delay10ms(); if(key1 == 0)
// There was a button pressed { if(flag == 0) { display(); flag = 1; // Key status flag position 1 } } } else
// The key is up , The button is not pressed at this time { delay10ms(); flag = 0; } delay10ms(); } } void
delay(unsigned char t) { unsigned char i,j; for(i=0;i<t;i++) for(j=0;j<t;j++);
} void display(void) { P0 = varry[keynum]; keynum += 1; if(keynum> 15) keynum =
0; } void delay10ms(void) // error 0us { unsigned char a,b,c; for(c=5;c>0;c--)
for(b=4;b>0;b--) for(a=248;a>0;a--); }

The logic of this program is this : First of all, check that there is a button pressed —> When the flag bit is in the default state, press the key to display the content and set the flag position 1( Place 1 After that, even when the button is pressed, the display state is still the original state , It doesn't jump continuously )-----> Check the status of key lifting ( When it's true ), Flag bit reset .

<>1.6 Introduction of interruption

(1) Mainline task and interrupt task
Main task : It's busy for a long time, but it's not very urgent
Interrupt task : Time is short and busy
(2) Interrupt mode is more suitable for handling asynchronous events than rotation mode , More efficient .

<>1.7 AT89C51 interrupt

(1) Interrupt trigger mode : Falling edge trigger and low level trigger
(2) Practical training

<>1.8 Matrix keyboard

(1) The pins on both sides of the matrix keyboard are connected to the MCU pins , The independent button is connected to the MCU pin while grounding .
(2) Matrix keyboard Province IO mouth
(3) The matrix keyboard does not support simultaneous pressing
(4) Look at the schematic diagram of the matrix keyboard :4 individual IO Port control line ,4 individual IO Port control column .
/******************* How to judge input and output ********************/ P0 = 0xf0; // from IO output , write IO mouth
if(P0 != 0xf0) // from IO input , read IO mouth I/O Port is the general output port , hundred I/O The port can only be in and out 0 and 1,0 Corresponding to low level ,
1 Corresponding to high level , If it is 3.3V system , High level is 3.3, If 5V, The high level is 5V, Low level is 0V. If it's an output port , Is the MCU through the software to set the relevant register to let the
Port set high or low , Achieve the purpose of level output , If Dao Guo is used as the input port , It's a single chip The computer captures the specific level of the port and then sets the relevant registers , Then the software reads the data in the register 0 or 1, achieve
Input function . This is a very popular understanding ,
<>1.8.1 Matrix keyboard control idea

(1) Send it first (IO Pin out )0x0f
(2) If there is a key to receive is not 0x0f, Data received from (IO Pin in ) Determine which line is pressed
(3) resend (IO Pin out )0xf0
(4) Data received from (IO Pin in ) Determine which column is pressed
(5) comprehensive 2 Second row and column positions , Calculate the key value

<>1.8.12 Real combat of matrix keyboard
#include <reg51.h> // The matrix keyboard is used to press the key to display on the nixie tube in turn 0-F. /* connection : Matrix keyboard :P1 port Digital tube : P0 port */
#define KEY P3 #define DIG P0 void delay10ms(void); unsigned char GetKey(void);
// Display of independent digital tube 0-F unsigned char varry[16] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xA1,0x86,0x8e}; unsigned char keynum = 0; void
main(void) { unsigned char Key = 0; while(1) { Key = GetKey(); if(Key != 0) {
DIG = varry[Key]; } } } void delay10ms(void) // error 0us { unsigned char a,b,c;
for(c=5;c>0;c--) for(b=4;b>0;b--) for(a=248;a>0;a--); } unsigned char
GetKey(void) { unsigned char KeyValue = 0; unsigned char hang = 0,lie = 0;
unsigned char flag = 0; KEY = 0x0f; if(KEY != 0x0f) { delay10ms(); switch (KEY)
{ case 0x0e: hang = 1; break; case 0x0d: hang = 2; break; case 0x0b: hang = 3;
break; case 0x07: hang = 4; break; default:break; } KEY = 0xf0; if(KEY != 0xf0)
{ switch (KEY) { case 0xe0: lie = 1; break; case 0xd0: lie = 2; break; case
0xb0: lie = 3; break; case 0x70: lie = 4; break; default:break; } KeyValue =
(hang - 1)*4 + lie; } return KeyValue; } return 0; }

Technology
Daily Recommendation