“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Small code farmer for president, cough cough, for knowledge dare to say the key won’t?
Independent keys and matrix keys
Separate button
pattern
After the button is pressed down, there will often be a jitter problem. The elasticity of the button itself, or the button is pressed down or released, will form an unstable process of impact. This process takes a certain amount of time, so the single chip microcomputer cannot read the IO state in this process, which can be delayed by software or hardware
Schematic diagram
Simple function of buttons (can lean up for smart car)
Display process
The reason why I did not make GIF is that I have not been less than 1M resulting in typora insert I do not show, so cruel contribute to B station, B station is my video library in the future
function
There’s nothing interesting about it, nothing exciting about it, so let’s start over, okay
Semi-finished product demonstration
Add the display of the long press logo
Gai version shows
// Key scan
void Key_Scan_Drive(a)
{
static bit flag = 0;// The length of the signal
static bit key_down = 0;// Press the flag. Since double clicking also uses this, the tag range has to be expanded
static u8 count1 = 0;// count 1, u8 is sufficient for 100
static u16 count2 = 0;// use u16 if the counter is greater than 255
if(key_down)// Check whether the button is pressed
{
count2++;
if(count2>250)
{
count2 = 0;
flag = 1;
// Long press the program
LCD12864_Write_Cmd(0x8c);
LCD12864_Write_Data(0x30);
LCD12864_Write_Data(0x30+LCD12864_Num_Buffer[1]);
}
if(KEY1)
{
key_down = 0;// Release to clear the flag
count2 = 0;
if(flag)
{
flag = 0;
}
else
{
// Press the program
LCD12864_Write_Cmd(0x8c);
LCD12864_Write_Data(0x30);
LCD12864_Write_Data(0x30+LCD12864_Num_Buffer[3]); }}}else
{
if(! KEY1) { count1++;if(count1>20)
{
count1 = 0;
if(! KEY1) { key_down =1;
}
}
}
}
}
Copy the code
There are always press is always add function, but the code is basically similar, there is no need to write, are repeated similar work
Orgasm incoming
Matrix keyboard
P6^0 P6^1 P6^2 P6^3 Quad-line is a row scan, P6^4 P6^5 P6^6 P6^7 quad-line is a column scan. 4 rows and 4 columns use a total of 8 IO ports, which can scan the switch status of 16 keys, which is the advantage of the matrix keyboard. (The IO port must be connected in series resistance).
Schematic diagram
Analysis of the
#include "all.h"
u8 code KEY4x4_Buffer[4] = {0xef.0xdf.0xbf.0x7f};// Column input/column control array
u8 xdata KEY4x4_Read_Byte = 0; // Bytes read/detected
void KEY4x4_Drive_Init(a)
{
P6M1 = 0;
P6M0 = 0;
}
void KEY4x4_Scan_Drive(a)
{
static xdata u16 count = 0;
u8 i = 0;
u8 j = 0;// loop to scan variables
u8 Value = 0;// Test row variables
count++;
if(count>10)// Reduce the number of cycles by counting
{
count = 0;
for(i = 0; i<4; i++)// Column input/column control
{
P6 = KEY4x4_Buffer[i];
Value = 0x08;
for(j = 0; j<4; j++) {if(! (Value&P6))// Which bit is pulled to 0 will be detected
{
KEY4x4_Read_Byte = j*4+i+1;
}
Value >>= 1;// Move to the right one bit
}
}
P6 = 0xff;//P6 is used up, because it will be used later}}Copy the code