/****************************************Keyboard_no timer_no delay Time characteristics: The keys are effective after letting go, with high sensitivity, low resource consumption and high operating efficiency. The independent keyboard is: K01=P2^4; K02=P2^5; K03=P2^6; K04=P2^7; matrix keyboard is : Row (top to bottom)_P2.3_P2.2_P2.
/****************************************
Keyboard_no timer_no delay
Features:
The button is effective after letting go, with high sensitivity, low resource consumption and high operating efficiency
The independent keyboard is: K01=P2^4; K02=P2^5; K03=P2^6; K04=P2^7;
The matrix keyboard is: row (top to bottom)_P2.3_P2.2_P2.1_P2.0
Column (left to right)_P2.7_P2.6_P2.5_P2.4
Provided operation function:
//Independent keyboard. When there is no key action, the return value num_key=0, otherwise it returns the key number num_key
extern unsigned char keyboard_self();
//Matrix keyboard. When there is no key action, the return value num_key=0, otherwise it returns the key number num_key**** to detect the high four digits
extern unsigned char keyboard_matrix();
****************************************/
.
First look at the independent keyboard (the same algorithm as the matrix keyboard)
-------------------------------------------------- ---------------------
#include
#include
//Independent keyboard. When there is no key action, the return value num_key=0, otherwise it returns the key number num_key
extern unsigned char keyboard_self()
{
unsigned char num_key=0;//Key number
unsigned char temp=0;//Used to read the value of the button on the P2 line
static unsigned char temp_code=0;//Save the key value
staTIc unsigned char num_check=0;//The number of active low
staTIc unsigned char key_flag=0;//Key valid flag
temp=P2&0xF0;//Read P2 line data
if (temp!=0xF0) // low level judgment
{
num_check++;
if (num_check==10)//Continuous 10 times (10ms) low level is valid, then the key is considered valid
{
key_flag=1;//Enable key valid flag
temp_code=temp;//Save the key value
}
}
else//Judge when let go
{
num_check=0;
if (key_flag==1)//Key is valid
{
key_flag=0;
switch (temp_code)//Read the button number
{
case 0xE0: num_key=1;
break;
case 0xD0: num_key=2;
break;
case 0xB0: num_key=3;
break;
case 0x70: num_key=4;
break;
}
}
}
return(num_key);
}
Now it’s the matrix keyboard
-------------------------------------------------- ---------------------
#include
#include
//Matrix keyboard. When there is no key action, the return value num_key=0, otherwise it returns the key number num_key**** to detect the high four digits
extern unsigned char keyboard_matrix()
{
unsigned char num_key=0;//Key number
unsigned char temp=0;//Read P2 port line data
staTIc unsigned char temp_code=0;//used to save the key value
staTIc unsigned char temp_circle=0xFE;//Save the cycle scan value on the P2 line
static unsigned char num_check=0;//Low level count
static unsigned char key_flag=0;//Key valid flag
P2=temp_circle;//0xFX
temp=P2;//Read P2 port line data
if (temp!=temp_circle)//There is a key action
{
num_check++;//Low level count|Increase 1 every low level
if (num_check==10)//Continuous 10 times (10ms) low level is active
{
key_flag=1;//Key valid flag is set to 1
temp_code=temp;//Save the key value
}
}
else//Let go OR no key action, at this time the scan line should be changed
{
num_check=0;
if (key_flag==1)//Key is valid judgment
{
key_flag=0;
switch (temp_code)//Read the button number
{
//P2^0 line
case 0xEE: num_key=1;
break;
case 0xDE: num_key=2;
break;
case 0xBE: num_key=3;
break;
case 0x7E: num_key=4;
break;
//P2^1 line
case 0xED: num_key=5;
break;
case 0xDD: num_key=6;
break;
case 0xBD: num_key=7;
break;
case 0x7D: num_key=8;
break;
//P2^2 line
case 0xEB: num_key=9;
break;
case 0xDB: num_key=10;
break;
case 0xBB: num_key=11;
break;
case 0x7B: num_key=12;
break;
//P2^3 line
case 0xE7: num_key=13;
break;
case 0xD7: num_key=14;
break;
case 0xB7: num_key=15;
break;
case 0x77: num_key=16;
break;
}
}
temp_circle=_crol_(temp_circle, 1);//Change the scan line
if(temp_circle==0xEF)
{
temp_circle=0xFE;
}
}
return(num_key);//Return the key number
}
/************************************************* ************************
When no key is pressed, the scan line keeps changing.
When the key is long, the scan line does not change, making the row of keys become independent keys, and this scan efficiency is extremely high.
For example, when a key on the P2.0 line is pressed, the program will scan to this key, and then the scan line will not change.
When the keyboard program is entered 10 times in succession, the 10 keys are detected to be valid, and the scan line does not change until after letting go
************************************************** ***********************/
Din Rail Connectors,Din Terminal Block,Din Rail Mount Terminal Block,Rail Terminal Block
Cixi Xinke Electronic Technology Co., Ltd. , https://www.cxxinke.com