多个DS18B20温度测量程序(共9页)_第1页
多个DS18B20温度测量程序(共9页)_第2页
多个DS18B20温度测量程序(共9页)_第3页
多个DS18B20温度测量程序(共9页)_第4页
多个DS18B20温度测量程序(共9页)_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上#include<reg51.h> #include<intrins.h> /nop函数 #include<stdlib.h> #include<math.h>#define uchar unsigned char#define uint unsigned intsbit DS18B20_DQ=P27;sbit feng=P20;sbit deng=P10;#define LCD_data P0 /ch375数据端口 uchar count1,count2,count;sbit LCD_RS=P35;sbit LCD_R

2、W=P36;/ch375的rw也是此口 sbit LCD_EN=P34;#include<key_fo.h>uchar ds18b20_serial48=0x28,0x51,0x8c,0x9e,0x03,0x00,0x00,0xfd, /第1个18B20的ID 0x28,0x71,0x91,0x9e,0x03,0x00,0x00,0xdb, /第2个18B20的ID 0x28,0x43,0xa6,0x9a,0x03,0x00,0x00,0x83, /第3个18B20的ID 0x28,0xe3,0x8a,0x9e,0x03,0x00,0x00,0x08 /第4个18B20的ID;#i

3、nclude<12864.h>uchar temp_num1;uchar temp_num2;uint temper=0,yushe1=325,yushe2=325,yushe3=325,yushe4=325,piancha;/*函数:void Ds18b20_Delay(uint timer) 功能:STC单片机严格的us级延时函数 */void Ds18b20_Delay(uint timer) /uswhile(timer-) _nop_(); _nop_(); _nop_(); /*函数:void rest()功能:DS18B20芯片复位函数说明:复位时,MCU将数据线置为

4、低电平,并且保持480uS-960uS,然后置为高电平, 延时一段时间后,然后由MCU接受DS18B20的响应信号。如果接受到了响应信号, 则完成了复位操作。*/uchar Reset(void) /初始化18b20 uchar x; DS18B20_DQ=1; /复位脉冲 _nop_(); _nop_();DS18B20_DQ=0;Ds18b20_Delay(700); DS18B20_DQ=1;Ds18b20_Delay(40);/48usx=DS18B20_DQ; /稍做延时后 如果x=0则初始化成功 x=1则初始化失败while(!DS18B20_DQ);/等到存在脉冲的结束 retu

5、rn x;/*函数:uchar read_bit(void) 功能:从DS18B20读一个位 */uchar read_bit(void)/从DS18B20读一个位值 uchar bdat=0;DS18B20_DQ=1;Ds18b20_Delay(4); /两次间隔大于1usDS18B20_DQ=0;Ds18b20_Delay(4); /读操作开始 DS18B20_DQ=1;Ds18b20_Delay(4);bdat=DS18B20_DQ;Ds18b20_Delay(45);/读时间最短为60us return(bdat); /*函数:void write_bit(uchar bitval)

6、功能:向DS18B20写一个位 */void write_bit(uchar bitval) /向DS18B20写一位DS18B20_DQ=1;Ds18b20_Delay(4);/jiangeDS18B20_DQ=0;Ds18b20_Delay(4); /写操作开始 DS18B20_DQ=bitval;Ds18b20_Delay(60);DS18B20_DQ=1; /*函数:uchar read_DS18B20()功能:DS18B20读取函数*/uchar read_byte(void)/从DS18B20读一个字节 uchar i; uchar temp= 0; for(i=0;i<8;

7、i+) temp|=(read_bit()<<i); return temp; /把读到的8位数据输出 /*函数:void ds1820wr(uchar wdata)功能:DS18B20写入函数说明:DS18B20写入数据时, 1.数据线先置低电平0、延时确定的时间为15us 按从低位到高位的顺序发送数据(一次只发送一位)、延时时间为45us; 将数据线拉到高电平、重复上1到5步,知道发送完整个字节、最后将数据线拉高* */void write_byte(uchar dat)/向DS18B20写8位 unsigned char i; for(i=0;i<8;i+) write

