1602显示ds18b20温度
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include <intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd_data P0
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit psb=P3^2;
sbit dq=P3^7;
uchar dis1[] = {"Temp:"};
uchar ds18b20init();
void ds18b20write(uchar dat);
uchar ds18b20read();
int ds18b20readtemp();
void delay(uint i);
void write_com(uchar com);
void write_data(uchar dat);
void LcdInit();
void delay(uint i)
{
while(i--);
}
uchar ds18b20init()
{
uchar date=0;
dq=1;
delay(10);
dq=0;
delay(60);
dq=1;
delay(10);
date=dq;
delay(10);
return date;
}
void ds18b20write(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
dq=0;
delay(2);
dq=1;
delay(2);
dq=date&0x01;
dq=1;
delay(6);
date >>= 1;
}
}
uchar ds18b20read()
{
uchar dat=0;
uchar j;
for(j=0;j<8;j++)
{
dq=0;
delay(1);
dat>>=1;
dq=1;
delay(2);
if(dq)
dat|=0x80;
delay(5);
}
return dat;
}
int ds18b20readtemp()
{
int temp=0;
uchar templ,temph;
ds18b20init();
delay(100);
ds18b20write(0xcc);
ds18b20write(0x44);
ds18b20init();
delay(100);
ds18b20write(0xcc);
ds18b20write(0xbe);
templ=ds18b20read();
temph=ds18b20read();
temp=temph;
temp<<=8;
temp|=templ;
temp=temp*0.0625*100;
return (temp);
}
void write_com(uchar com)
{
rs=0;
rw=0;
e=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_data(uchar dat)
{
rs=1;
rw=0;
e=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void LcdInit()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display()
{
uchar i;
int a1,a2,a3,a4,a5;
int temp=0;
temp= ds18b20readtemp();
a1 = temp/10000;
a2 = temp%10000/1000;
a3 = temp%1000/100;
a4 = temp%100/10;
a5 = temp%10;
write_com(0x00+0x80);
for(i=0;i<5;i++)
{
write_data(dis1);
}
write_com(0x80+0x05);
write_data(a1+0x30);
write_com(0x80+0x06);
write_data(a2+0x30);
write_com(0x80+0x07);
write_data(a3+0x30);
write_com(0x80+0x08);
write_data('.');
write_com(0x80+0x09);
write_data(a4+0x30);
write_com(0x80+0x0a);
write_data(a5+0x30);
write_com(0x80+0x0b);
write_data(0xdf);
write_com(0x80+0x0c);
write_data('C');
}
void main()
{
LcdInit();
while(1)
{
ds18b20readtemp();
display();
}
}
一直显示的是000.00,为什么啊
#include <intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd_data P0
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit psb=P3^2;
sbit dq=P3^7;
uchar dis1[] = {"Temp:"};
uchar ds18b20init();
void ds18b20write(uchar dat);
uchar ds18b20read();
int ds18b20readtemp();
void delay(uint i);
void write_com(uchar com);
void write_data(uchar dat);
void LcdInit();
void delay(uint i)
{
while(i--);
}
uchar ds18b20init()
{
uchar date=0;
dq=1;
delay(10);
dq=0;
delay(60);
dq=1;
delay(10);
date=dq;
delay(10);
return date;
}
void ds18b20write(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
dq=0;
delay(2);
dq=1;
delay(2);
dq=date&0x01;
dq=1;
delay(6);
date >>= 1;
}
}
uchar ds18b20read()
{
uchar dat=0;
uchar j;
for(j=0;j<8;j++)
{
dq=0;
delay(1);
dat>>=1;
dq=1;
delay(2);
if(dq)
dat|=0x80;
delay(5);
}
return dat;
}
int ds18b20readtemp()
{
int temp=0;
uchar templ,temph;
ds18b20init();
delay(100);
ds18b20write(0xcc);
ds18b20write(0x44);
ds18b20init();
delay(100);
ds18b20write(0xcc);
ds18b20write(0xbe);
templ=ds18b20read();
temph=ds18b20read();
temp=temph;
temp<<=8;
temp|=templ;
temp=temp*0.0625*100;
return (temp);
}
void write_com(uchar com)
{
rs=0;
rw=0;
e=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_data(uchar dat)
{
rs=1;
rw=0;
e=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void LcdInit()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display()
{
uchar i;
int a1,a2,a3,a4,a5;
int temp=0;
temp= ds18b20readtemp();
a1 = temp/10000;
a2 = temp%10000/1000;
a3 = temp%1000/100;
a4 = temp%100/10;
a5 = temp%10;
write_com(0x00+0x80);
for(i=0;i<5;i++)
{
write_data(dis1);
}
write_com(0x80+0x05);
write_data(a1+0x30);
write_com(0x80+0x06);
write_data(a2+0x30);
write_com(0x80+0x07);
write_data(a3+0x30);
write_com(0x80+0x08);
write_data('.');
write_com(0x80+0x09);
write_data(a4+0x30);
write_com(0x80+0x0a);
write_data(a5+0x30);
write_com(0x80+0x0b);
write_data(0xdf);
write_com(0x80+0x0c);
write_data('C');
}
void main()
{
LcdInit();
while(1)
{
ds18b20readtemp();
display();
}
}
一直显示的是000.00,为什么啊
在temp= ds18b20readtemp();后面给temp赋任意值,看是否能正确显示,来判断显示和数据转换有没有问题,这两个没问题的话,那就是读取传感器部分出错。
赋任意值可以正确显示
那就主要查传感器部分吧,这个传感器没用过,好好看看资料,注意操作时序