发表于:2007-04-03 19:11:00
303楼
digi
你好,想请教你一个问题,我最近在做一个单片机测温的程序,用的是80C52连接DS18B20,采用两线制,DQ接上拉电阻:5K左右,电源为五伏.我用KEIL C编程,程序如下:
#include <reg51.h>
sbit DQ=P1^1;
#define jump_ROM 0xCC
#define start 0x44
#define read_EEROM 0xBE
unsigned char TMPH,TMPL;
void delay(unsigned int N)
{
int i;
for(i=0;i<N;i++);
}
bit Reset(void)
{
unsigned char deceive_ready;
DQ=0;
delay(30);
DQ=1;
delay(2);
deceive_ready=DQ;
delay(28);
return(deceive_ready);
}
bit read_bit(void)
{
unsigned char i;
DQ=0;
DQ=1;
for(i=0;i<3;i++);
return(DQ);
}
void write_bit(unsigned char bitval)
{
DQ=0;
if(bitval==1)
DQ=1;
delay(4);
DQ=1;
}
void write_byte(unsigned char val)
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
temp=val>>i;
temp=temp&0x01;
write_bit(temp);
delay(1);
}
}
unsigned char read_byte(void)
{
unsigned char i,m,receive_data;
m=1;
receive_data=0;
for(i=0;i<8;i++)
{
if(read_bit())
{
receive_data=receive_data+(m<<i);
}
delay(6);
}
return(receive_data);
}
void main()
{
unsigned int i,j;
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
SCON=0xd8;
PCON=0x00;
TR1=1;
while(1)
{
if(Reset());
{
write_byte(jump_ROM);
write_byte(start);
if(Reset());
{
write_byte(jump_ROM);
write_byte(read_EEROM);
TMPL=read_byte();
TMPH=read_byte();
SBUF=TMPH;
while(TI==0);
TI=0;
SBUF=TMPL;
while(TI==0);
TI=0;
for(i=0;i<1000;i++)
for(j=0;j<100;j++);
}
}
}
}
希望收到温度信号后通过串口发回电脑,我在DEBUG中看汇编的时序好像都没有问题了,但是总是收到0550,也就是85度,我都调整了四天了,比较郁闷,望恢复,还有,你那里有C编写的程序可以给我发一份吗,我比较一下,我的邮箱:zhoujianpingboy@sina.com,谢谢你了,哈哈