📄 ds18b20.c
字号:
#include "reg51.h"
#include "stdio.h"
#include "intrins.h"
#include "math.h"
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^5;
//主函数中的变量定义
/*********************************************************/
//18b20的驱动程序
//延时程序
void delay1820(uint i)
{
while(i--);
}
//18B20初始化程序
void initial1820()
{
/* DQ=1;
delay1820(8);
DQ=0;
delay1820(80);
DQ=1;
delay1820(10);*/
char presence=1;
while(presence)
{
while(presence)
{
DQ=1;
delay1820(50);
DQ=0;
delay1820(50);
DQ=1;
delay1820(6);
presence=DQ;
}
delay1820(45);
presence=~DQ;
}
DQ=1;
}
//读一个字节
uchar rchar()
{
uchar i;
uchar value=0;
for(i=8;i>0;i--)
{
DQ=1;_nop_();_nop_();
value>>=1;
DQ=0;
_nop_();_nop_();_nop_();_nop_();
DQ=1;_nop_();_nop_();_nop_();_nop_();
if(DQ)value|=0x80;
delay1820(6);
}
DQ=1;
return(value);
}
//写一个字节
void wchar(uchar val)
{
uchar i;
for(i=8;i>0;i--)
{
DQ=1;_nop_();_nop_();
DQ=0;_nop_();_nop_();_nop_();_nop_();_nop_();
DQ=val&0x01;
delay1820(6);
val=val/2;
}
DQ=1;
delay1820(1);
}
//读取温度
unsigned char rtemp()
{
uchar a=0;
uchar b=0;
uchar t=0;
initial1820();
wchar(0xCC);
wchar(0x44);
delay1820(200);
initial1820();
wchar(0xCC);
wchar(0xBE);
a=rchar();
b=rchar();
initial1820();
wchar(0xCC);
wchar(0x44);
b<<=4;
b+=(a&0xF0)>>4;
t=b;
return(t);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -