




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
电路实物图如下图所示:电路原理图如下图所示:C语言程序如下所示:/* 程序名; 基于DS18B20的测温系统* 功 能: 实时测量温度,超过上下限报警,报警温度可手动调整。K1是用来* 进入上下限调节模式的,当按一下K1进入上限调节模式,再按一下进入下限* 调节模式。在正常模式下,按一下K2进入查看上限温度模式,显示1s左右自动* 退出;按一下K3进入查看下限温度模式,显示1s左右自动退出;在调节上下限* 温度模式下,K2是实现加1功能,K1是实现减1功能,K3是用来设定上下限温* 度正负的。 * 编程者:ZPZ* 编程时间:2009/10/2 */#include#include#define uint unsigned int #define uchar unsigned charuchar max_int=0x00,max_dot=0x00,min_int=0x00,min_dot=0x00;bit s=0,s1=0; #includeds18b20.h#includekeyscan.h#includedisplay.h/*主函数*/void main() beer=0; led=1; IT1=1; EX1=0; EA=1; timer1_init(0); get_temperature(1); while(1) keyscan(); get_temperature(0); keyscan(); display(temp,temp_d*0.625); alarm(); keyscan(); /* 程序名; _ds18b20_h_* 功 能: DS18B20的c51编程头文件 * 编程者:ZPZ* 编程时间:2009/10/2 * 说 明:用到的全局变量是:无符号字符型变量temp(测得的温度整数部分),temp_d * (测得的温度小数部分),标志位f(测量温度的标志位0表示“正温度”1表* 示“负温度”),标志位f_max(上限温度的标志位0表示“正温度”、1表* 示“负温度”),标志位f_min(下限温度的标志位0表示“正温度”、1表* 示“负温度”),标志位w(报警标志位1启动报警0关闭报警)。 */#ifndef _ds18b20_h_#define _ds18b20_h_#define uint unsigned int #define uchar unsigned charsbit DQ= P23;sbit beer=P30; sbit led=P31;uchar temp=0; /温度的整数部分uchar temp_d=0; /温度的小数部uchar n;bit f=0,f_max=0,f_min=0;w=0;/*延时子函数*/void ds18b20_delayus(uint t) while(t-);void ds18b20_delayms(uint t)uint i,j;for(i=t;i0;i-) for(j=120;j0;j-);/*ds18b20初始化函数*(*/void ds18b20_init()/ DS18B20初始化 DQ=1;DQ=0; /控制器向DS18B20发低电平脉冲ds18b20_delayus(30); /延时480sDQ=1;/控制器拉高总线,while(DQ); /等待DS18B20拉低总线,在60-240s之间ds18b20_delayus(20);/延时,等待上拉电阻拉高总线DQ=1;/提升数据线,准备数据传输;/*ds18b20字节读函数*/uchar ds18b20_read() /DS18B20 字节读取uchar i;uchar d = 0;DQ = 1;/准备读;for(i=8;i0;i-) d = 1; /低位先发;DQ = 0;_nop_();_nop_();_nop_();DQ = 1;/必须写1,否则读出来的将是不预期的数据;if(DQ)/在12us处读取数据;d |= 0x80;ds18b20_delayus(10);return d;/*ds18b20字节写函数*/void ds18b20_write(uchar d)/ ds18b20字节写 uchar i; for(i=8;i0;i-) DQ=0; _nop_(); _nop_(); _nop_(); DQ=d&0x01; ds18b20_delayus(5); DQ=1; d = 1; /*获取温度函数*/ void get_temperature(bit f) /得到整数的温度值uchar a=0,b=0,c=0,d=0; uint i;ds18b20_init(); /DS18B20初始化ds18b20_write(0xcc);/向DS18B20发SKIP ROM命令ds18b20_write(0x44);/启动DS18B20进行温度转换,结果存入内部RAMds18b20_delayms(1);ds18b20_init(); /DS18B20初始化ds18b20_write(0xcc); /向DS18B20发SKIP ROM命令ds18b20_write(0xbe);/读内部RAM中9字节的内容命令a=ds18b20_read();/读内部RAM (LSB)b=ds18b20_read();/读内部RAM (MSB)if(f=1) max_int=ds18b20_read();/读内部RAM (LSB) min_int=ds18b20_read();if(max_int&0x80)=0x80)f_max=1;max_int=(max_int-0x80); if(min_int&0x80)=0x80)f_min=1;min_int=(min_int-0x80); i=b;i=4;if (i=0) f=0; /i为0,正温度,设立正温度标记 temp=(a4)|(b4)|(b4);/整数部分 a=(a&0x0f);/小数部分 temp_d=a; void store_t() if(f_max=1)max_int=max_int+0x80;if(f_min=1)min_int=min_int+0x80; ds18b20_init(); /DS18B20初始化ds18b20_write(0xcc); /向DS18B20发SKIP ROM命令ds18b20_write(0x4e);ds18b20_write(max_int);ds18b20_write(min_int);ds18b20_write(0xff);ds18b20_init(); /DS18B20初始化ds18b20_write(0xcc); /向DS18B20发SKIP ROM命令ds18b20_write(0x48); /*温度超限报警函数*/ void alarm() if(f_max=0) if(f_min=0) if(f=0) if(temp+temp_d*0.0625)=max_int) w=1;TR1=1; if(temp+temp_d*0.0625)min_int) w=0; if(f=1)w=1;TR1=1; if(f_min=1) if(f=0) if(temp+temp_d*0.0625)=max_int) w=1;TR1=1; if(temp+temp_d*0.0625)=min_int) w=1;TR1=1; if(temp+temp_d*0.0625)min_int ) w=0; if(f_max=1) if(f_min=1) if(f=1) if(temp+temp_d*0.0625)=min_int) w=1;TR1=1; if(temp+temp_d*0.0625)max_int) w=0; if(f=0)w=1;TR1=1; #endif/* 程序名; _keyscan_H_ * 功 能: ds18b20键盘头文件,通过键盘设定设定上下限报警温度* 编程者:ZPZ* 编程时间:2009/10/2 */#ifndef _keyscan_H_#define _keyscan_H_sbit key1=P22; sbit key2=P21; sbit key3=P20;sbit key4=P33;uchar a=0,i=0;bit k4=0,v=0,v1=0,v2=0;/*读键盘延时子函数*/void keyscan_delay(uint z) uint i,j; for(i=z;i0;i-)for(j=120;j0;j-);/*温度调节函数*/int temp_change(int count,bit f) if(key2=0)keyscan_delay(10);if(key2=0) if(f=0) count+; if(a=1)if(count125) count=125; if(a=2)if(count125) count=125; if(f!=0) count+; if(a=1)if(count55) count=55; if(a=2)if(count55) count=55; while(key2=0);keyscan_delay(10);if(key3=0)keyscan_delay(10);if(key3=0) count-;if(a=1)if(count0) count=0;if(a=2)if(count2)i=0;TR1=0;k4=0;v=0;store_t();get_temperature(1); switch(i) case 0:a=0;break; case 1:a=1;break; case 2:a=2;break; default:break; while(key1=0);keyscan_delay(10);if(a=1&v=1)led=0;max_int=temp_change(max_int,f_max);else if(a=2&v=1)led=1;min_int=temp_change(min_int,f_min);else;if(k4=1)if(key4=0)keyscan_delay(5);if(key4=0) if(a=1) if(max_int55) f_max=0;else f_max=f_max; if(a=2) if(min_int55) f_max=0;else f_min=f_min; while(key4=0);keyscan_delay(10);if(v=0)if(key2=0)keyscan_delay(10);if(key2=0) a=1; TR1=1; s1=1; while(key2=0);keyscan_delay(10);if(key3=0)keyscan_delay(10);if(key3=0) a=2; TR1=1; s1=1; while(key3=0);keyscan_delay(10);if(v1=1)a=0;v1=0;TR1=0; #endif/* 程序名; _ds18b20_display_H_ * 功 能: ds18b20数码管动态显示头文件,通过定时器0延时实现数码管动态显示* 编程者:ZPZ* 编程时间:2009/10/2 */#ifndef _ds18b20_display_H_#define _ds18b20_display_H_#define uint unsigned int #define uchar unsigned charsbit wei1=P24; sbit wei2=P25; sbit wei3=P26; sbit wei4=P27;uchar num=0;uchar code temperature1= 0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f;uchar code temperature2= 0xbf,0x86,0xdb,0xcf,0xe6, 0xed,0xfd,0x87,0xff,0xef;uchar code temperature3= 0x00,0x80,0x40,0x76,0x38;/*延时子函数*/void display_delay(uint t)uint i,j;for(i=t;i0;i-) for(j=20;j0;j-);/*定时器1初始化函数*/void timer1_init(bit t) TMOD=0x10; TH0=0x3c; TL0=0xb0; EA=1; ET1=1; TR1=t;/*定时器1中断函数*/void timer1() interrupt 3 TH0=0x3c; TL0=0xb0;num+; if(num25) num=0; s1=0; v1=1; /*调节选择函数*/void selsct_1(uchar f,uchar k) if(f=0) if(k/100=0) P0=temperature30; else P0=temperature1k/100; if(f=1) if(k%100/10=0) P0=temperature30; else P0=temperature32; void selsct_2(bit f,uchar k) if(f=0) if(k/100=0)&(k%100/10=0) P0=temperature30; else P0=temperature1k%100/10; if(f=1) if(k%100/10=0) P0=temperature32; else P0=temperature1k%100/10; /*显示函数*/void display(uchar t,uchar t_d) uchar i; for(i=0;i4;i+) switch(i) case 0: if(a=0)selsct_1(f,t); if(a=1) if(s=0) selsct_1(f_max,max_int); else P0=temperature30; if(s1=1) selsct_1(f_max,max_int); if(a=2) if(s=0) selsct_1(f_min,min_int); else P0=temperature30; if(s1=1) selsct_1(f_min,min_int); wei2=0; wei3=0; wei4=0; wei1=1; break; case 1: if(a=0)selsct_2(f,t); if(a=1) if(s=0) selsct_2(f_max,max_int); else P0=temperature30; if(s1=1) selsct_2(f_max,max_int)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年江西中烟工业公司笔试试题及答案
- 川菜考试题及答案
- 取样考试题及答案
- 办公考试题及答案
- 中华传统文化融入《思想道德与法治》知到智慧树答案
- 基础护理学习题及答案
- 高血压试题及答案4
- 2025年二手车转让与售后服务保障协议
- 2025年房屋拆迁补偿安置协议书范本与案例分析
- 2025年度养老地产项目委托销售代理协议
- 电影院安全生产与安全管理规定制度
- 废气处理合同协议
- 镁铝合金行业前景
- 2025-2030中国余热回收行业市场现状供需分析及投资评估规划分析研究报告
- 无人机物流配送服务手册
- 见证取样送检计划方案
- 二年级上册语文课内阅读理解每日一练(含答案)
- 2025-2030年中国功率器件市场发展趋势规划研究报告
- 基层管理培训课程
- 宇宙飞船的发射与回收技术分析
- 2024考研 政治 思维导图(马原)
评论
0/150
提交评论