|
串口发送完固定字符后,按下键码不能发送指定字符protues仿真结果如下
p1为未按下键码仿真结果
p2为按下键码后结果 (s2,s3按下情况相同)
#include<reg51.h>
#include"Delay.h"
unsigned char scan()
{
if((P3&0x0C)!=0x0C)
{Delay(10);
if((P3&0x0C)!=0x0C)
{return(P3&0x0C);
}
}
return 0xFF;
}
void sendbyte(unsigned char byte)
{
SBUF=byte;
while(!TI);
TI=0;
}
void sendstr(unsigned char *str)
{
while(*str!='\0')
{
sendbyte(*str);
*str++;
}
}
void time_adjust()
{
static bit kp=0;
unsigned char s_code=scan();//S——scan键盘扫描
if((s_code!=0xFF)&&(kp==0))//none=0xFF
{
kp=1;
switch(s_code)
{
case 0x08:
{sendy("222\r\n");
break;
}
case 0x04:
{
sendy("111\r\n");
break;
}
default:break;
}
}
else if((kp==1)&&(s_code==0xFF))
{
kp=0;
}
}
void main()
{
SCON=0x50;//串口方式1,RX/TX
ES=1;EA=1;//使能
TMOD=TMOD|0x20;//方式2定时
TL1=0xFD;TH1=0xFD;//9600bps初值@110952Mhz
TR1=1;
sendstr("2021040140238\r\n");
sendstr("0\r\n");
while(1)
{
time_adjust();
}
}
void isr_uart()interrupt 4
{
if(RI==1)
{
P1=SBUF;
RI=0;
}
}
|
|