单片机串口通信,为啥我上位机发送命令单片机没有反应?求高手详解用的是查询法
题目:按下k1,向上位机发送“hello, what do you want to do?”。用上位机软件输入m1,则m1数码管全亮,其他位灭。m2-m4与此相同
用上位机软件输入L1,则L1全亮,其他位灭。L2-L4与此相同
用上位机软件输入beer,蜂鸣器响1s
输入其它不起作用。
注:上位机软件就是串口调试小助手
程序代码:
# include<reg52.h> # include<HL_1.h> unsigned char a[10]; unsigned char flag = 0, count = 0; unsigned char code TABLE[30] = "hello, what do you want to do?"; void Init(void) { TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; REN = 1; SM0 = 0; SM1 = 1; // EA = 1; // ES = 1; } void Switch_K1(void) { char c;//控制数组字符位置 if(!K1) { delay(50); if(!K1) { for(c=0;c<30;++c) { SBUF = TABLE[c]; while(!TI); TI = 0; } } } } void Control(void) { if(a[0]=='m') { if(a[1]=='1') { WE = 1; P0 = 0xfe; WE = 0; DU = 1; P0 = 0xff; DU = 0; count = 0; } if(a[1]=='2') { WE = 1; P0 = 0xfd; WE = 0; DU = 1; P0 = 0xff; DU = 0; count = 0; } if(a[1]=='3') { WE = 1; P0 = 0xfb; WE = 0; DU = 1; P0 = 0xff; DU = 0; count = 0; } if(a[1]=='4') { WE = 1; P0 = 0xf7; WE = 0; DU = 1; P0 = 0xff; DU = 0; count = 0; } } if(a[0]=='L') { if(a[1]=='1') { P1 = 0xff; LED0 = 0; count = 0; } if(a[1]=='2') { P1 = 0xff; LED1 = 0; count = 0; } if(a[1]=='3') { P1 = 0xff; LED2 = 0; count = 0; } if(a[1]=='4') { P1 = 0xff; LED3 = 0; count = 0; } } if(a[0]=='b'&&a[1]=='e'&&a[2]=='e'&&a[3]=='r') { P1 = 0xff; FM = 0; delay(1000); FM = 1; count = 0; } } void main() { unsigned int j; Init(); while(1) { RI = 0; Switch_K1(); Control(); for(j=0;j<5000;j++)//这里是希望通过这么长时间的循环判断让上位机发送的数据全部放到a[10]数组里,以便单片机做出对应处理 { if(RI&&(SBUF>47&&SBUF<123)) { a[count] = SBUF; count++; } } } }
我的代码如下