} T_RST=1; write_ds1302(add|0x01); temp=read_ds1302(); T_CLK=1; T_RST=0; return(temp); void w_1302(uchar add,uchar dat) { } #endif 3、DS18B20温度检测流程图(5)如下: 图(5) DS18B20温度检测流程图 LCD显示 温度值转换 读取温度 初始化DS18B20 T_RST=0; T_CLK=0; T_RST=1; write_ds1302(add); write_ds1302(dat); T_CLK=1; T_RST=0; DS18B20部分程序: 29
#ifndef __ds18b20_h__ #define __ds18b20_H__ sbit DQ = P2^3; //定义DS18B20端口DQ bit stemp; bit sflag; uint temp_value; uchar tempbuffer[7]; uchar data temp_data[2] = {0x00,0x00}; uchar data display[5] = {0x00,0x00,0x00,0x00,0x00}; uchar code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04, 0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; void delay(uint s) { while(s--); } void delay_15us(uchar n) { } void Init_DS18B20(void) { DQ=1; delay_15us(38); 30
do { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); n--; } while(n) ;
} DQ=0; delay_15us(38); DQ=1; delay_15us(2); delay_15us(25); uchar ReadOneChar(void) { return (dat); } void WriteOneChar(uchar dat) { } 31
uchar i = 0; uchar dat = 0; for (i = 8; i > 0; i--) { DQ = 0; // 给脉冲信号 dat >>= 1; DQ = 1; // 给脉冲信号 if(DQ) dat |= 0x80; delay(2); } unsigned char i = 0; for (i = 8; i > 0; i--) { } DQ = 0; DQ = dat&0x01; delay(2); DQ = 1; dat>>=1;
void Read_Temperature(void) { } #endif 4、液晶LCD1602显示部分程序设计流程图(6)如下: 图(6) 液晶LCD1602显示部分程序设计流程图 结束 输入显示数据 设定数据位置 LCD1602初始化 开始 Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器 temp_data[0] = ReadOneChar(); //温度低8位 temp_data[1] = ReadOneChar(); //高8位 Init_DS18B20(); LCD1602部分程序: #ifndef __lcd1602_h__ #define __lcd1602_H__ #define LCD_DB P0//定义LCD的数据端口 sbit LCD_RS=P2^0; 32
sbit LCD_RW=P2^1; sbit LCD_E=P2^2; #define uchar unsigned char #define uint unsigned int //===========延时子函数======================== void delay_lcd(uint x) {uint i,j; for(i=x;i>0;i--) for(j=0;j<2;j++); } //==================写指令函数================= void LCD_write_command(uchar command) {LCD_DB=command; LCD_RS=0;//指令 LCD_RW=0;//写入 LCD_E=1; delay_lcd(100); LCD_E=0; delay_lcd(1);//等待执行完毕。。。 } //===================写数据函数================= void LCD_write_data(uchar dat) {LCD_DB=dat; LCD_RS=1;//数据寄存器 LCD_RW=0;//写入数据 LCD_E=1; delay_lcd(100); LCD_E=0; delay_lcd(1);//等待程序执行完毕。。。 } void LCD_init(void) {LCD_write_command(0x38);//设置8位格式,2行,5*7 LCD_write_command(0x0c);//整体显示,关光标,不闪烁 LCD_write_command(0x06);//设定输入方式,增量不移位 LCD_write_command(0x01);//清屏显示 delay_lcd(20); 33