打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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 ();

}

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C51--DS18B20数字温度计--C语言
51单片机制作实践
51单片机CO2检测显示程序资料
单片机温度检测报警程序(ds18b20)-Changing's Blog
单片机C语言设计实例——第1篇基础程序设计
51单片机定时闹钟程序(带按键)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服