初级会员
- 积分
- 124
- 金钱
- 124
- 注册时间
- 2017-2-8
- 在线时间
- 28 小时
|
1金钱
#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=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[i]);
}
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,为什么啊
|
最佳答案
查看完整内容[请看2#楼]
你的程序只适合实验,实际使用中问题很大,主要问题是等待转换完成的时间750mS你浪费了。
看看我这个程序,保证能用,使用了状态机,可以充分利用单片机资源,不浪费。
http://blog.sina.com.cn/u/2050455692
|