#include
#include
/*
Program function: This program is used to test the 4X4 matrix keyboard; and has the ability to long press test.
When a key is pressed, the pressed key number is displayed on LCD1602, such as: K1 K2.... K16;
When a key is pressed for more than 1S, a key value is returned every 250MS, realizing the function of long pressing.
At the same time, the key value is sent to the port through the serial port for display;
Program Description: This is a traditional method of using delay to scan keys, drive keys and test programs; this method is
It is not used in actual project programming. It is used in teaching when students are just beginning to learn microcontrollers.
Note: In this program, only the long press of K4 is displayed. If you want to handle the long press of other keys, just add
Just handle the relevant ones.
*/
/*
Matrix keyboard pin definition
*/
sbit keyin4 = P2^7;
sbit keyin3 = P2^6;
sbit keyin2 = P2^5;
sbit keyin1 = P2^4;
sbit keyout1 = P2^3;
sbit keyout2 = P2^2;
sbit keyout3 = P2^1;
sbit keyout4 = P2^0;
//Macro definition
#define TH0_NUM 1000 //Easy to transplant the program;
#define TL0_NUM 1000
bit Flag1s, Flag250Ms, Flag2Ms; //Time reached flag
//lcd1602 display array
unsigned char keynum[]="keynum:k ";
unsigned char keycount[3]="000";
//Key value query array
//k1,k2,k3,k4,k5,k6,k7,k8,
unsigned char code keytabel[]={0xe7,0xd7,0xb7,0x77,0xeb,0xdb,0xbb,0x7b,
//k9,k10,k11,k12,k13,k14,k15,k16,
0xed,0xdd,0xbd,0x7d,0xee,0xde,0xbe,0x7e};
/*
External function declaration
*/
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
void ConfigUART(unsigned int baud);
void DelayMs(unsigned int ms);
unsigned char keyscan();
void InitTimer0();
/*
Main Program
*/
void main()
{
unsigned char i,j,keytemp=0;
InitTimer0();
ConfigUART(9600);
InitLcd1602();
LcdShowStr(0,0,keynum);
while(1)
{
keytemp=keyscan();
if(keytemp!=0) //Judge whether a key is pressed;
{
for(i=0;i<16;i++) //When a key is pressed, look up the table to get the key value;
{
if(keytemp == keytabel[i])
break;
}
keynum[8]=(i+1)/10+0x30;
keynum[9]=(i+1)%10+0x30;
TI=1;
SBUF=keytemp;
while(TI!=1);
TI=0;
LcdShowStr(0,0,keynum);
if(keytemp==0x77) //Used to detect long press of up key K4, and display on the second line of LCD1602
{ //Add 1 every 250MS;
j++;
if(j>254)
j=0;
keycount[0] = j/100%10+0x30;
keycount[1] = j/10%10+0x30;
keycount[2] = j%10+0x30;
TI=1; //Used for serial port, print plus 1 every 250MS;
printf("key is long pushing %s \n",keycount);
while(TI!=1);
TI=0;
}
LcdShowStr(0,1,keycount);
}
}
}
/*
Function: N milliseconds delay
*/
void DelayMs(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
Reference address:51 MCU long key test program
for(j=0;j<113;j++);
}
/*
Function: Matrix keyboard scanning (with long key detection function)
*/
unsigned char keyscan()
{
static unsigned char num1,temp;
P2 = 0xf0;
num1=P2;
if(num1!=0xf0)
{
DelayMs(50);
num1=P2;
if(num1!=0xf0)
{
TR0=1; //Turn on the timer
temp=num1;//
P2 = 0x0f;
DelayMs(1);
num1=P2;
temp =temp|num1;
do
{
P2 = 0xf0;
num1=P2;
if(Flag1s == 1) // Check if the key is pressed for 1S;
{
if(Flag250Ms==1) //If you don't release the key within 1S, it means you have long pressed a key; then return every 250MS
{ //One key value; this implements the function of long pressing a key
Flag250Ms=0;
return temp;
}
}
}
while(num1!=0xf0);
}
else
{
TR0=0; //If not pressed, turn off the timer
Flag1s=0;
Flag250Ms=0;
temp=0;
}
}
else
{
TR0=0;
Flag1s=0;
Flag250Ms=0;
temp = 0;
}
TR0=0; //When you let go, you should also turn off the timer and clear several flags;
Flag1s=0;
Flag250Ms=0;
return temp;
}
/*
Function: Initialize timer 0 and set it to interrupt once every 1MS
*/
void InitTimer0()
{
TMOD &=0xf0; //This operation mode will not destroy other settings of the register;
TMOD |= 0x01; //Mode 1, 16-bit timer
TH0/=(65536-TH0_NUM)/256;
TL0=(65536-TL0_NUM)%256;
EA=1;
ET0=1;
TR0=0;
}
/*
Function: Timer 0 interrupt service routine
*/
void Timer0_Interrupt() interrupt 1
{
static unsigned char i,j,k;
TH0=(65536-TH0_NUM)/256;
TL0=(65536-TL0_NUM)%256;
i++;j++;
if(i>2)
{
i=0;
Flag2Ms=1;
}
if(j==250)
{
j=0;
Flag250Ms=1;
k++;
if(k==4)
{
k=0;
Flag1s=1;
}
}
}
Previous article:Design and implementation of liquid level monitoring instrument using multiple single chip microcomputers
Next article:51 MCU 4X4 matrix keyboard driver and application (C language)
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Pulse meter based on 51 single chip monitors pulse rate and draws electrocardiogram
- EEWORLD University - How to repair damaged printed lines
- [Environmental Expert's Smart Watch] Part 17: Watch Modeling and Animation
- Thank you for your partnership
- NUCLEO-G431RB Review -> COMP
- ST MEMS Device Resource Library - ST MEMS Sensor Design Application Guide and Frequently Asked Questions (FAQ)
- EEWORLD University ---- Texas Instruments SimpleLink? Wi-Fi Solution
- Qinheng CH32V103 Review Summary
- Motor stroke control problem
- Urgently looking for chip model