// 3.DS18B20数据口线的修改请参考DS18B20.h中的定义.
#include"DS18B20.h"
#include <IOM8v.h>
#include <macros.h>
void Delay1us(uchar value){
value = value;
}
//---------------------------------------------
//name:Ds18b20Reset
//description:复位ds18b20芯片进行初始化
//parameter: no
//---------------------------------------------
void Ds18b20Reset(void){
DS18B20_DQ_CLE;
DelayUs(198); //100us
DelayUs(198);
DelayUs(198);
DelayUs(198);
DS18B20_DQ_SET;
DelayUs(38); //20us
}
//---------------------------------------------
//name:Ds18b20Ack
//description:应答单总线
//parameter: no
//---------------------------------------------
void Ds18b20Ack(void){
while(DS18B20_DQ_DAT);
while(!DS18B20_DQ_DAT);
DelayUs(38); //20us
}
//---------------------------------------------
//name:Ds18b20Write
//description: 写一个字节
//parameter: dat 要写入的数据,无符号字符型
//---------------------------------------------
void Ds18b20Write(uchar data){
uchar i;
for(i=0;i<8;i++) {
if(data & 0x01) { // 写 1
DS18B20_DQ_CLE;
DelayUs(14); //8us
DS18B20_DQ_SET;
DelayUs(108); //55us
}else{ // 写 0
DS18B20_DQ_CLE;
DelayUs(108); //55us
DS18B20_DQ_SET;
DelayUs(14); //8us
}
data = data>>1;
}
}
//---------------------------------------------
//name:Ds18b20ReadBit
//description: 读一个位
//parameter: no
//---------------------------------------------
uchar Ds18b20ReadBit(void){
uchar data;
DS18B20_DQ_CLE;
Delay1us(1); //延时1个us
DS18B20_DQ_SET;
Delay1us(1); //延时2个us
Delay1us(1);
data = DS18B20_DQ_DAT;
DelayUs(118); //60us
return data;
}
//---------------------------------------------
//name:Ds18b20ReadByte
//description: 读一个字节
//parameter: no
//---------------------------------------------
uchar Ds18b20ReadByte(void){
uchar i,data;
data = 0;
for(i=0;i<8;i++){
data = data >> 1;
if(Ds18b20ReadBit())
data |= 0x80;
}
return data;
}
//---------------------------------------------
//name:Ds18b20Convert
//description: 命令Ds18b20开始转换温度
//parameter: no
//---------------------------------------------
void Ds18b20Convert(void){
Ds18b20Reset();
Ds18b20Ack();
delms(1);
Ds18b20Write(DS18B20_SKIP_ROM);
Ds18b20Write(DS18B20_CONVERT_TEM);
}
//---------------------------------------------
//name:Ds18b20ReadTem
//description: 从Ds18b20读取温度
//parameter: no
//---------------------------------------------
uint Ds18b20ReadTem(void){
uchar Height,Low;
uint Temp;
Ds18b20Reset(); //初始化
Ds18b20Ack();
delms(1);
Ds18b20Write(DS18B20_SKIP_ROM); //跳过ROM
Ds18b20Write(DS18B20_READ_RAM); //读存储器
Low = Ds18b20ReadByte();
Height = Ds18b20ReadByte();
Temp = (Height << 8) + Low;
return Temp;
}
用户309506 2011-3-24 23:03
用户203227 2009-9-2 14:49
用户203227 2009-9-2 14:49
用户134257 2008-5-24 17:48