📄 ds18b20程序.txt
字号:
void delay(uint t) /*延时函数*/
{
for(;t>0;t--);
}
/************DS18b20复位函数************/
ow_reset(void)
{
uchar presence=1;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();
DQ=0;
delay(50); //550us
DQ=1;
delay(6); //66us
presence=DQ; //presence=0继续下一步
}
delay(45); //延时500us
presence=~DQ;
}
DQ=1;
}
/**********DS18b20读一个字节函数**********/
uchar read_byte(void)
{
uchar i;
uchar value = 0;
for (i=8;i>0;i--)
{
DQ=1;_nop_();_nop_();
value>>=1;
DQ=0;_nop_();_nop_();_nop_();_nop_(); //4us
DQ=1;_nop_();_nop_();_nop_();_nop_(); //4us
if(DQ) value|=0x80;
delay(6); //66us
}
DQ=1;
return(value);
}
/************DS18b20写一个字节函数**************/
void write_byte(char val)
{
uchar i;
for (i=8; i>0; i--)
{
DQ=1;_nop_();_nop_();
DQ=0;_nop_();_nop_();_nop_();_nop_();_nop_(); //5us
DQ=val&0x01; //最低为移出
delay(6); //66us
val=val/2; //右移一位
}
DQ=1;
delay(1);
}
/***************读取温度函数**************/
uint read_temperature(void)
{
union{unsigned char c[2];
unsigned int x;
}temp;
ow_reset(); //总线复位
write_byte(0xCC); // 发Skip ROM命令
write_byte(0xBE); // 发读命令
temp.c[1]=read_byte(); //温度低8位
temp.c[0]=read_byte(); //温度高8位
ow_reset();
write_byte(0xCC); //Skip ROM
write_byte(0x44); //发转换命令
temp.x<<=4;
return (temp.c[0]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -