感觉DS18B20没把数据显示到LCD1602中,怎么回事呢?

#include
#include
#define uint unsigned int
#define uchar unsigned char
#define data 1000
#define lcd_date P0
sbit lcd_en=P2^2;
sbit lcd_rw=P2^1;
sbit lcd_rs=P2^0;
sbit key0=P1^0;
sbit key1=P1^1;
sbit led=P1^7;
uchar code lcdtable[]={"0123456789- "};
uchar code error[12]={"no facility"};
uchar rom_id[3][8]={ {0x28,0xc4,0x14,0x11,0x00,0x00,0x00,0x89},
{0x28,0xc4,0x14,0x10,0x00,0x00,0x00,0x06}};

uchar dispbuf[8];

uchar dispbuf1[8];
uint data1=50000;
uchar tem1,tem2,temt1,temt2;
uint temper1;//温度寄存器
uint temper11;
uint temper22;
uint temper2;
uint tempert1;//温度阀值R-min
uint tempert2;//温度阀值R-max
uint tempert11;
uint tempert22;
#define NOP3() nop();_nop_();_nop_()
sbit DQ=P3^3;
bit flag_init; //DS18B20是否存在标志

void delay_us(unsigned int tt)

{
while(tt--)
{
;
}
}
void delay_ms(unsigned int tms)
{
unsigned char i;
while(tms--)
for(i=123;i>0;i--)
;
}

void Write_com(uchar com)
{
lcd_rs=0;
lcd_rw=0;
lcd_date=com;
delay_ms(2);
lcd_en=1;
delay_ms(2);
lcd_en=0;
}
void Write_dat(uchar dat)
{
lcd_rs=1;
lcd_rw=0;
lcd_date=dat;
delay_ms(2);
lcd_en=1;
delay_ms(2);
lcd_en=0;
}

void lcd_init()
{
lcd_rw=0;
lcd_en=0;
delay_ms(15);
Write_com(0x38);
delay_ms(5);
Write_com(0x38);
delay_ms(5);
Write_com(0x38);//显示模式设置
Write_com(0x08);//显示关闭
Write_com(0X01);//清屏
Write_com(0x06);//显示光标移动设置
Write_com(0X0c);//显示开及光标设置
}

/****DS18B20初始化**************************************/
void reset()
{
flag_init=1;
EA=0;
DQ=1;
NOP3();
DQ=0;
delay_us(60);//480~960us 556
DQ=1;
delay_us(5);//60us
flag_init=DQ;
delay_us(25);//241us
EA=1;
}
/****写一个字节函数*************************************/
void Write_Byte(uchar dat)
{
uchar i;
DQ=1;
EA=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=(bit)(dat&0x01);
delay_us(5);//61us
DQ=1;
dat>>=1;
}
EA=1;
}

/*****读数据函数*****************************/
uchar Read_Byte()
{
uchar i,date;
date=0;
DQ=1;
EA=0;//关中断
for(i=8;i>0;i--)
{
DQ=0;
date>>=1;
DQ=1;
NOP3();
if(DQ)
{
date|=0x80;
}
delay_us(5);//61us
}
EA=1;//开中断
return(date);
}
void match_rom(uchar rom_id[])
{
uchar n;
reset();
Write_Byte(0x33);
for(n=0;n<8;n++)
rom_id[n]=Read_Byte();
}

void get_tem()
{

uchar teml,temh;
reset();

Write_Byte(0xcc);
Write_Byte(0x44);//启动温度转换
delay_ms(800);//750ms
match_rom(0);
Write_Byte(0xbe);//读取温度转换结果
teml=Read_Byte();
temh=Read_Byte();
temper11=(temh*256+teml);
if(temper11&0x8000)
{

temper1=(~temper11+1)*0.625;
tem1=1;
}
else
{
temper1=(temh*256+teml)*0.625;
tem1=0;
}
}
void get1_tem()
{

uchar teml,temh;
reset();
Write_Byte(0xcc);
Write_Byte(0x44);//启动温度转换
delay_ms(800);//750ms
match_rom(1);
Write_Byte(0xbe);//读取温度转换结果
teml=Read_Byte();
temh=Read_Byte();
temper22=(temh*256+teml);
if(temper22&0x8000)
{
temper2=(~temper22+1)*0.625;
tem2=1;
}
else
{
temper2=(temh*256+teml)*0.625;
tem2=0;
}
}

