According to the key value code returned by scanning the keyboard, the key value is looked up in the key value code table to obtain the key value and send it to the digital tube for display.
When the power is turned on, the digital tube displays “-”.
When a key is pressed, the digital tube displays the key value of the pressed key and the buzzer sounds once. */
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P3^6; //Buzzer drive line
uchar key;
unsigned char code disp_code[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};
unsigned char code key_code[]={
0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77 };
/**********************************************************
Delay subfunction
**********************************************************/
void delayms(uint ms)
{
uchar t;
while(ms--)
{
for(t = 0; t < 120; t++);
}
}
/**********************************************************
x*0.14MS delay sub-function
**********************************************************/
void delay0(uchar x)
{
uchar i;
while(x--)
{
for (i = 0; i<13; i++) {;}
}
}
/**********************************************************
Buzzer driver subfunction
**********************************************************/
void beep()
{
uchar i;
for (i=0;i<180;i++)
{
delay0(5);
BEEP=!BEEP; //BEEP inversion
}
BEEP=1; //turn off the buzzer
delayms(250); //delay
}
/**********************************************************
Keyboard scanning subroutine
****************************************************** ********/
fly keyscan()
{
fly scan1,scan2,keycode,j;
P1=0xf0;
scan1=P1;
if((scan1&0xf0)!=0xf0) //Judge whether the key is pressed
{
delayms(30); //Delay 30ms
scan1=P1;
if((scan1&0xf0)!=0xf0) //Secondary judgment whether the key is pressed
{
P1=0x0f;
scan2=P1;
keycode=scan1|scan2; //Combined into key code
for(j=0;j<=15;j++)
{
if(keycode== key_code[j]) //Look up the table to get the key value
{
key=j;
return(key);
}
}
}
}
else P1=0xff;
return (16);
}
/**********************************************************
Determine whether the key is pressed sub function
**********************************************************/
void keydown()
{
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
keyscan();
P0=disp_code[key];
beep();
}
}
/**********************************************************
Main function
**************************************************************/
main()
{
P0 = 0xbf;
P2 = 0x7f; //The digital tube displays "-"
P1 = 0xff;
while(1)
{
keydown();
}
}
Previous article:Matrix keyboard key value display program C language programming
Next article:Digital tube display number 5 C language programming
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Guys, please help me find out what the problem is.
- I would like to ask about the interrupt problem of msp430f5529
- Three questions on embedded ARM basics
- [Rawpixel RVB2601 development board trial experience] GRB breathing light
- Dancing flame pendant
- Do you purchase samples or apply for them for free?
- SDK BLE example project command line compilation (HeartRate device)
- EEWorld interviews Toshiba PCIM booth engineer: 2-minute short video shows you key products and solutions
- Is it necessary to learn TI's KeystoneII series multi-core heterogeneous processors?
- cc2540 cc2541 low power consumption measurement and summary - and precautions - low power consumption less than 10uA