DS18b20是热敏电阻吗

问题描述:

DS18b20是热敏电阻吗
1个回答 分类:综合 2014-12-09

问题解答:

我来补答
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit D=P3^5;
sbit LED_EN_PORT = P2^5;
sbit DAC_ADC_EN_PORT = P3^7;/*定义发光二极管及ADC、DAC、时钟的锁存器有效脚LE*/
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //段选
uchar code able[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uint temp ;
void delay (uint z);
void init_ds18b20(void);
ReadOneChar();
WriteOneChar(uchar dat);
ReadTemperature(void);
void delay(unsigned int z)
{
while(z--);
}
void init_ds18b20(void) //初始化
{ unsigned char x=0;
D = 1; //DQ复位
delay(8); //稍做延时
D = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
D = 1; //拉高总线
delay(14);
x=D; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
ReadOneChar()
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
D = 0; // 给脉冲信号
dat>>=1;
D = 1; // 给脉冲信号
if(D) dat|=0x80;
delay(4);
}
return(dat);
}
WriteOneChar(uchar dat)
{
unsigned char i=0;
for (i=8; i>0; i--){
D = 0;
D = dat&0x01;
delay(5);
D = 1;
dat>>=1;
}
}
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
init_ds18b20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
init_ds18b20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t
 
 
展开全文阅读
剩余:2000
下一页:原创8