请教高手关于多路采样温度的问题DS18B20

hugang1984 2012-03-27 02:22:58
写得不好,请谅解,自己按照自己的想法来写的,怎么读出来全是"I50"那些地方不对请高手指点迷津!!!!
#include <reg52.h>
#include <math.h>
#include <1602.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="0123456789";
uchar c,sn1,sn2,sn3,sn4;
void mathrom();
uint read_temp();
sbit DQ=P3^7;
void delay_18b20(uint z)
{
while(z--);
}
void init_18b20()
{
uchar x=0;
DQ=1;
delay_18b20(8);
DQ=0;
delay_18b20(80);
DQ=1;
delay_18b20(14);
x=DQ;
delay_18b20(20);
}
void write_18b20(uchar date)
{
uchar i=0;
for (i=8;i>0;i--)
{
DQ=0;
DQ=date&0x01;
delay_18b20(5);
DQ=1;
date>>=1;
}
}
uchar read_18b20()
{
uchar i,date;
for (i=8;i>0;i--)
{
DQ=0;
date>>=1;
DQ=1;
if(DQ)
date=date|0x80;
delay_18b20(4);
}
return(date);
}
void mathrom(uchar z)
{
init_18b20();
delay_18b20(10);
write_18b20(0x33);
switch(z)
{ case 1:sn1=read_18b20();break;
case 2:sn2=read_18b20();break;
case 3:sn3=read_18b20();break;
case 4:sn4=read_18b20();break;
}
}
void display_wendu()
{
uint temp;
uchar shi,ge,xiaoshu;
temp=read_temp();
shi=temp/100;
ge=temp/10%10;
xiaoshu=temp%10;
write_1602com(0x80+c);
write_1602date(0x30+shi);
write_1602com(0x80+1+c);
write_1602date(0x30+ge);
write_1602com(0x80+2+c);
write_1602date(0x30+xiaoshu);
}
uint read_temp()
{
uchar a=0,b=0,temp_value=0;
uint value;
init_18b20();
write_18b20(0xcc);
write_18b20(0x44);
init_18b20();
write_18b20(0x55);
write_18b20(0xbe);
a=read_18b20();
b=read_18b20();
value=b;
value<<=8;
value=value|a;
temp_value=value*0.0625;
value=temp_value*10+0.5;
return(value);
}
void main()
{
init_1602();
while(1)
{
init_18b20();
mathrom(1);
write_18b20(0x55);
display_wendu();
delay(1000);
c=0;
c=c+3;
init_18b20();
mathrom(2);
write_18b20(0x55);
display_wendu();
delay(1000);
c=0;
c=c+6;
init_18b20();
mathrom(3);
write_18b20(0x55);
display_wendu();
delay(1000);
c=0;
c=c+9;
init_18b20();
mathrom(4);
write_18b20(0x55);
display_wendu();
delay(1000);
c=0;
}
}
下面是1602的宏定义
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table1[]="value:";
sbit lcden=P2^2;
sbit lcdwrite=P2^1;
sbit lcdrs=P2^0;
void delay(uint z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_1602com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_1602date(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_1602()
{
lcden=0;
lcdwrite=0;
write_1602com(0x38);
write_1602com(0x01);
write_1602com(0x0c);
write_1602com(0x06);
}
全部只显示"I50"这个数
...全文
104 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hugang1984 2012-03-29
  • 打赏
  • 举报
回复
没人知道???
hugang1984 2012-03-27
  • 打赏
  • 举报
回复
我是用proteus6.9来显示的,是不是在每个DS18B20都要设置?

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