怎么没有接受到数据呢?
时间:10-02
整理:3721RD
点击:
下面是我的程序:(望各位帮忙解答,不吝赐教,助我成长)
上位机给单片机发送指令(发送1则启动AD并在数码管显示;发送2关闭AD,并显示上一次的值;发送其他则报错“error”)
#include<reg52.h>
//#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
sbit wela=P2^7;
sbit dula=P2^6;
sbit wr=P3^6;
sbit rd=P3^7;
uchar voltage=0;
uchar n,f,r;
uchar flag_on,flag;
uchar number=0;
uchar code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar code table1[]="Turn on ad9";
uchar code table2[]="Turn off ad9";
uchar code table3[]="error9";
void delayms(uchar);
void display(uchar);
void serial_initial();
uchar ad_converter();
void main()
{
serial_initial();
TMOD=0x01;
TH0=(65536-48572)/256;
TL0=(65536-48572)%256;
ET0=1;
TR0=0;
while(1)
{
if(flag==1)
{
TR0=1;
ES=0;
n=0;
while(table1[n]!='9')
{
TI=0;
SBUF=table1[n];
while(TI==0);
TI=0;
n++;
}
flag=0;
ES=1;
//display(voltage);
}
else if(flag==2)
{
TR0=0;
ES=0;
f=0;
while(table2[f]!='9')
{
TI=0;
SBUF=table2[f];
while(TI==0);
TI=0;
f++;
}
flag=0;
ES=1;
//display(voltage);
}
else if(flag==3)
{
r=0;
ES=0;
while(table3[r]!='9')
{
TI=0;
SBUF=table3[r];
while(TI==0);
TI=0;
r++;
}
flag=0;
ES=1;
}
else if(flag==0);
display(voltage);
}
}
void delayms(uchar ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void serial_initial()
{
SCON=0x40;
REN=1;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
EA=1;
ES=1;
TR1=1;
//ET1=1;
}
void display(uchar v)
{
uchar bai=v/100;
uchar shi=v%100/10;
uchar ge=v/10;
dula=1;
P0=table[bai];
dula=0;
P0=0x7f;
wela=1;
P0=0x7e;
wela=0;
delayms(2);
dula=1;
P0=table[shi];
dula=0;
P0=0x7f;
wela=1;
P0=0x7d;
wela=0;
delayms(2);
dula=1;
P0=table[ge];
dula=0;
P0=0x7f;
wela=1;
P0=0x7b;
wela=0;
delayms(2);
}
uchar ad_converter()
{
wr=1;
_nop_();
wr=0;
_nop_();
wr=1;
P1=0xff;
rd=1;
_nop_();
rd=0;
_nop_();
voltage=P1;
rd=1;
return voltage;
}
void serial()interrupt 4
{
if(RI==1)
RI=0;
flag_on=SBUF;
if(flag_on=='1')
flag=1;
else if(flag_on=='2')
flag=2;
else
flag=3;
}
void time0()interrupt 1
{
TH0=(65536-48572)/256;
TL0=(65536-48572)%256;
number++;
if(number==20)
{
number=0;
ad_converter();
}
}
上位机给单片机发送指令(发送1则启动AD并在数码管显示;发送2关闭AD,并显示上一次的值;发送其他则报错“error”)
#include<reg52.h>
//#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
sbit wela=P2^7;
sbit dula=P2^6;
sbit wr=P3^6;
sbit rd=P3^7;
uchar voltage=0;
uchar n,f,r;
uchar flag_on,flag;
uchar number=0;
uchar code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar code table1[]="Turn on ad9";
uchar code table2[]="Turn off ad9";
uchar code table3[]="error9";
void delayms(uchar);
void display(uchar);
void serial_initial();
uchar ad_converter();
void main()
{
serial_initial();
TMOD=0x01;
TH0=(65536-48572)/256;
TL0=(65536-48572)%256;
ET0=1;
TR0=0;
while(1)
{
if(flag==1)
{
TR0=1;
ES=0;
n=0;
while(table1[n]!='9')
{
TI=0;
SBUF=table1[n];
while(TI==0);
TI=0;
n++;
}
flag=0;
ES=1;
//display(voltage);
}
else if(flag==2)
{
TR0=0;
ES=0;
f=0;
while(table2[f]!='9')
{
TI=0;
SBUF=table2[f];
while(TI==0);
TI=0;
f++;
}
flag=0;
ES=1;
//display(voltage);
}
else if(flag==3)
{
r=0;
ES=0;
while(table3[r]!='9')
{
TI=0;
SBUF=table3[r];
while(TI==0);
TI=0;
r++;
}
flag=0;
ES=1;
}
else if(flag==0);
display(voltage);
}
}
void delayms(uchar ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void serial_initial()
{
SCON=0x40;
REN=1;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
EA=1;
ES=1;
TR1=1;
//ET1=1;
}
void display(uchar v)
{
uchar bai=v/100;
uchar shi=v%100/10;
uchar ge=v/10;
dula=1;
P0=table[bai];
dula=0;
P0=0x7f;
wela=1;
P0=0x7e;
wela=0;
delayms(2);
dula=1;
P0=table[shi];
dula=0;
P0=0x7f;
wela=1;
P0=0x7d;
wela=0;
delayms(2);
dula=1;
P0=table[ge];
dula=0;
P0=0x7f;
wela=1;
P0=0x7b;
wela=0;
delayms(2);
}
uchar ad_converter()
{
wr=1;
_nop_();
wr=0;
_nop_();
wr=1;
P1=0xff;
rd=1;
_nop_();
rd=0;
_nop_();
voltage=P1;
rd=1;
return voltage;
}
void serial()interrupt 4
{
if(RI==1)
RI=0;
flag_on=SBUF;
if(flag_on=='1')
flag=1;
else if(flag_on=='2')
flag=2;
else
flag=3;
}
void time0()interrupt 1
{
TH0=(65536-48572)/256;
TL0=(65536-48572)%256;
number++;
if(number==20)
{
number=0;
ad_converter();
}
}
先试试发送数据吧,看能不能发出你指定的数据,这样先确保串口连接是正确的
可以的!。
过来看看,因为不是很懂,