《基于51单片机的温度检测系统程序及仿真(共6页).doc》由会员分享,可在线阅读,更多相关《基于51单片机的温度检测系统程序及仿真(共6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上/*/*用DS18B20进行测量,lcd1602显示*/*#include reg51.h#include intrins.h#define uchar unsigned char#define uint unsigned intuchar code table1 =ID: ; /欢迎显示,包括空格在内=16uchar code table2 =Name: ;/欢迎显示,包括空格在内=16uchar code str1 = Temperature ; uchar code str2 = ;/*管脚定义*sbit lcd_rs = P30; /液晶数据命令选择端sbit
2、 lcd_en = P31; /液晶使能sbit DQ = P36; /液晶使能/*参数定义*uint tvalue;/温度值uchar tflag;/温度正负标志uchar data disdata5;/*子函数定义*void delay(uchar z); /delay延时子程序void init_lcd(); /LCD1602初始化函数void write_com(uchar com); /LCD1602写指令函数void write_data(uchar date); /LCD1602写数据函数void lcd1602_display(uchar *q,uchar *p);/LCD16
3、02显示函数void welcome(); /LCD1602显示欢迎函数void delay_DS18B20(uint i); /delay_DS18B20函数void Init_DS18B20_display(); /DS18B20初始化显示void Init_DS18B20(); /DS18B20初始化uchar ReadOneByte(); /DS18B20读一字节void WriteOneByte(uchar dat); /DS18B20写一字节Read_Temperature(); /DS18B20读取温度值并转换void DS18B20_display(); /DS18B20温度显
4、示/*主函数*void main()welcome();delay(2000);Init_DS18B20_display();while(1)Read_Temperature();DS18B20_display(); /*delay延时子程序*void delay(uchar z)uchar x,y;for(x=0;xz;x+)for(y=0;y0;i-)DQ = 0;/给脉冲信号dat=1;DQ = 1; /给脉冲信号if(DQ) dat|=0x80;delay_DS18B20(10);return (dat);/*DS18B20写一字节*void WriteOneByte(uchar da
5、t)uchar i;for(i=8;i0;i-)DQ = 0;DQ = dat&0x01;delay_DS18B20(10);DQ = 1;dat=1;/*DS18B20读取温度值并转换*Read_Temperature() uchar a,b; Init_DS18B20(); WriteOneByte(0xcc); /跳过读序列号 WriteOneByte(0x44); /启动温度转换 Init_DS18B20(); WriteOneByte(0xcc); /跳过读序列号 WriteOneByte(0xbe); /读取温度 a = ReadOneByte(); b = ReadOneByte
6、(); tvalue=b; tvalue=8; tvalue=tvalue|a; if(tvalue0xfff)tflag = 0;elsetvalue = tvalue+1;tflag = 1;tvalue = tvalue*(0.625); /温度值扩大10倍,精确到1位小数return (tvalue);/*DS18B20温度显示*void DS18B20_display()uchar flagdat;disdata0=tvalue/1000+0x30; /百位数disdata1=tvalue%1000/100+0x30;/十位数disdata2=tvalue%100/10+0x30;
7、/个位数disdata3=tvalue%10+0x30; /小数位if(tflag = 0)flagdat = 0x20; /正温度不显示符号elseflagdat = 0x2d; /负温度显示负号if(disdata0 = 0x30)disdata0 =0x20;/如果百位为0,不显示if(disdata1 = 0x30)disdata1 =0x20;/如果百位为0,十位为0也不显示write_com(0xc4);write_data(flagdat); /显示符号位write_com(0xc5);write_data(disdata0);/显示百位write_com(0xc6);write_data(disdata1);/显示十位 write_com(0xc7);write_data(disdata2);/显示个位write_com(0xc8);write_data(0x2e); /显示小数点write_com(0xc9);write_data(disdata3);/显示小数位专心-专注-专业