DS18B20与数码管温度显示C程序复习课程

上传人:go****e 文档编号:137378856 上传时间:2020-07-07 格式:DOC 页数:5 大小:17.50KB
返回 下载 相关 举报
DS18B20与数码管温度显示C程序复习课程_第1页
第1页 / 共5页
DS18B20与数码管温度显示C程序复习课程_第2页
第2页 / 共5页
DS18B20与数码管温度显示C程序复习课程_第3页
第3页 / 共5页
DS18B20与数码管温度显示C程序复习课程_第4页
第4页 / 共5页
DS18B20与数码管温度显示C程序复习课程_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《DS18B20与数码管温度显示C程序复习课程》由会员分享,可在线阅读,更多相关《DS18B20与数码管温度显示C程序复习课程(5页珍藏版)》请在金锄头文库上搜索。

1、DS18B20与数码管温度显示C程序精品文档#include#define uchar unsigned char#define uint unsigned intsbit DQ=P14;/ds18b20与单片机连接口unsigned char code str=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x39;/共阴数码管字码表unsigned char code str1=0x0bf,0x86,0x0db,0x0cf,0x0e6,0x0ed,0x0fd,0x87,0x0ff,0x0ef,0x39;/个位带小数点字码表unsigne

2、d char code wei=0x0fe,0x0fd,0x0fb,0x0f7;uchar data disdata5;uint tvalue;/温度值uchar tflag;/温度正负标志/*ds1820程序*/void delay_18B20(unsigned int i)/延时1微秒 while(i-);void ds1820rst()/*ds1820复位*/ unsigned char x=0;DQ = 1; /DQ复位delay_18B20(4); /延时DQ = 0; /DQ拉低delay_18B20(100); /精确延时大于480usDQ = 1; /拉高delay_18B20

3、(40); uchar ds1820rd()/*读数据*/ unsigned char i=0;unsigned char dat = 0;for (i=8;i0;i-) DQ = 0; /给脉冲信号 dat=1; DQ = 1; /给脉冲信号 if(DQ) dat|=0x80; delay_18B20(10); return(dat);void ds1820wr(uchar wdata)/*写数据*/unsigned char i=0; for (i=8; i0; i-) DQ = 0; DQ = wdata&0x01; delay_18B20(10); DQ = 1; wdata=1; r

4、ead_temp()/*读取温度值并转换*/uchar a,b;ds1820rst();ds1820wr(0xcc);/*跳过读序列号*/ds1820wr(0x44);/*启动温度转换*/ds1820rst();ds1820wr(0xcc);/*跳过读序列号*/ds1820wr(0xbe);/*读取温度*/a=ds1820rd();b=ds1820rd();tvalue=b;tvalue=8;tvalue=tvalue|a; if(tvalue0x0fff) tflag=0; else tvalue=tvalue+1;tflag=1; tvalue=tvalue*(0.625);/温度值扩大1

5、0倍,精确到1位小数return(tvalue);/*/ void ds1820disp()/温度值显示 uchar flagdat; uchar i; disdata0=tvalue/1000;/百位数 disdata1=tvalue%1000/100;/十位数 disdata2=tvalue%100/10;/个位数 disdata3=tvalue%10;/小数位 if(tflag=0) flagdat=0x20;/正温度不显示符号 else flagdat=0x2d;/负温度显示负号:- if(disdata0=0x30) disdata0=0x20;/如果百位为0,不显示 if(disd

6、ata1=0x30) disdata1=0x20;/如果百位为0,十位为0也不显示 for(i=0;i150;i+) P2=wei0; P0=strdisdata3; delay_18B20(20); P2=wei1; P0=str1disdata2; delay_18B20(20); P2=wei2; P0=strdisdata1; delay_18B20(20); P2=wei3; P0=strdisdata0; delay_18B20(20); /*主程序*/void main() ds1820rst;/初始化显示 while(1)read_temp();/读取温度 ds1820disp();/显示收集于网络,如有侵权请联系管理员删除

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 其它小学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号