#include
#define key_port P0 //Keyboard interface definition
sbit key_port_0=key_port^0;
sbit key_port_1=key_port^1;
sbit key_port_2=key_port^2;
sbit key_port_3=key_port^3;
/*******************************
STC89C59 MCU one millisecond delay function
*******************************/
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for( i=0;i
}
/*****************************
The serial port sends a character
**************************/
void com_send_dat( unsigned char dat)
{
SBUF=dat;
while (TI== 0);
TI= 0 ;
}
/******************************
Serial port initialization
******************************/
void init_com( void )
{
SCON=0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr //UART is mode 1, 8-bit data, allow reception
TMOD|=0x20 ; //TMOD: timer 1, mode 2, 8-bit reload //Timer 1 is mode 2, 8-bit automatic reload
TH1=0xfa ; //Baud:19200 fosc="22.1184MHz
TL1=0xfa;
PCON|=0x80; //SMOD=1; double the baud rate;
ES=1; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
}
/**************************
Keyboard scanning function
*****************************/
unsigned char keyscan(void)
{
unsigned char key,i;
unsigned char co
de key_table[16]=
{0xee,0xed,0xeb,0xe7,0xde,0xdd,0xdb,0xd7,0xbe,0xbd,0xbb,0xb7,0x7e,0x7d,0x7b,0x77};
key_port=0x0f; //Determine the row and column position
if(key_port==0x0f)return(0);//Return to 0 if no key is pressed
delay_ms(10); //Call the delay function to remove the front key jitter.
if(key_port==0x0f)return(0);//Judge again The purpose is to ensure correct detection
else
{
for(i=0;i<4;i++) //The following is the classic calculation of key value (to determine the location of the closed key)
{
P0=_cror_(0x7f,i);
if(key_port_0==0)break;
if(key_port_1==0)break;
if(key_port_2==0)break;
if(key_port_3==0)break;
}
key=key_port; //Get the key value
for(;key_port!=0x0f; key_port=0x0f); //Wait for the key to be released, the purpose is to remove the back edge key jitter
for(i=0;key_table[i]!=key && i<16;i++); //Look up the table to get the value of key 0-F
key=i;
return(key); //Return to the main calling function with key value
}
}
/*****************************
Keyboard scan test main function
**************************/
void main(void)
{
unsigned char key;
init_com(); //Serial port initialization
while(1)
{
key=keyscan();
if(key!=0)
{
com_send_dat(key);
delay_ms(200);
}
}
}
Previous article:80C51 Serial Expansion Technology
Next article:C51 simulates SPI interface
- 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
- 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)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Another new board has arrived~~Jiefa AC7801X motor demo board is here, and the evaluation activity will be online soon...
- CY8CKIT-149 PSoC 4100S Review (1)
- STM32F765@216MHz running NES emulator [code] [video]
- Which expert can help me solve the volume control problem of the digital-analog chip WM8766G?
- Recommend! Good domestic chips you have used
- I heard someone bought a fake chip? I'm doubting myself after adjusting the fake chip
- Warm congratulations to the Chinese men's football team for drawing with the powerful Philippines
- Using NT33510 display screen, change the digital content on the display screen through the matrix keyboard
- MicroPython version 1.16 released
- [NUCLEO-L552ZE review] + Development environment construction