- 5
- 0
- 约1.5千字
- 约 7页
- 2023-03-12 发布于湖北
- 举报
DS18B20 温度传感器
#includereg51.h
#define uint unsigned int #define uchar unsigned char #define Data P0
sbit DQ=P1^7;
uchar Flag,num; uint temp;
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0-9
code
//
0x39}; // C uchar str[3];
void delayms(uint z)
{
uint x,y; for(x=0;xz;x++)
for(y=0;y110;y++);
}
void delay(uint i)
{
while(i--);
}
void init()
{
TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1;
EA=1; TR0=1; P2=0;
num=0;
}
void display()
{
str[2]=table[10]; //显示 C 符号str[0]=table[temp/10]; //十位温度
str[1]=table[temp%10]; //个位温度
P2=5;
Data=str[0]; delayms(1);
P2=6;
Data=str[1]; delayms(1);
P2=7;
Data=str[2]; delayms(1);
}
void Init_DS18B20()
{
uchar x=0; DQ=1;
delay(8);
DQ=0;
delay(80); DQ=1;
delay(10); x=DQ;
delay(5);
}
void WriteOneChar(uchar dat)
{
uchar i=0; for(i=0;i8;i++)
{
DQ=0;
DQ=dat0x01; delay(5); DQ=1;
dat=1;
}
delay(5);
}
uchar ReadOneChar()
{
uchar i=0; uchar dat=0; for(i=0;i8;i++)
{
DQ=0;
dat=1; DQ=1;
if(DQ)
dat|=0x80; delay(5);
}
return(dat);
}
uint ReadTemperature()
{
uchar a=0; uchar b=0; uint t=0;
Init_DS18B20();
WriteOneChar(0xcc); WriteOneChar(0x44); delay(200); Init_DS18B20();
WriteOneChar(0xcc); WriteOneChar(0xbe); a=ReadOneChar(); b=ReadOneChar(); b=4;
b+=(a0xf0)4; t=b;
return(t);
}
void main()
{
init(); while(1)
{
display();
if(Flag==1) //定时读取温度
{
Flag=0; temp=ReadTemperature();
}
}
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256; TL0=(65536-50000)%256;
num++; if(num==20)
{
num=0;
Flag=1; //标志位
}
}
您可能关注的文档
最近下载
- 船舶柴油机装配与调试章节及期末答案.docx VIP
- FOCUS-PDCA原理及流程完整版.pptx VIP
- 整顿劳动纪律实施方案.docx VIP
- 深度解析(2026)《YCT 223.1-2014 特种滤棒 第 1 部分:醋纤沟槽滤棒》.pptx VIP
- 2025年新疆中考语文第二次模拟考试(附答案) .pdf VIP
- 冀教版(新教材)七年级下册英语Unit 5《Our neighbourhood》Lesson 1教学课件.pptx
- 2024年中考模拟试卷语文(新疆卷).docx VIP
- 立足教材开展小学音乐跨学科学习的方式与路径.docx VIP
- 2024新疆中考模拟语文试题(附参考答案) .pdf VIP
- 煤矿劳动纪律整顿实施方案.docx VIP
原创力文档

文档评论(0)