MUC与pc串口通信时 出现错误帧
程序如下 运行到send_one时出现错误帧
#include<reg52.h>
#include<intrins.h>
sbit k2=P3^1;
sbit k1=P3^0;
sbit rs=P2^4;
sbit rw=P2^5;
sbit ep=P2^6;
unsigned char num1=0;
unsigned char num2=0;
unsigned char cou=0;
unsigned char check[15]; //数据缓存
unsigned char message_string[50];
unsigned char message_sended[]={'T','h','e','M','e','s','s','a','g','e','H','a','s','B','e','e','n','S','e','n','d','e','d','.','\0'};
unsigned char code connecting[]={'c','o','n','n','e','c','t','i','n','g','.','.','.','\0'};
unsigned char code start[]={0xa1,'\0'};
unsigned char code message[]={'W','r','i','t','e','T','h','e','M','e','s','s','a','g','e',':','\0'};
unsigned char code wrong[]={'E','r','r','o','r','!','\0'};
unsigned char code num[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
unsigned char code tc_at[]={'A','T','\n','\0'};
unsigned char code tc_at_cmgf[]={'A','T','+','C','M','G','F','=','1','\n','\0'};
unsigned char code tc_at_cmgs[]={'A','T','+','C','M','G','S','=','"','+','8','6','1','5','9','5','1','8','6','3','3','1','6','"','\n','\0'};
unsigned char code tc_at_csca[]={'A','T','+','C','S','C','A','=','"','+','8','6','1','3','8','0','0','2','5','0','5','0','0','"','\n','\0'};
void delay(unsigned int n) //延迟nMS
{
unsigned int m;
while(n--)
{
for(m=0;m<=114;m++);
}
}
bit lcd_bz() //忙
{
bit result;
rs=0;
rw=1;
ep=1;
_nop_();_nop_();_nop_();
result=(bit)(P0&0x80);
_nop_();_nop_();_nop_();
ep=0;
_nop_();_nop_();_nop_();
return result;
}
void lcd_wcmd(unsigned char cmd) //写命令
{
while(lcd_bz());
rs=0;
rw=0;
ep=0;
_nop_();_nop_();_nop_();
P0=cmd;
_nop_();_nop_();_nop_();
ep=1;
_nop_();_nop_();_nop_();
ep=0;
}
void lcd_wdat(unsigned char dat) //写数据
{
while(lcd_bz());
rs=1;
rw=0;
ep=0;
P0=dat;
delay(80);
_nop_();_nop_();_nop_();
ep=1;
_nop_();_nop_();_nop_();
ep=0;
}
void lcd_init() //初始化
{
lcd_wcmd(0x01);
delay(1);
lcd_wcmd(0x05);
delay(1);
lcd_wcmd(0x38);
delay(1);
lcd_wcmd(0x0f);
delay(1);
lcd_wcmd(0x06);
delay(1);
}
void uart_init() //串口初始化
{
SCON = 0x50;
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
ES = 1;
TR1 = 1;
EA = 1;
}
void display(unsigned char *p3) //显示
{
unsigned int m=0;
while(*p3!='\0')
{
if(m<=15)
{
lcd_wcmd(0x00+0x80+m);
}
if(m>=16&&m<=31)
{
lcd_wcmd(0x40+0x70+m);
}
if(m>=32&&m<=47)
{
lcd_wcmd(0x00+0x70+m);
}
if(m>=48&&m<=63)
{
lcd_wcmd(0x40+0x60+m);
}
if(m>=64)
{
lcd_wcmd(0x01);
m=0;
}
lcd_wdat(*p3);
p3++;
m++;
}
}
void send(unsigned char *p1) //串口发送
{
unsigned int count=0;
unsigned char *p2;
while(1)
{
unsigned int k=1;
unsigned int flag=0;
cou=0;
count++;
p2=p1;
while(*p2!='\0')
{
SBUF=*p2;
while(!TI);
TI=0;
p2++;
}
delay(500);
while(check[k]!='\0')
{
if(check[k-1]=='O'&&check[k]=='K')
{
check[k-1]='\0';
check[k]='\0';
flag++;
break;
}
k++;
}
if(flag==1)
{
break;
}
if(count==10)
{
lcd_wcmd(0x01);
display(wrong);
while(1);
}
}
}
void send_one(unsigned char *p3)
{
while(*p3!='\0')
{
SBUF=*p3;
while(!TI);
TI=0;
p3++;
}
}
unsigned char keyscan(void) //键盘扫描
{
unsigned char cord_h,cord_l;
P1=0x0f;
cord_h=P1&0x0f;
if(cord_h!=0x0f)
{
delay(9);
if((P1&0x0f)!=0x0f)
{
cord_h=P1&0x0f;
P1=cord_h|0xf0;
cord_l=P1&0xf0;
return(cord_h+cord_l);
}
}
return(0xff);
}
void change(unsigned char m,unsigned char n) //按键变换
{
if(num2==0)
{
lcd_wcmd(0x01);
delay(10);
}
if(num1<m||num1>n)
{
num1=m;
}
if(num2<=15)
{
lcd_wcmd(0x00+0x80+num2);
}
if(num2>=16&&num2<=31)
{
lcd_wcmd(0x40+0x70+num2);
}
if(num2>=32&&num2<=47)
{
lcd_wcmd(0x00+0x70+num2);
}
if(num2>=48&&num2<=63)
{
lcd_wcmd(0x40+0x60+num2);
}
if(num2>=64)
{
lcd_wcmd(0x01);
num2=0;
}
lcd_wdat(num[num1]);
delay(10);
message_string[num2]=num[num1];
num1++;
}
void receive_uart() interrupt 4
{
while(RI==1)
{
RI=0;
check[cou]=SBUF;
cou++;
}
}
void main()
{
unsigned char key;
uart_init(); //串口初始化
lcd_init(); //初始化LCD
display(connecting);
delay(10);
send(tc_at);
send(tc_at_csca);
send(tc_at_cmgf);
send(tc_at_cmgs);
lcd_wcmd(0x01);
display(message);
delay(10);
while(1)
{
while(!k1)
{
delay(10);
{
while(!k1)
{
num2++;
while(!k1)
{
delay(10);
while(!k1);
}
}
}
}
while(!k2)
{
delay(10);
{
while(!k2)
{
send_one(message_string);//////从这开始出现错误帧、、、、
delay(30);
send_one(start);
display(message_sended);
while(1);
}
}
}
key=keyscan();
switch(key)
{
case 0x7e:
{
change(0,2);
break;
}
case 0x7d:
{
change(3,5);
break;
}
case 0x7b:
{
change(6,9);
break;
}
case 0x77:
{
change(10,11);
break;
}
case 0xbe:
{
change(12,13);
break;
}
case 0xbd:
{
change(14,15);
break;
}
case 0xbb:
{
change(16,17);
break;
}
case 0xb7:
{
change(18,29);
break;
}
case 0xde:
{
change(20,21);
break;
}
case 0xdd:
{
change(22,23);
break;
}
case 0xdb:
{
change(24,25);
break;
}
case 0xd7:
{
change(26,27);
break;
}
case 0xee:
{
change(28,39);
break;
}
case 0xed:
{
change(30,31);
break;
}
case 0xeb:
{
change(32,33);
break;
}
case 0xe7:
{
change(34,36);
break;
}
}
}
}