分享

51/51单片机第9课:DS18B20温度检测系统

 xpxys99 2019-09-04

51/51单片机第9课:DS18B20温度检测系统

51/51单片机第9课:DS18B20温度检测系统

#include <reg51.h>//头文件

#define uint unsigned int//宏定义

#define uchar unsigned char //宏定义

sbit DQ=P3^2;//18B20的2脚与单片机P3.2口相接

sbit P10=P1^0;//数码管位选引脚P1.0

sbit P11=P1^1;//数码管位选引脚P1.1

uchar temp;

code unsigned char sz []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0-9数组

void delay (uint t);//延时函数声明

void delayms(uint a);//for语句延时函数声明

void init();//18B20初始化程序声明

void write (uchar dat);//写1字节程序声明

uchar read ();//读1字节程序声明

void display();//显示程序声明

uchar readtemp();//读温度程序声明

void delay (uint t)//延时函数

{

while(t--);

}

void delayms(uint a) //for语句延时函数

{

uint x,y;

for(x=a;x>0;x--)

for(y=110;y>0;y--);

}

void init ()//18B20初始化程序

{

uchar n;

DQ=1;

delay(8);

DQ=0;

delay(80);

DQ=1;

delay(8);

n=DQ;

delay(4);

}

void write (uchar dat)//写1字节程序

{

uchar i;

for (i=0;i<8;i++)

{

DQ=0;

DQ=dat&0x01;

delay (4);

DQ=1;

dat>>=1;//dat=dat>>1

}

delay(4);

}

uchar read ()//读1字节程序

{

uchar i,value;

for (i=0;i<8;i++)

{

DQ=0;

value>>=1;

DQ=1;

if(DQ)

value|=0x80;

delay(4);

}

return value;

}

uchar readtemp()//读温度程序

{

uchar a,b;

init();

write (0xcc);

write (0x44);

delay (300);

init ();

write (0xcc);

write (0xbe);

a=read();

b=read();

b<<=4;

b+=(a&0xf0)>>4;

return b;

}

void display ()//显示程序

{

P10=0;

P0=sz[temp/10];

delayms(2);

P10=1;

P11=0;

P0=sz[temp%10];

delayms(2);

P11=1;

}

void main()//主程序

{

while(1)

{

temp=readtemp();

display ();

}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多