#includesbitDQ=P3^4;#defineuint8unsignedchar#defineuint16unsignedint//定义DS18B20总线I/Ounsignedcharx=0;voidDelay(intnum){//延时函数while(num--);}voidInit_DS18B2...
#include
sbit DQ = P3 ^ 4;
#define uint8 unsigned char
#define uint16 unsigned int
//定义DS18B20总线I/O
unsigned char x=0;
void Delay(int num){//延时函数
while(num--) ;
}
void Init_DS18B20(void){//初始化ds1820
x=0;
DQ = 1; //DQ复位
Delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
Delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
Delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void){//读一个字节
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay(4);
}
return(dat);
}
void WriteOneChar(unsigned char dat){//写一个字节
unsigned char i=0;
for (i=8; i>0; i--){
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
/******************************************************************************/
unsigned int 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); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt+0.5; //四舍五入
return(t);
}
sbit le = P1 ^ 2;
void de(unsigned int a){
unsigned int i;
while( --a != 0){
for(i = 0; i < 125; i++);
}
}
为什么输出的莫尔斯码一直是"tem96"?不可能96度吧
展开