基于ds18b20测温系统论文内容摘要:

covert1(TL,39。 L39。 )。 LCD_Print(0,1,TempBuffer0)。 } if(%5==0) { ISendStr(AT24C02,0,amp。 TemperatureA,2)。 } 22 } } 各模块子程序 [1]温度测试程序: Ds18b20: include include include unsigned char tt[2]。 sbit DQ1 = P3^4。 /**********ds18b20 初始化函数 **********************/ void Init_DS18B20(void) { unsigned char x=0。 DQ1= 1。 //DQ 复位 delay(8)。 //稍做延时 DQ1= 0。 //单片机将 DQ拉低 delay(80)。 //精确延时 大于 480us DQ1= 1。 //拉高总线 delay(14)。 x=DQ1。 //稍做延时后 如果 x=0 则初始化成功 x=1 则初始化失败 delay(20)。 } /***********ds18b20 读一个字节 **************/ unsigned char ReadOneChar(void) { unsigned char i=0。 unsigned char dat = 0。 23 for (i=8。 i0。 i) { DQ1 = 0。 // 给脉冲信号 dat=1。 DQ1 = 1。 // 给脉冲信号 if(DQ1) dat|=0x80。 delay(4)。 } return(dat)。 } /*************ds18b20 写一个字节 ****************/ void WriteOneChar(unsigned char dat) { unsigned char i=0。 for (i=8。 i0。 i) { DQ1 = 0。 DQ1 = datamp。 0x01。 delay(5)。 DQ1 = 1。 dat=1。 } } /**************读取 ds18b20 当前温度 ************/ unsigned char *ReadTemperature() { // Init_DS18B20()。 // WriteOneChar(0xCC)。 // 跳过读序号列号的操作 // WriteOneChar(0x4E)。 // //写入 写暂存器 命令 ,修改 TH 和 TL 和分辩率配置寄存器 //先写 TH,再写 TL,最后写配置寄存器 // WriteOneChar(TH)。 //写入想设定的温度报警上限 // WriteOneChar(TL)。 //写入想设定的温度报警下限 // WriteOneChar(RS)。 //写配置寄存器 ,格式为 0 R1 R0 1,1 1 1 1 //R1R0=00 分辨率娄 9 位 ,R1R0=11 分辨率为 12 位 // delay(80)。 // this message is wery important Init_DS18B20()。 WriteOneChar(0xCC)。 // 跳过读序号列号的操作 WriteOneChar(0x44)。 // 启动温度转换 24 delay(80)。 // this message is wery important Init_DS18B20()。 WriteOneChar(0xCC)。 //跳过读序号列号的操作 WriteOneChar(0xBE)。 //读取温度寄存器等(共可读 9 个寄存器 ) 前两个就是温度 delay(80)。 tt[0]=ReadOneChar()。 //读取温度值低位 tt[1]=ReadOneChar()。 //读取温度值高位 return(tt)。 } Ds18b201: include include include unsigned char tt1[2]。 sbit DQ2 = P2^5。 /**********ds18b20 初始化函数 **********************/ void Init_DS18B201(void) { unsigned char x=0。 DQ2= 1。 //DQ 复位 delay(8)。 //稍做延时 DQ2= 0。 //单片机将 DQ 拉低 delay(80)。 //精确延时 大于 480us DQ2= 1。 //拉高总线 delay(14)。 x=DQ2。 //稍做延时后 如果 x=0 则初始化成功 x=1 则初始化失败 delay(20)。 } /***********ds18b20 读一个字节 **************/ unsigned char ReadOneChar1(void) { unsigned char i=0。 unsigned char dat = 0。 for (i=8。 i0。 i) { 25 DQ2 = 0。 // 给脉冲信号 dat=1。 DQ2 = 1。 // 给脉冲信号 if(DQ2) dat|=0x80。 delay(4)。 } return(dat)。 } /*************ds18b20 写一个字节 ****************/ void WriteOneChar1(unsigned char dat) { unsigned char i=0。 for (i=8。 i0。 i) { DQ2 = 0。 DQ2 = datamp。 0x01。 delay(5)。 DQ2 = 1。 dat=1。 } } /**************读取 ds18b201 当前温度 ************/ unsigned char *ReadTemperature1() { // Init_DS18B201()。 // WriteOneChar1(0xCC)。 // 跳过读序号列号的操作 // WriteOneChar1(0x4E)。 // //写入 写暂存器 命令 ,修改 TH 和 TL 和分辩率配置寄存器 //先写 TH,再写 TL,最后写配置寄存器 // WriteOneChar1(TH)。 //写入想设定的温度报警上限 // WriteOneChar1(TL)。 //写入想设定的温度报警下限 // WriteOneChar1(RS)。 //写配置寄存器 ,格式为 0 R1 R0 1,1 1 1 1R1R0=00 分辨率娄9 位 ,R1R0=11 分辨率为 12 位 // delay(80)。 // this message is wery important Init_DS18B201()。 WriteOneChar1(0xCC)。 // 跳过读序号列号的操作 WriteOneChar1(0x44)。 // 启动温度 转换 delay(80)。 // this message is wery important 26 Init_DS18B201()。 WriteOneChar1(0xCC)。 //跳过读序号列号的操作 WriteOneChar1(0xBE)。 //读取温度寄存器等(共可读 9 个寄存器) 前两个就是温度 delay(80)。 tt1[0]=ReadOneChar1()。 //读取温度值低位 tt1[1]=ReadOneChar1()。 //读取温度值高位 return(tt1)。 } [2]显示程序 : include include include include //定义的时间类型 SYSTEMTIME MTIME。 sbit LcdRs = P2^0。 sbit LcdRw = P2^1。 sbit LcdEn = P2^2。 sfr DBPort = 0x80。 unsigned char ReadValue。 unsigned char t[2],*pt。 //用来存放温度值 ,测温程序就是通过这个数组与主函数通信的 //unsigned char display[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88}。 unsigned char TempBuffer1[9]={0x2b,0x31,0x32,0x32,0x2e,0x30,0x30,0x43,39。 \039。 }。 //显示实时温度 ,上电时显示 + unsigned char TempBuffer0[17]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,39。 \039。 }。 //显示温度上下限 ,上电时显示 TH:+125 TL:+124C unsigned char code dotcode[4]={0,25,50,75}。 // 内部等待函数************************************************************************** unsigned char LCD_Wait(void) { LcdRs=0。 LcdRw=1。 _nop_()。 LcdEn=1。 _nop_()。 //while(DBPortamp。 0x80)。 //在用 Proteus 仿真时,注意用屏蔽此语句,在调用 GotoXY() 27 时,会进入死循环, //可能在写该控制字时,该模块没有返回写入完备命令,即 DBPortamp。 0x80==0x80 //实际硬件时打开此语句 LcdEn=0。 return DBPort。 } // 向 LCD 写 入 命 令 或 数 据************************************************************ define LCD_COMMAND 0 // Command define LCD_DATA 1 // Data define LCD_CLEAR_SCREEN 0x01 // 清屏 define LCD_HOMING 0x02 // 光标返回原点 void LCD_Write(bit style, unsigned char input) { LcdEn=0。 LcdRs=style。 LcdRw=0。 _nop_()。 DBPort=input。 _nop_()。 //注意顺序 LcdEn=1。 _nop_()。 //注意顺序 LcdEn=0。 _nop_()。 LCD_Wait()。 } //设置显示模式 ************************************************************ define LCD_SHOW 0x04 //显示开 define LCD_HIDE 0x00 //显示关 define LCD_CURSOR 0x02 //显示光标 define LCD_NO_CURSOR 0x00 //无光标 define LCD_FLASH 0x01 //光标闪动 define LCD_NO_FLASH 0x00 //光标不闪动 void LCD_SetDisplay(unsigned char DisplayMode) { LCD_Write(LCD_COMMAND, 0x08|DisplayMode)。 } //设置输入模式 ************************************************************。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。