#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
***it dula=P2^6;
***it wela=P2^7;
***it wr=P3^6;
***it rd=P3^7;
***it le=P2^5;
uchar flag_uart,flag_time,flag_on,a,num,ad_val;
float ad_vo;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delayms(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=112;j>0;j--);
}
void innit()
{
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=0Xfd;
TL1=0Xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
ET0=1;
}
uchar get_ad()
{
uchar adval;
wr=1;
_nop_();
wr=0;
_nop_();
wr=1;
P1=0xff;
rd=1;
_nop_();
rd=0;
_nop_();
adval=P1;
rd=1;
return adval;
}
void display(uchar value)
{
uchar ge,shi,bai;
bai=value/100;
shi=value%100/10;
ge=value%100%10;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0x7e;
wela=0;
delayms(5);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0x7d;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0x7b;
wela=0;
delayms(5);
}
void main()
{
wela=1;
P0=0x7f;
wela=0;
innit();
while(1)
{
if(flag_uart==1)
{
flag_uart=0;
ES=0;
TI=1;
switch(flag_on)
{
case 0:puts("turn on adn");
TR0=1;
break;
case 1:puts("turn off adn");
TR0=0;
break;
case 2:puts("errorn");
break;
}
while(!TI);
TI=0;
ES=1;
}
if(flag_time==1)
{
flag_time=0;
ad_val=get_ad();
ad_vo=(float)ad_val*5.0/256.0;
ES=0;
TI=1;
printf("the voltage is %fvn",ad_vo);
while(!TI);
TI=0;
ES=1;
}
display(ad_val);
}
}
void time_0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num==20)
{
num=0;
flag_time=1;
}
}
void ser() interrupt 4
{
flag_uart=1;
RI=0;
a=SBUF;
if(a==1)
flag_on=0;
else if(a==2)
flag_on=1;
else
flag_on=2;
}
程序如上 ,烧入板中出现如下问题:
1.上位机发送指令,单片机不能返回相应内容。
2.数码管不能显示AD输出值(不能进入串口中断所致)。
问题还是处在串口通信这块,看半天没看出来,求高手指教,
|