ds18b20.c
来自「温度控制PWM」· C语言 代码 · 共 113 行
C
113 行
#include <REG52.h>
#include "DS18B20.h"
/*************************************************
/*
/*
/************************************************/
sbit DQ = P2^0; //数据输出口
/*************************************************
/*
/*
/*
/*
/*
/************************************************/
void delay1(int us)
{
int s;
for (s=0; s<us; s++);
}
/**********************************************************
*Header:
*File Name:
*Author:陈培炜,杨帆
*Data;2007
*Function:rest the Ds18b20
*********************************************************/
void rst(void)
{
DQ = 1;
delay1(2);
DQ = 0;
delay1(30); // 精确延时 480~960us //
DQ = 1;
delay1(8);
}
/*************************************************
/*Header:DS18B20.h
/*File Name:DS18B20.c
/*Author: 陈培炜,杨帆
/*Data;2007
/*Function:
/************************************************/
unsigned int read(void)
{
int i = 0;
unsigned int u = 0;
for (i=0; i<16; i++)
{
DQ = 0;
u >>= 1;
DQ = 1;
if (DQ)
{
u |= 0x8000;
}
delay1(4);
}
return (u);
}
/*************************************************
/*Header:DS18B20.h
/*File Name:DS18B20.c
/*Author: 陈培炜,杨帆
/*Data;2007
/*Function:
/************************************************/
void write(unsigned char ku)
{
int i = 0;
for (i=0; i<8 ;i++)
{
DQ = 0;
DQ = ku & 0x01;
delay1(3);
DQ = 1;
ku >>= 1;
}
}
/*************************************************
/*Header:DS18B20.h
/*File Name:DS18B20.c
/*Author: 陈培炜,杨帆
/*Data;2007
/*Function:
/************************************************/
char read0(void)
{
int t1 = 0;
unsigned int tp;
unsigned int lsb;
rst();
write(0xCC);
write(0x44);
rst();
write(0xCC);
write(0xBE);
tp = read();
lsb = (unsigned int)(tp*6.25); //
t1=lsb/100;
return((char)t1);
}
/*************************************************
/*
/*
/************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?