我这有个51 的 ds18b20+1602 的- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ=P1^4;//ds18b20与单片机连接口
- sbit RS=P2^0;//lcd与单片机连接口
- sbit RW=P2^1;
- sbit EN=P2^2;
- unsigned char code str1[]={"temperature: "};
- unsigned char code str2[]={" "};
- unsigned char temp[7]={48,48,48,48,'.',48,'\0'};
- uchar data disdata[5];
- uint temp_value;//温度值
- uchar mark;//温度正负标志
- /*****************************************************
- * Function Name : delay1ms()
- * Description : lcd 显示用延时
- * Input : 延时时间
- * Return : None
- ******************************************************/
- void delay1ms(unsigned int ms)//延时1毫秒(不够精确的)
- {unsigned int i,j;
- for(i=0;i<ms;i++)
- for(j=0;j<100;j++);
- }
- /*****************************************************
- * Function Name : wr_com()
- * Description : lcd 写指令
- * Input : 指令
- * Return : None
- ******************************************************/
- void wr_com(unsigned char com)//写指令//
- {
- delay1ms(1);
- RS=0;
- RW=0;
- EN=0;
- P0=com;
- delay1ms(1);
- EN=1;
- delay1ms(1);
- EN=0;
- }
- /*****************************************************
- * Function Name : wr_dat()
- * Description : lcd 写数据
- * Input : 数据
- * Return : None
- ******************************************************/
- void wr_dat(unsigned char dat)//写数据//
- { delay1ms(1);;
- RS=1;
- RW=0;
- EN=0;
- P0=dat;
- delay1ms(1);
- EN=1;
- delay1ms(1);
- EN=0;
- }
- /*****************************************************
- * Function Name : lcd_init()
- * Description : LCD初始化
- * Input : None
- * Return : None
- ******************************************************/
- void lcd_init()//初始化设置//
- {
- delay1ms(15);
- wr_com(0x38);delay1ms(5); //8 位数据,双列,5*7 字形
- wr_com(0x01);delay1ms(5); //清屏指令
- wr_com(0x06);delay1ms(5); //显示地址递增,即写一个数据后,显示位置右移一位
- wr_com(0x0c);delay1ms(5); //开启显示屏,关光标,光标不闪烁
- }
- /*****************************************************
- * Function Name : LCD_set_xy()
- * Description : 设置显示位置
- * Input : 显示坐标
- * Return : None
- ******************************************************/
- void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
- {
- unsigned char address;
- if (y == 0) address = 0x80 + x;
- else address = 0xc0 + x;
- wr_com( address);
- }
- /*****************************************************
- * Function Name : LCD_write_string()
- * Description : 向指定的地址写字符串
- * Input : 要写入的数据
- * Return : None
- ******************************************************/
- void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
- {
- LCD_set_xy( X, Y ); //写地址
- while (*s) // 写显示字符
- {
- wr_dat( *s );
- s ++;
- }
- }
- /*****************************************************
- * Function Name : init_play()
- * Description : 初始化显示
- * Input : 要写入的数据
- * Return : None
- ******************************************************/
- init_play()//初始化显示
- {
- lcd_init();
- LCD_write_string(0,0,str1);
- LCD_write_string(0,1,str2);
- }
- /*****************************************************
- * Function Name : delay_18B20()
- * Description : DS18B20 显示用延时
- * Input : 延时时间
- * Return : None
- ******************************************************/
- void delay_18B20(unsigned int i)//延时1微秒
- {
- while(i--);
- }
- /*****************************************************
- * Function Name : delay_18B20()
- * Description : DS18B20 显示用延时
- * Input : 延时时间
- * Return : None
- ******************************************************/
- void ds1820reset()/*ds1820复位*/
- { unsigned char x=0;
- DQ = 1; //DQ复位
- delay_18B20(1); //延时
- DQ = 0; //DQ拉低
- delay_18B20(60); //精确延时 大于480us
- DQ = 1; //拉高
- delay_18B20(15); // 大于60us
- }
- /*****************************************************
- * Function Name : ds1820rd()
- * Description : DS18B20 读数据
- * Input : None
- * Return : 读到的数据
- ******************************************************/
- uchar ds1820rd()//读数据
- { unsigned char i=0;
- unsigned char dat = 0;
- for (i=8;i>0;i--)
- {
- DQ = 0; //给脉冲信号
- dat>>=1;
- DQ = 1; //给脉冲信号
- if(DQ)
- dat|=0x80;
- delay_18B20(15);//大于60us
- }
- return(dat);
- }
- /*****************************************************
- * Function Name : ds1820rd()
- * Description : DS18B20 写数据
- * Input : None
- * Return : None
- ******************************************************/
- void ds1820wr(uchar wdata)//写数据
- {unsigned char i=0;
- for (i=8; i>0; i--)
- {
- DQ = 0;
- DQ = wdata&0x01;
- delay_18B20(15);//大于60us
- DQ = 1;
- wdata>>=1;
- }
- }
-
- /*****************************************************
- * Function Name : read_temp()
- * Description : 读取温度值并将读到的温度进行处理
- * Input : None
- * Return : temp_value 温度值
- ******************************************************/
- read_temp()//读取温度值并转换
- {
- uchar a,b;
- ds1820reset();
- ds1820wr(0xcc);//*跳过读序列号*/
- ds1820wr(0x44);//*启动温度转换*/
- ds1820reset();
- ds1820wr(0xcc);//*跳过读序列号*/
- ds1820wr(0xbe);//*读取温度*/
- a=ds1820rd();
- b=ds1820rd();
- temp_value=b;
- temp_value<<=8;
- temp_value=temp_value|a;
- if(temp_value<0x0fff) //如果为正数
- mark=0;
- else //如果为负数
- {
- temp_value=~temp_value+1;
- mark=1;
- }
- temp_value=temp_value*(0.625);//温度值扩大10倍,精确到1位小数
- return(temp_value);
- }
- /*****************************************************
- * Function Name : ds1820disp()
- * Description : 显示读取到的温度值
- * Input : None
- * Return : None
- ******************************************************/
- void ds1820disp()//温度值显示
- {
- temp[1]=temp_value/1000+0x30;//百位数
- temp[2]=temp_value%1000/100+0x30;//十位数
- temp[3]=temp_value%100/10+0x30;//个位数
- temp[5]=temp_value%10+0x30;//小数
- if(mark==0)
- temp[0]=0x20;//正温度不显示符号
- else
- temp[0]='-';//负温度显示负号:-
- if(temp[1]==0x30)
- {
- temp[1]=0x20;//如果百位为0,不显示
- if(temp[2]==0x30)
- {
- temp[2]=0x20;//如果百位为0,十位为0也不显示
- }
- }
- LCD_write_string(0,1,temp);
- }
- /*****************************************************
- * Function Name : main()
- * Description : 主程序
- * Input : None
- * Return : None
- ******************************************************/
- void main()
- {
- delay_18B20(1);
- init_play();//初始化显示
- while(1)
- {
- read_temp();//读取温度
- ds1820disp();//显示
- }
- }
|