单片机温度控制程序.doc

上传人:文库蛋蛋多 文档编号:4144530 上传时间:2023-04-07 格式:DOC 页数:9 大小:184.50KB
返回 下载 相关 举报
单片机温度控制程序.doc_第1页
第1页 / 共9页
单片机温度控制程序.doc_第2页
第2页 / 共9页
单片机温度控制程序.doc_第3页
第3页 / 共9页
单片机温度控制程序.doc_第4页
第4页 / 共9页
单片机温度控制程序.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《单片机温度控制程序.doc》由会员分享,可在线阅读,更多相关《单片机温度控制程序.doc(9页珍藏版)》请在三一办公上搜索。

1、单片机DS18B20温度计C语言程序 (2008-09-27 17:01:06) #include #include #include /要用到取绝对值函数abs()/通过DS18B20测试当前环境温度, 并通过数码管显示当前温度值, 目前显示范围: -55 +125度 sbit wela = P27; /数码管位选 sbit dula = P26; /数码管段选 sbit ds = P22; int tempValue; /0-F数码管的编码(共阳极) unsigned char code table=0xc0,0xf9,0xa4,0xb0,0x99, 0x92,0x82,0xf8,0x80

2、,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e; /0-9数码管的编码(共阳极), 带小数点 unsigned char code tableWidthDot=0x40, 0x79, 0x24, 0x30,0x19, 0x12, 0x02,0x78, 0x00, 0x10; /延时函数, 对于11.0592MHz时钟, 例i=10,则大概延时10ms. void delay(unsigned int i) unsigned int j; while(i-) for(j = 0; j 0) i-; ds = 1; /产生一个上升沿, 进入等待应答状态 i = 4; whi

3、le(i0) i-; void dsWait() unsigned int i; while(ds); while(ds); /检测到应答脉冲 i = 4; while(i 0) i-;/向DS18B20读取一位数据/读一位, 让DS18B20一小周期低电平, 然后两小周期高电平,/之后DS18B20则会输出持续一段时间的一位数据bit readBit() unsigned int i; bit b; ds = 0; i+; /延时约8us, 符合协议要求至少保持1us ds = 1; i+; i+; /延时约16us, 符合协议要求的至少延时15us以上 b = ds; i = 8; whi

4、le(i0) i-; /延时约64us, 符合读时隙不低于60us要求 return b;/读取一字节数据, 通过调用readBit()来实现unsigned char readByte() unsigned int i; unsigned char j, dat; dat = 0; for(i=0; i8; i+) j = readBit(); /最先读出的是最低位数据 dat = (j 1); return dat;/向DS18B20写入一字节数据void writeByte(unsigned char dat) unsigned int i; unsigned char j; bit b

5、; for(j = 0; j = 1; /写1, 将DQ拉低15us后, 在15us60us内将DQ拉高, 即完成写1 if(b) ds = 0; i+; i+; /拉低约16us, 符号要求1560us内 ds = 1; i = 8; while(i0) i-; /延时约64us, 符合写时隙不低于60us要求 else /写0, 将DQ拉低60us120us ds = 0; i = 8; while(i0) i-; /拉低约64us, 符号要求 ds = 1; i+; i+; /整个写0时隙过程已经超过60us, 这里就不用像写1那样, 再延时64us了 /向DS18B20发送温度转换命令

6、void sendChangeCmd() dsInit(); /初始化DS18B20, 无论什么命令, 首先都要发起初始化 dsWait(); /等待DS18B20应答 delay(1); /延时1ms, 因为DS18B20会拉低DQ 60240us作为应答信号 writeByte(0xcc); /写入跳过序列号命令字 Skip Rom writeByte(0x44); /写入温度转换命令字 Convert T/向DS18B20发送读取数据命令void sendReadCmd() dsInit(); dsWait(); delay(1); writeByte(0xcc); /写入跳过序列号命令

7、字 Skip Rom writeByte(0xbe); /写入读取数据令字 Read Scratchpad/获取当前温度值int getTmpValue() unsigned int tmpvalue; int value; /存放温度数值 float t; unsigned char low, high; sendReadCmd(); /连续读取两个字节数据 low = readByte(); high = readByte(); /将高低两个字节合成一个整形变量 /计算机中对于负数是利用补码来表示的 /若是负值, 读取出来的数值是用补码表示的, 可直接赋值给int型的value tmpva

8、lue = high; tmpvalue 0 ? 0.5 : -0.5); /大于0加0.5, 小于0减0.5 return value;unsigned char const timeCount = 3; /动态扫描的时间间隔/显示当前温度值, 精确到小数点后一位/若先位选再段选, 由于IO口默认输出高电平, 所以当先位选会使数码管出现乱码void display(int v) unsigned char count; unsigned char datas = 0, 0, 0, 0, 0; unsigned int tmp = abs(v); datas0 = tmp / 10000; d

