大家看看哪里出错了,本人实在找不出来!拜托
#include<reg52.h>#include<intrins.h>
sbit rs=P2^4; //LCD液晶引脚定义,p0接DB0_DB7
sbit rw=P2^7;
sbit ep=P2^6;
sbit ale=P2^0; //ADC0809引脚定义
sbit start=P3^7;
sbit oe=P3^5;
sbit eoc=P3^6;
sbit adda=P2^5;
sbit addb=P2^2;
sbit addc=P2^3;
//sbit 2-8=P0^0;
//sbit 2-7=P0^1;
//sbit 2-6=P0^2;
//sbit 2-5=P0^3;
//sbit 2-4=P0^4;
//sbit 2-3=P0^5;
//sbit 2-2=P0^6;
//sbit 2-1=P0^7;
sbit light=P3^0; //报警输出
sbit DQ=P3^3; //DS18B20输入脚定义
sbit DE=P3^1;
unsigned char tempL=0;
unsigned char tempH=0;
float temperature;
unsigned char code dis1[]={"WORK START"}; //状态显示
unsigned char code dis2[]={"ALCOHOL TEST"};
unsigned char code dis3[]={"mg/100ml"};
unsigned char code dis4[]={"SAFE DRIVING"};
unsigned char code dis5[]={"DRINK-DRIVING"};
unsigned char code dis6[]={"DRUNK DRIVING"};
void delay(unsigned char ms) //延时1
{ unsigned char i,j;
while(ms--)
{ for(i=0;i<250;i++)
for(j=0;j<50;j++)
{
_nop_();
_nop_();
}
}
}
void delay2(unsigned int time ) //延时2
{
unsigned int n;
n=0;
while (n<time)
{ n++;}
return;
}
bit lcd_bz() //LCD判忙
{
bit result;
rs=0;
rw=1;
ep=1;
_nop_();
_nop_();
result=(bit)(P0&0x80);
ep=0;
return result;
}
lcd_wcmd(unsigned char cmd) //写指令数据到LCD
{
while(lcd_bz())
rs=0;
rw=0;
ep=0;
_nop_();
_nop_();
P0=cmd;
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
ep=0;
}
lcd_pos(unsigned char pos) //设定显示位置
{
lcd_wcmd(pos | 0x80);
}
lcd_wdat(unsigned char dat) //写入显示数据到
{
while(lcd_bz());
rs=1;
rw=0;
ep=0;
P0=dat;
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
ep=0;
}
lcd_init() //LCD初始化
{
lcd_wcmd(0x38);
delay(1);
lcd_wcmd(0x0c);
delay(1);
lcd_wcmd(0x06);
delay(1);
lcd_wcmd(0x01);
delay(1);
}
Init_DS18B20(void) //DS18B20初始化
{
unsigned char x=0;
DQ=1;
delay2(8);
DQ=0;
delay2(85);
DQ=1;
delay2(14);
delay2(20);
}
ReadOneChar(void) // 向DS18B20读一字节数据
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i>0;i--)
{
DQ=1;
delay2(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay2(4);
}
return(dat);
}
WriteOneChar(unsigned char dat) //向DS18B20写一字节数
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0X01;
delay2(5);
DQ=1;
dat>>=1;
}
delay2(4);
}
ReadTemperature(void) //向DS18B20读温度值
{
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
delay2(125);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
tempL=ReadOneChar();
tempH=ReadOneChar();
temperature=((tempH*256)+tempL)*0.0625;
delay2(200);
return(temperature);
}
main()
{
float x,y,k;
unsigned int da,n,temp1,temp2,i,gw,sw,bw,kw,xw;
light=0; //数据初始化
lcd_init();
delay(3);
lcd_pos(0);
i=0;
while(dis1[i]!='\0') //显示开始工作
{
lcd_wdat(dis1[i]);
i++;
}
lcd_pos(0x41);
i=0;
while(dis2[i]!='\0') //显示开始测试
{
lcd_wdat(dis2[i]);
i++;
}
delay(32);
while(1) //无限循环测试
{
lcd_init(); //初始化LCD
ale=0; //初始化ADC0809
start=0;
adda=0; //定义TGS822信号输入引脚号
addb=0;
addc=0;
_nop_();
ale=1; //启动 ADC0809
start=1;
_nop_();
start=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(eoc==0); //判断ADC0809是否转换完毕
oe=1; //打开ADC0809数据锁存
_nop_();
da=P1; //读取转换后数据
oe=0;
y=(2550/da-10)/9.27; //代入传感器性质公式进行计算
if(y>2.68)
x=61.61-4.08*y;
else if(y>2.38)
x=139.3-33.3*y;
else if(y>2.23)
x=218.7-66.7*y;
else if(y>2.128)
x=288.6-98*y;
else if(y>2)
x=262.88-85.9*y;
else if(y>1.89)
x=254.5-81.8*y;
else if(y>1.62)
x=450-185.2*y;
else if(y>1.209)
x=347.1-121.7*y;
else if(y>0.822)
x=816.5-516.8*y;
else if(y>0.635)
x=1279.2-1069.5*y;
else
x=2238.7-2580.6*y;
if(da<=17)
x=0;
n=(int)(x/0.23698);
gw=n%10+0x30; //取出所得数据个、十、百、千每位上的 数,并转换对应的ASCII表中数据便
temp1=n/10;
sw=temp1%10+0x30;
temp2=temp1/10;
bw=temp2%10+0x30;
kw=temp2/10+0x30;
lcd_pos(0x84); //设定小数点在LCD显示位置
lcd_wdat(0x2e); //输入小数点
if(n>=1000) //判断数据大小,按位输出
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);
lcd_pos(0x82);
lcd_wdat(bw);
lcd_pos(0x81);
lcd_wdat(kw);
}
else if(n>=100)
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);
lcd_pos(0x82);
lcd_wdat(bw);
}
else
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);
}
lcd_pos(0x86); //输出单位
i=0;
while(dis3[i]!='\0')
{
lcd_wdat(dis3[i]);
i++;
}
n=n/10;
if(n<20) //判断并显示测试结果
{
lcd_pos(0x42);
i=0;
while(dis4[i]!='\0')
{
lcd_wdat(dis4[i]);
i++;
}
delay(16);
}
else if(n>=20 && n<80)
{
lcd_pos(0x42);
i=0;
while(dis5[i]!='\0')
{
lcd_wdat(dis5[i]);
i++;
}
delay(16);
}
else
{
light=1; //亮灯报警
lcd_pos(0x42);
i=0;
while(dis6[i]!='\0')
{
lcd_wdat(dis6[i]);
i++;
}
delay(16);
light=0;
}
lcd_init(); //初始化ADC0809
k=ReadTemperature(); //读取DS18B20数据
n=(int)(100*k);
xw=n%10; //处理数据,便于输出
n=n/10;
gw=n%10+0x30;
temp1=n/10;
sw=temp1%10+0x30;
temp2=temp1/10;
bw=temp2%10+0x30;
kw=temp2/10+0x30;
n=n/10;
lcd_pos(0x84); //设定小数点在LCD显示位置
lcd_wdat(0x2e); //输出小数点
if(n>=100) //判断数据大小,按位输出
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);
lcd_pos(0x82);
lcd_wdat(bw);
lcd_pos(0x81);
lcd_wdat(kw);
}
else if(n>=10)
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);
lcd_pos(0x82);
lcd_wdat(bw);}
else
{
lcd_pos(0x85);
lcd_wdat(gw);
lcd_pos(0x83);
lcd_wdat(sw);}
lcd_pos(0x86);
lcd_wdat(0xdf);
lcd_pos(0x87);
lcd_wdat(0x43);
delay(16);
}
}