update()
{
if(tem1)
{
dispbuf[0]=10;
if(temper1/100)
dispbuf[1]=temper1/100;
else
dispbuf[1]=11;
dispbuf[2]=temper1%100/10;
dispbuf[3]=temper1%10;
}
else
{
if(temper1/1000)
{
dispbuf[0]=temper1/1000;
dispbuf[1]=temper1%1000/100;
}
else
{
dispbuf[0]=11;
if(temper1/100)
dispbuf[1]=temper1/100;
else
dispbuf[1]=11;
}
dispbuf[2]=temper1%100/10;
dispbuf[3]=temper1%10;

}
if(tem2)
{
dispbuf[4]=10;
if(temper2/100)
dispbuf[5]=temper2/100;
else
dispbuf[5]=11;
dispbuf[6]=temper2%100/10;
dispbuf[7]=temper2%10;
}
else
{
if(temper2/1000)
{
dispbuf[4]=temper2/1000;
dispbuf[5]=temper2%1000/100;
}
else
{
dispbuf[4]=11;
if(temper2/100)
dispbuf[5]=temper2/100;
else
dispbuf[5]=11;
}
dispbuf[6]=temper2%100/10;
dispbuf[7]=temper2%10;

}
}
update_1()
{

if(tempert1/1000)
{
dispbuf1[0]=tempert1/1000;
dispbuf1[1]=tempert1%1000/100;
}
else
{
dispbuf1[0]=11;
if(tempert1/100)
dispbuf1[1]=tempert1/100;
else
dispbuf1[1]=11;
}
dispbuf1[2]=tempert1%100/10;
dispbuf1[3]=tempert1%10;

if(tempert2/1000)
    {
        dispbuf1[4]=tempert2/1000;
        dispbuf1[5]=tempert2%1000/100;
    }
else
    {
        dispbuf1[4]=11;
        if(tempert2/100)
            dispbuf1[5]=tempert2/100;
        else
            dispbuf1[5]=11;
    }
dispbuf1[6]=tempert2%100/10;
dispbuf1[7]=tempert2%10;

}

ds_inti()
{

led=0;
tempert1=100;
tempert2=600;
}

void disp()
{
Write_com(0xc0);
Write_dat('1');
Write_dat(':');
Write_dat(lcdtable[dispbuf[0]]);
Write_dat(lcdtable[dispbuf[1]]);
Write_dat(lcdtable[dispbuf[2]]);
Write_dat('.');
Write_dat(lcdtable[dispbuf[3]]);
Write_com(0xc9);
Write_dat('2');
Write_dat(':');
Write_dat(lcdtable[dispbuf[4]]);
Write_dat(lcdtable[dispbuf[5]]);
Write_dat(lcdtable[dispbuf[6]]);
Write_dat('.');
Write_dat(lcdtable[dispbuf[7]]);

Write_com(0x80);
Write_dat('L');
Write_dat(':');
Write_dat(lcdtable[dispbuf1[0]]);
Write_dat(lcdtable[dispbuf1[1]]);
Write_dat(lcdtable[dispbuf1[2]]);
Write_dat('.');
Write_dat(lcdtable[dispbuf1[3]]);
Write_com(0x89);
Write_dat('H');
Write_dat(':');
Write_dat(lcdtable[dispbuf1[4]]);
Write_dat(lcdtable[dispbuf1[5]]);
Write_dat(lcdtable[dispbuf1[6]]);
Write_dat('.');
Write_dat(lcdtable[dispbuf1[7]]);

}
key() interrupt 3
{
TH1=(65536-data1)>>8;
TL1=(65536-data1)%256;
if(key0==0)
{
tempert1+=10;
if(tempert1>1280)
tempert1=0;
while(key0==0);
}
if(key1==0)
{
tempert2-=10;
if(tempert2>1280)
tempert2=1280;
while(key1==0);
}
}
void init_mcu()
{
TMOD=0x01;
TH1=(65536-data1)>>8;
TL1=(65536-data1)%256;

ET1=1;TR1=1;
EA=1;
}
void alarm()
{

    if((temper1<tempert1||temper1>tempert2)||(temper2<tempert1||temper2>tempert2))
{       led=1;}
    else
    {   led=0;} 

}
// 主函数
void main()
{

uchar i;
init_mcu();
ds_inti();
// close_led();
lcd_init();

while(1)
{
    reset();
    if(flag_init)//不存在时
    {
    Write_com(0x80);
        for(i=0;i<12;i++)
        {
            Write_dat(error[i]);
        }
    }
    else//存在时
    {
        get_tem();
        get1_tem();
        update();
        update_1();
        disp();
        alarm();
    }
}

}


1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