9、atas1 = tmp % 10000 / 1000; datas2 = tmp % 1000 / 100; datas3 = tmp % 100 / 10; datas4 = tmp % 10; if(v 0) /关位选, 去除对上一位的影响 P0 = 0xff; wela = 1; /打开锁存, 给它一个下降沿量 wela = 0; /段选 P0 = 0x40; /显示-号 dula = 1; /打开锁存, 给它一个下降沿量 dula = 0; /位选 P0 = 0xfe; wela = 1; /打开锁存, 给它一个下降沿量 wela = 0; delay(timeCount); for(

10、count = 0; count != 5; count+) /关位选, 去除对上一位的影响 P0 = 0xff; wela = 1; /打开锁存, 给它一个下降沿量 wela = 0; /段选 if(count != 2) P0 = tabledatascount; /显示数字 else P0 = tableWidthDotdatascount; /显示带小数点数字 dula = 1; /打开锁存, 给它一个下降沿量 dula = 0; /位选 P0 = _crol_(0xfd, count); /选择第(count + 1) 个数码管 wela = 1; /打开锁存, 给它一个下降沿量 w

11、ela = 0; delay(timeCount); void main() unsigned char i; while(1) /启动温度转换 sendChangeCmd(); /显示5次 for(i = 0; i 40; i+) display(tempValue); tempValue = getTmpValue(); 1.18b20跳过查询序列号是因为一般都是单个应用,18b20只是自带唯一的ID,而不是唯一的地址,所以除非你用之前先把每个的ID读出来存起来,否则是不能一线多个器件一起用的,也就是为什么大多数都是直接skip掉,因为很少会采用它的那个多器件寻址功能。2.0xCC就是跳过

12、序列号的命令,0x44就是启动温度转换的命令,建议你看看18b20的datasheet。3.NOP就是机器执行一下空指令,一般都是延迟用的,如果单片机是12MHz的标准51,一个NOP指令可以延迟1us。#include #include #define uchar unsigned char#define uint unsigned intsbit DQ=P37; sbit s1=P10;sbit s2=P11;uchar temp;uchar du();void xie(uchar dat);void ds1820();void delay(uint t);bit flag;uchar d

13、uwendu();void yanshi();uint a=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f; void yanshi(void) uchar a,b,c; for(c=167;c0;c-) for(b=171;b0;b-) for(a=16;a0;a-); _nop_ (); void yanshi1(void) unsigned char a,b; for(b=35;b0;b-) for(a=25;a0;a-);void delay(uint t)while(t-); void ds1820() DQ=1; _nop_();

14、 DQ=0; delay(80); /530us_nop_();DQ=1; delay(14); /delay 100 uS/14_nop_();_nop_();_nop_();if(DQ=0)flag = 1; /detect 1820 success!elseflag = 0; /detect 1820 fail! delay(20); /20 _nop_(); _nop_(); DQ = 1; void xie(uchar dat)uint i; for(i=0;i=1; delay(4); uchar du() uchar i,va; for(i=0;i=1; DQ=1; if(DQ)

15、va|=0x80; delay(4); return va; uchar duwendu()uchar a,b;ds1820();xie(0xcc); /跳过 romxie(0x44); /启动温度测量delay(300);/延时ds1820(); xie(0xcc); /跳过 romxie(0xbe);a=du(); /低位b=du(); /高位 b4;b=a+b;return b; void main() temp=duwendu(); P2=atemp/10;/10WEI s1=0;s2=1; yanshi1(); P2=atemp%10;/GEWEI s1=1;s2=0; yanshi1();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号