用键盘检测程序检测按键按下,检测到按下就用串口向上位机发送字符“1”的ASCII码值,这样在串口调试助手里就可以看到字符“1”了
首先你必须要做一个串口通讯程序,然后扫描端口,把扫描到的端口编码发送出去就可以了
#include
#include
sbit Key0_IN = P1^0;
sbit Key1_IN = P1^1;
sbit Key2_IN = P1^2;
sbit Key3_IN = P1^3;
sbit Key4_IN = P1^4;
sbit Key5_IN = P1^5;
sbit Key6_IN = P1^6;
sbit Key7_IN = P1^7;
//
unsigned char UARTByte;
unsigned char MACByte;
unsigned char Cont;
const unsigned char code UART_TABLE1[]={1,};
const unsigned char code UART_TABLE2[]={2};
const unsigned char code UART_TABLE3[]={3};
const unsigned char code UART_TABLE4[]={4};
const unsigned char code UART_TABLE5[]={5};
const unsigned char code UART_TABLE6[]={6};
const unsigned char code UART_TABLE7[]={7};
const unsigned char code UART_TABLE8[]={8};
//
void UART_Send_Byte(unsigned char Byte);
//?óê±
void Delay_1ms(unsigned int time)
{
unsigned int a, b;
for(a=time; a>0; a--)
for(b=110; b>0; b--);
}
int main(void)
{
unsigned char i;
SCON=0X50;
TMOD=0X20;
PCON=0X00;
TH1=0XFD;
TL1=0XFD;
ES=1;
EA=1;
TR1=1;
while(1)
{
MACByte=P1;
if(MACByte!=0xFF)
{
MACByte=P1;
switch(MACByte)
{
case 0xFE: Cont = 1;break;
case 0xFD: Cont = 2;break;
case 0xFB: Cont = 3;break;
case 0xF7: Cont = 4;break;
case 0xEF: Cont = 5;break;
case 0xDF: Cont = 6;break;
case 0xBF: Cont = 7;break;
case 0x7F: Cont = 8;break;
default : Cont = 0;break;
}
for(i = 0; i < 2; i++)
{
if(Cont == 1)
UART_Send_Byte(UART_TABLE1[i]);
else if(Cont == 2)
UART_Send_Byte(UART_TABLE2[i]);
else if(Cont == 3)
UART_Send_Byte(UART_TABLE3[i]);
else if(Cont == 4)
UART_Send_Byte(UART_TABLE4[i]);
else if(Cont == 5)
UART_Send_Byte(UART_TABLE5[i]);
else if(Cont == 6)
UART_Send_Byte(UART_TABLE6[i]);
else if(Cont == 7)
UART_Send_Byte(UART_TABLE7[i]);
else if(Cont == 8)
UART_Send_Byte(UART_TABLE8[i]);
}
Delay_1ms(1000);
}
}
}
void UART_Send_Byte(unsigned char Byte)
{
ES = 0;
TI = 0;
SBUF = Byte;
while(!TI);
TI = 0;
ES = 1;
}
void UART_IRQ(void) interrupt 4
{
if(RI)
{
UARTByte = SBUF;
RI = 0;
}
}