8、_bit(dat&(1<<i); /*函数:void read_temp()功能:读取温度值*/uint gettemperature(uchar num)/读取温度 uchar i; uchar templ=0; uint temph=0; num=key_scan(); while(Reset(); write_byte(0x55);/匹配ROM指令 write_byte(0xcc);/跳过ROM for(i=0;i<8;i+)write_byte(ds18b20_serialnumi); /发出64位ROM码 write_byte(0x44);/启动温度转换 D

9、s18b20_Delay(150); while(Reset(); write_byte(0x55); for(i=0;i<8;i+)write_byte(ds18b20_serialnumi); write_byte(0xbe); /读取温度 templ=read_byte(); temph=read_byte();/先低后高 为二进制的数值 temph<<=8; temper=(uint)(temph|templ)*0.0625*10+0.5); return(temper); /*函数:dis_temperature(void)功能:各个DS18B20温度显示、预设值改

10、变和显示、偏差显示函数 */dis_temperature1(void) /SW_K6Init_12864();write_hanzi(0,0,str1);write_hanzi(2,0,str10);while(SW_K5=1&&SW_K4=1&&SW_K3=1&&(key_scan()!=26)&&(key_scan()!=21)write_shuzi(1,2,gettemperature(0);/while(count1<2); /200ms钟更新一次显示 /count1=0;TR0=0;if(key_scan()=2

11、)yushe1+=10;/write_shuzi2(3,0,yushe1);if(key_scan()=1)yushe1-=10;write_shuzi2(3,0,yushe1); /yushe为PID中的SetPointpiancha=gettemperature(0)-yushe1;write_shuzi2(3,5,abs(piancha);if(yushe1<gettemperature(0)feng=0;deng=0;else feng=1;deng=1;delay_x_ms(20);dis_serial1(uchar z)uchar i;Init_12864();for(i=0

12、;i<8;i+)if(i<4)write_serial(i,0,ds18b20_serialzi);elsewrite_serial(i-4,4,ds18b20_serialzi);/*函数:void get_ser(void)功能:显示DS18B20的序列号 */dis_serial(void)/读取序列号 uchar i,temp;Init_12864();while(Reset();write_byte(0x33);/读取光刻ROM中编码命令 3for(i=0;i<8;i+) temp=read_byte();if(i<4)write_serial(i,0,tem

13、p);elsewrite_serial(i-4,4,temp);/*12864程序模块*/#ifndef uchar#define uchar unsigned char#define uint unsigned int#endif uchar code str5="度"void delay_1ms(uint z)uint i,j;for(j=0;j<z;j+)for(i=0;i<110;i+);/*写指令到指令寄存器 */void write_cmd(uint cmd) delay_1ms(1);LCD_RS=0;LCD_RW=0; /LCD_EN=0;LCD

14、_data=cmd;LCD_EN=1;delay_1ms(1);LCD_EN=0;delay_1ms(1);/*写数据到指令寄存器 */void write_dat(uint dat) delay_1ms(1);LCD_RS=1;LCD_RW=0;LCD_EN=0;LCD_data=dat;LCD_EN=1;delay_1ms(1);LCD_EN=0;delay_1ms(1);/*12864初始化程序 */void Init_12864(void)write_cmd(0x30);/基本指令集设置 delay_1ms(5);write_cmd(0x0c);/整体显示开,游标关闭,游标不显示位置

15、delay_1ms(5);write_cmd(0x01);/清除显示 delay_1ms(5);/*set汉字位置及显示 */write_hanzi(uchar x,uchar y,uchar *p)uchar wei,num;num=0;switch(x)case 0:wei=0x80;break;case 1:wei=0x90;break;case 2:wei=0x88;break;case 3:wei=0x98;break;write_cmd(wei+y);while(*(p+num)!='0')write_dat(*(p+num);num+; /*set数字位置及显示

16、*/write_shuzi(uchar x,uchar y,uint temp)uchar bai,shi,ge,xiao;uchar wei,num;num=0;switch(x)case 0:wei=0x80;break;case 1:wei=0x90;break;case 2:wei=0x88;break;case 3:wei=0x98;break;write_cmd(wei+y);bai=temp/1000+0x30;shi=temp/100%10+0x30;ge=temp/10%10+0x30;xiao=temp%10+0x30;write_dat(bai);write_dat(sh

17、i);write_dat(ge);write_dat('.');write_dat(xiao);write_hanzi(1,5,str5);write_shuzi2(uchar x,uchar y,uint temp)uchar bai,shi,ge,xiao;uchar wei,num;num=0;switch(x)case 0:wei=0x80;break;case 1:wei=0x90;break;case 2:wei=0x88;break;case 3:wei=0x98;break;write_cmd(wei+y);bai=temp/1000+0x30;shi=temp/100%10+0x30;ge=temp/10%10+0x30;xiao=temp%10+0x30;write_dat(bai);write_dat(shi);write_dat(ge);write_dat('.');write_dat(xiao); /*四个DS18B20序列号位置设定及显示 */write_serial(uchar x,uchar y,uint temp)uchar bai,shi,ge;uchar wei,num;num=0;switch(x)case 0:wei=0x80

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论