串口持续接收字符串并显示问题
时间:10-02
整理:3721RD
点击:
刚开始学单片机,最近接手一个项目,是单片机通过串口跟外接设备通信,单片机下发指令指令后设备开始工作并每隔2s发送22字节数据,单片机接收并通过数码管显示。
那么问题来了,现在调试发现单片机只能接收第一次数据并正常显示,后面的就不能显示了,查了好久,麻烦各位大侠给看看,不胜感谢!
下面是C代码:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit button_start=P3^7;
sbit button_stop=P3^6;
sbit jingbao=P1^5;
sbit LED1=P2^7;
sbit LED2=P2^6;
sbit LED3=P2^5;
sbit B_temp=P1^0;
sbit S_temp=P1^1;
sbit G_temp=P1^2;
sbit X_temp=P1^3;
uchar instr_start[]="@0530<>";
uchar instr_stop[]="@0540<>";
uchar SMG_table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar SMG_table2[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar data_temp[22];
uchar j=0;
uchar k;
uchar flag=0;
uint zs_temp;
uchar xs_temp;
void display();
void serial_init()
{
TMOD=0x20;
SCON=0x50;
EX1=1;
EX0=1;
TH1=0xFD;
TL1=0xFD;
EA=1;
ES=1;
TR1=1;
}
void delay(uint i)
{
uint j,k;
for(j=0;j<100;j++)
{
for(k=i;k>0;k--)
;
}
}
void main()
{
serial_init();
while(1)
{
if(RI==1)
{
RI=0;
data_temp[(j++)%22]=SBUF;
}
if(j>22)
j=1;
if(j%22==0)
display();
}
}
void INTO_interrupt(void) interrupt 0
{
uint i;
delay(200);
for(i=0;i<7;i++)
{
SBUF=instr_start;
while(!TI);
TI=0;
}
TI=0;
LED1=0;
LED2=1;
}
void INT1_interrupt(void) interrupt 2
{
uint i;
delay(200);
for(i=0;i<7;i++)
{
SBUF=instr_stop;
while(!TI);
TI=0;
}
TI=0;
LED1=1;
LED2=0;
}
/* void serial_interrupt(void) interrupt 4
{
data_temp[j++]=SBUF;
RI=0;
//flag=0;
if(j==22)
flag=1;
}*/
void display(void)
{
if((data_temp[0]==':')&&((data_temp[4]=='+')||(data_temp[4]=='-'))&&(data_temp[8]=='.'))
{
uchar baiwei,shiwei,gewei,xiaoshu;
baiwei=data_temp[5]>=0x61?(data_temp[5]-0x57):(data_temp[5]-0x30);
shiwei=data_temp[6]>=0x61?(data_temp[6]-0x57):(data_temp[6]-0x30);
gewei=data_temp[7]>=0x61?(data_temp[7]-0x57):(data_temp[7]-0x30);
xiaoshu=data_temp[9]>=0x61?(data_temp[9]-0x57):(data_temp[9]-0x30);
zs_temp=baiwei*256+shiwei*16+gewei;
xs_temp=xiaoshu*10/16;
B_temp=0;
P0=SMG_table[zs_temp/100];
delay(1);
B_temp=1;
P0=0xFF;
S_temp=0;
P0=SMG_table[zs_temp%100/10];
delay(1);
S_temp=1;
P0=0xFF;
G_temp=0;
P0=SMG_table2[zs_temp%10];
delay(1);
G_temp=1;
P0=0xFF;
X_temp=0;
P0=SMG_table[xs_temp];
delay(1);
X_temp=1;
P0=0xFF;
if(zs_temp>90)
jingbao=0;
}
}
那么问题来了,现在调试发现单片机只能接收第一次数据并正常显示,后面的就不能显示了,查了好久,麻烦各位大侠给看看,不胜感谢!
下面是C代码:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit button_start=P3^7;
sbit button_stop=P3^6;
sbit jingbao=P1^5;
sbit LED1=P2^7;
sbit LED2=P2^6;
sbit LED3=P2^5;
sbit B_temp=P1^0;
sbit S_temp=P1^1;
sbit G_temp=P1^2;
sbit X_temp=P1^3;
uchar instr_start[]="@0530<>";
uchar instr_stop[]="@0540<>";
uchar SMG_table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar SMG_table2[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar data_temp[22];
uchar j=0;
uchar k;
uchar flag=0;
uint zs_temp;
uchar xs_temp;
void display();
void serial_init()
{
TMOD=0x20;
SCON=0x50;
EX1=1;
EX0=1;
TH1=0xFD;
TL1=0xFD;
EA=1;
ES=1;
TR1=1;
}
void delay(uint i)
{
uint j,k;
for(j=0;j<100;j++)
{
for(k=i;k>0;k--)
;
}
}
void main()
{
serial_init();
while(1)
{
if(RI==1)
{
RI=0;
data_temp[(j++)%22]=SBUF;
}
if(j>22)
j=1;
if(j%22==0)
display();
}
}
void INTO_interrupt(void) interrupt 0
{
uint i;
delay(200);
for(i=0;i<7;i++)
{
SBUF=instr_start;
while(!TI);
TI=0;
}
TI=0;
LED1=0;
LED2=1;
}
void INT1_interrupt(void) interrupt 2
{
uint i;
delay(200);
for(i=0;i<7;i++)
{
SBUF=instr_stop;
while(!TI);
TI=0;
}
TI=0;
LED1=1;
LED2=0;
}
/* void serial_interrupt(void) interrupt 4
{
data_temp[j++]=SBUF;
RI=0;
//flag=0;
if(j==22)
flag=1;
}*/
void display(void)
{
if((data_temp[0]==':')&&((data_temp[4]=='+')||(data_temp[4]=='-'))&&(data_temp[8]=='.'))
{
uchar baiwei,shiwei,gewei,xiaoshu;
baiwei=data_temp[5]>=0x61?(data_temp[5]-0x57):(data_temp[5]-0x30);
shiwei=data_temp[6]>=0x61?(data_temp[6]-0x57):(data_temp[6]-0x30);
gewei=data_temp[7]>=0x61?(data_temp[7]-0x57):(data_temp[7]-0x30);
xiaoshu=data_temp[9]>=0x61?(data_temp[9]-0x57):(data_temp[9]-0x30);
zs_temp=baiwei*256+shiwei*16+gewei;
xs_temp=xiaoshu*10/16;
B_temp=0;
P0=SMG_table[zs_temp/100];
delay(1);
B_temp=1;
P0=0xFF;
S_temp=0;
P0=SMG_table[zs_temp%100/10];
delay(1);
S_temp=1;
P0=0xFF;
G_temp=0;
P0=SMG_table2[zs_temp%10];
delay(1);
G_temp=1;
P0=0xFF;
X_temp=0;
P0=SMG_table[xs_temp];
delay(1);
X_temp=1;
P0=0xFF;
if(zs_temp>90)
jingbao=0;
}
}
多看看人家程序怎么写的把
程序大体都不对,串口中断向量是4吧
我是用查询方式,没有用串口中断。