/***********************************************************************************************************************
ds18b20.h 头文件
***********************************************************************************************************************/
#ifndef _DS18B20_H_
#define _DS18B20_H_
#include <reg51.h>
#define led P1
#define ledw P2
#define _DS18B20_H_
#include <reg51.h>
#define led P1
#define ledw P2
extern unsigned char f;
extern void ds18b20_reset();
extern void dela(unsigned char i);
extern void ds18b20_write(unsigned char dat);
extern unsigned char ds18b20_read();
extern unsigned char ds18b20_temp();
extern unsigned char ledtab[];
extern void ds18b20_reset();
extern void dela(unsigned char i);
extern void ds18b20_write(unsigned char dat);
extern unsigned char ds18b20_read();
extern unsigned char ds18b20_temp();
extern unsigned char ledtab[];
#endif
/********************************************************************************************************************
ds18b20.c 文件
********************************************************************************************************************/
#include"ds18b20.h"
#define uchar unsigned char
#define uint unsigned int
#define led P1
#define ledw P2
#define uchar unsigned char
#define uint unsigned int
#define led P1
#define ledw P2
uchar ledtab[]={0xc0,0xf9,0xa4, 0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit dq=P3^7 ;
uchar delay,f;
//uchar f;
sbit dq=P3^7 ;
uchar delay,f;
//uchar f;
void dela(uchar i) //5us t=5+i*2
{
while(--i);
}
{
while(--i);
}
void ds18b20_reset()
{
while(1)
{
dq=1;
dq=0;
delay=250; //540us延时,当delay=1时延时为2us ,T=2*x
while(--delay);
dq=1;
delay=25;
while(--delay);
while(dq==0)
{
delay=220;
while(--delay);
if(dq==1)
break;
}
delay=150;
while(--delay);
break;
}
}
{
while(1)
{
dq=1;
dq=0;
delay=250; //540us延时,当delay=1时延时为2us ,T=2*x
while(--delay);
dq=1;
delay=25;
while(--delay);
while(dq==0)
{
delay=220;
while(--delay);
if(dq==1)
break;
}
delay=150;
while(--delay);
break;
}
}
void ds18b20_write(uchar dat)
{
uchar i=0;
for(i=0;i<8;i++)
{
dq=1;
dq=0;
delay=3;
while(--delay);
dq=dat&0x01;
delay=35;
while(--delay);
dq=1;
dat=dat>>1;
}
}
{
uchar i=0;
for(i=0;i<8;i++)
{
dq=1;
dq=0;
delay=3;
while(--delay);
dq=dat&0x01;
delay=35;
while(--delay);
dq=1;
dat=dat>>1;
}
}
uchar ds18b20_read()
{
uchar i=0,m=0;
for(i=0;i<8;i++)
{
//m>>=1
dq=1;
dq=0;
delay=4;
while(--delay);
dq=1;
delay=4;
while(--delay);
if(dq==1)
m|=0x01<<i;
//m=m|0x80;
delay=25;
while(--delay);
dq=1;
delay=1;
while(--delay);
}
return m;
}
uchar ds18b20_temp()
{
uchar temp=0,temph=0,templ=0,k=0;
ds18b20_reset();
ds18b20_write(0xcc);
ds18b20_write(0x44);
{
uchar i=0,m=0;
for(i=0;i<8;i++)
{
//m>>=1
dq=1;
dq=0;
delay=4;
while(--delay);
dq=1;
delay=4;
while(--delay);
if(dq==1)
m|=0x01<<i;
//m=m|0x80;
delay=25;
while(--delay);
dq=1;
delay=1;
while(--delay);
}
return m;
}
uchar ds18b20_temp()
{
uchar temp=0,temph=0,templ=0,k=0;
ds18b20_reset();
ds18b20_write(0xcc);
ds18b20_write(0x44);
ds18b20_reset();
ds18b20_write(0xcc);
ds18b20_write(0xbe);
ds18b20_write(0xcc);
ds18b20_write(0xbe);
templ=ds18b20_read();
temph=ds18b20_read();
temph=ds18b20_read();
if((temph&0xf80)!=0)
{
f=1;
templ=~templ;
temph=~temph;
k=templ+1;
templ=k;
if(templ>0xff)
{
temph++;
}
}
else
f=0;
templ=templ>>4;
temph=temph<<4;
temp=templ|temph;
return temp;
}
{
f=1;
templ=~templ;
temph=~temph;
k=templ+1;
templ=k;
if(templ>0xff)
{
temph++;
}
}
else
f=0;
templ=templ>>4;
temph=temph<<4;
temp=templ|temph;
return temp;
}
/******************************************************************************************************************
main.c 主文件
*******************************************************************************************************************/
#include<reg51.h>
#include"ds18b20.h"
#define uchar unsigned char
void main()
{
uchar a=0,b=0,c=0,temp=0;
while(1)
{
temp=ds18b20_temp();
a=temp/100;
b=temp0/10;
c=temp;
if(f==1)
{
led=0xbf;
ledw=1;
dela(5000);
ledw=0;
dela(600);
}
led=ledtab[c];
ledw=8;
dela(5000);
ledw=0;
dela(600) ;
#include"ds18b20.h"
#define uchar unsigned char
void main()
{
uchar a=0,b=0,c=0,temp=0;
while(1)
{
temp=ds18b20_temp();
a=temp/100;
b=temp0/10;
c=temp;
if(f==1)
{
led=0xbf;
ledw=1;
dela(5000);
ledw=0;
dela(600);
}
led=ledtab[c];
ledw=8;
dela(5000);
ledw=0;
dela(600) ;
led=ledtab[b];
ledw=4;
dela(5000);
ledw=0;
dela(600) ;
led=ledtab[a];
ledw=2;
dela(5000);
ledw=0;
dela(600) ;
}
}