请教各位高手,为什么这个程序无论PC发什么,结果都是Error!
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6; //申明U1锁存器的锁存端
sbit wela=P2^7; //申明U2锁存器的锁存端
sbit sda=P2^0; //PCF8591串行数据线
sbit scl=P2^1; //PCF8591串行时钟线
uchar flag,a;
unsigned char flag_uart,flag_time,flag_on,a,i,t0_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 xms)
{
uint i,j;
for(i=xms;i>0;i--) //数码管延时
for(j=110;j>0;j--);
}
void delay() //IIC总线延时
{;;}
void init() //IIC总线数据线时钟线初始化
{
sda=1;
delay();
scl=1;
delay();
}
void start()//开始条件
{
sda=1;
delay();
scl=1;
delay();
sda=0;
}
void stop()//终止条件
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void response()//应答条件
{
uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<255))i++;
scl=0;
delay();
}
void write_byte(uchar date)//向PCF8591写一个字节
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()//从PCF8591读出一个字节
{
uchar i=0,k=0;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
delay();
return k;
}
uchar read_channel(uchar channel)//PCF通道选择函数
{
uchar num;
start();
write_byte(0x90);
response();
write_byte(0x40|channel);
response();
start();
write_byte(0x91);
response();
num=read_byte();
stop();
return num;
}
void serinit()//初始化
{
TMOD=0x21;
// SCON=0x50;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ET0=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
}
void display(uchar value) //显示子函数
{
uchar bai,shi,ge;
bai=value/100;
shi=value%100/10;
ge=value%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()
{
init();
serinit();
while(1)
{
if(flag_uart==1)
{
flag_uart=0;
ES=0;
TI=1;
switch(flag_on)
{
case 0: puts("Turn on ad!\n");
TR0=1;
break;
case 1: printf("Turn off ad!\n");
TR0=0;
break;
case 2: puts("Error!\n");
break;
}
while(!TI);
TI=0;
ES=1;
}
if(flag_time==1)
{
flag_time=0;
ad_val=read_channel(2);
ad_vo=(float)ad_val*5.0/256.0;
ES=0;
TI=1;
printf("The voltage is %fV\n",ad_vo);
while(!TI);
TI=0;
ES=1;
}
display(ad_val);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0_num++;
if(t0_num==20)
{
t0_num=0;
flag_time=1;
}
}
void ser() interrupt 4
{
RI=0;
a=SBUF;
flag_uart=1;
if(a==1)
flag_on=0;
else if(a==2)
flag_on=1;
else
flag_on=2;
}
|