帮忙解答温度测量报警器程序
帮忙解答这程序出了什么问题,为什么LCD不工作,什么都不行
#include<reg52.h>
#include <stdio.h>
# define uchar unsigned char
#define uint unsigned int
uchar DQ;
uchar k;
sbit ds=P3^7; //温度传感器引脚;
sbit beep=P3^6; //蜂鸣器
uint temp;
float ftemp;
uchar g1=22,g2=24,d1=20,d2=18;
sbit b=P2^6; //定义按键;
sbit a=P2^7; //定义按键;
sbit E=P2^5;//使能信号;
sbit RW=P2^4;//读写选择 写指令 RS=0;RW=0;D0~D7指令码;E高脉冲;
sbit RS=P2^3;//数据/命令选择 写数据 RS=1;RW=0;D0~D7数据;E高脉冲
uchar code table1[]="DS18B20 OK ! ";
uchar code table2[]="TEMP IS: . ";
//自定义字符显示'C ;
uchar code table[8] = {0x16,0x09,0x08,0x08,0x08,0x09,0x06,0x00} ;
uchar code table3[]="DS18B20 ERR0R !";
uchar code table4[]="PLEASE CHECK !";
uchar code table7[]="set standard ";
uchar code table8[]=" tempreture ! ";
uchar code table5[]="G1= ,G2= .";
uchar code table6[]="D1= ,D2= .";
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //液晶写指令;
{
P0=com;
RS=0;
RW=0;
E=1;
delay(5);
E=0;
delay(5);
}
void write_date(uchar date) //液晶写数据;
{
P0=date;
RS=1;
RW=0;
E=1;
delay(5);
E=0;
delay(5);
}
void chushi()
{
uchar f;
write_com(0x80);
f=0;
while(table7[f]!=0)
{
write_date(table7[f]);
f++;
}
write_com(0x80+0x40);
f=0;
while(table8[f]!=0)
{
write_date(table8[f]);
f++;
}
}
void init() //液晶初始化状态;
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
chushi();
}
dsreset(void) //DS18B20复位 初始化函数
{
uint i;
ds=0;
i=103;
while(i>0) i--;
ds=1;
i=4;
while(i>0) i--;
DQ=ds;
i=4;
while(i>0) i--;
return DQ;
}
bit tempreadbit(void) //读一位数据函数
{
uint i;
bit dat;
ds=0;i++;
ds=1;i++;i++;
dat=ds;
i=8;while(i>0) i--;
return(dat);
}
uchar tempread(void) //读一个字节数据函数
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void tempwritebyte(uchar dat) //写一个字节数据函数
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //写1
{
ds=0;
i++;i++;
ds=1;
i=8;while(i>0) i--;
}
else //写0
{
ds=0;
i=8;while(i>0) i--;
ds=1;
i++;i++;
}
}
}
void tempchange(void) //DS18B20 开始获取温度并转换
{
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0x44);
}
uint get_temp() //读取寄存器中存储的温度数据
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread();
b=tempread();
temp=b;
temp<<=8;
temp=temp|a;
ftemp=temp*0.0625;
temp=ftemp*10+0.5;
ftemp=ftemp+0.05;
return temp;
}
void init_com(void) //串口初始化;
{
PCON=0x00;
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
}
void comm(char *parr) //串口发送;
{
do
{
SBUF=*parr++;
while(!TI);
TI=0;
}while(*parr!='\0');
}
void zdy() //自定义字符显示’C;
{ uchar i;
write_com(0x40);
for(i=0;i<9;i++)
write_date(table[i]);
write_com(0x80+0x40+14);
write_date(0x00);
}
void ok() //温度传感器存在显示温度;
{
uchar i;
write_com(0x80);
for(i=0;i<16;i++)
write_date(table1[i]);
write_com(0x80+0x40);
for(i=0;i<16;i++)
write_date(table2[i]);
}
void error() //温度传感器不存在 显示错误;
{
uchar i;
write_com(0x80);
for(i=0;i<16;i++)
write_date(table3[i]);
write_com(0x80+0x40);
for(i=0;i<16;i++)
write_date(table4[i]);
}
void display() //液晶显示温度;
{
write_com(0x80+0x40+10);
write_date(temp/100+0x30);
write_com(0x80+0x40+11);
write_date(temp%100/10+0x30);
write_com(0x80+0x40+13);
write_date(temp%100%10+0x30);
}
void fmq(uint i) //蜂鸣器滴答;
{
beep=0;
delay(i);
beep=1;
}
void dywd(uchar add,uchar date) //定义温度 液晶显示;
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void di() //蜂鸣器滴答;
{
beep=0;
delay(100);
beep=1;
}
uint keyscan() //按键扫描,调标准温度;
{
uchar f;
if(a==0)
{
delay(10);
if(a==0)
{
k++;
while(!a);
di();
write_com(0x80);
f=0;
while(table5[f]!=0)
{
write_date(table5[f]);
f++;
}
write_com(0x80+0x40);
f=0;
while(table6[f]!=0)
{
write_date(table6[f]);
f++;
}
dywd(4,g1);
dywd(12,g2);
dywd(4+0x40,d1);
dywd(12+0x40,d2);
if(k==1)
{
write_com(0x80+5);
write_com(0x0f);
}
if(k==2)
{
write_com(0x80+13);
write_com(0x0f);
}
if(k==3)
{
write_com(0x80+0x40+5);
write_com(0x0f);
}
if(k==4)
{
write_com(0x80+0x40+13);
write_com(0x0f);
}
if(k==5)
{
write_com(0x0c);
}
}
}
if(k!=0)
{
if(b==0)
{
delay(10);
if(b==0)
{
while(!b);
di();
if(k==1)
{
g1--;
if(g1==0)
g1=60;
dywd(4,g1);
write_com(0x80+5);
}
if(k==2)
{
g2--;
if(g1==0)
g2=60;
dywd(12,g2);
write_com(0x80+13);
}
if(k==3)
{
d1--;
if(g1==0)
d1=60;
dywd(4+0x40,d1);
write_com(0x80+0x40+5);
}
if(k==4)
{
d2--;
if(g1==0)
d2=60;
dywd(12+0x40,d2);
write_com(0x80+0x40+13);
}
}
}
}
return g1;
return g2;
return d1;
return d2;
}
void warn(uint temp) //温度报警;
{
uchar a=d2,b=d1,c=g1,d=g2;
if(temp<=a*10) //灯 1,2 亮;
{
P1=0xfc;
fmq(250);
display();
}
else if((temp<b*10)&&(temp>=a*10)) //灯 3 亮;
{
P1=0xfb;
fmq(20);
display();
}
else if((temp<c*10)&&(temp>=a*10)) //灯 4,5 亮;
{
P1=0xe7;
display();
beep=1;
}
if((temp>c*10)&&(temp<d*10)) //灯 6 亮;
{
P1=0xdf;
fmq(20);
display();
}
else if(temp>=d*10) //灯 7,8 亮;
{
P1=0x3f;
fmq(250);
display();
}
}
void main() //主函数
{
uchar buff[4];
init_com(); //串口初始化;
init(); //液晶初始化状态;
dsreset(); //DS18B20复位 初始化函数;
tempchange(); //DS18B20 开始获取温度并转;
get_temp(); //读取寄存器中存储的温度数据
while(1)
{
keyscan();
if(k==5)
{
ok(); //温度传感器存在程序;
do
{
tempchange();
sprintf(buff,"%f",ftemp);
comm(buff); //串口发送;
zdy();
warn(get_temp());
}
while(!DQ);
error(); //温度传感器不存在程序;
do
{
fmq(1);
dsreset();
P1=0x00;
}
while(DQ);
}
}
}