本实验适51单片机开发板,需要接上带字库的LCD12864才能正常显示,如果接上后没有显示,请调节LCD12864背后的小 电位器进行对比度调节。
另外除A2、A3、A4和V2.0开发板以外的51开发板,还需要另外在温度模块输出管脚接线到P37。程序初始化较慢,下载完程序后需要稍等才能显示,属于正常现象。
程序还有许多可以优化或添加功能的地方,大家可以自行研究学习。
制作出来的实物图如下:
这里给出完整代码供参考。
单片机源程序如下:
- #include<ds18b20.h>
- uchar HZ1[]="温度曲线";
- uchar code table[]={0x00,0x00,0x00,0x00,0x30,0x00,0x48,0x00,0x48,
- 0x70,0x30,0x88,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x80,
- 0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //自定义字符:显示℃
- void delay_us(uchar i) //精确延时---- T=2*i+5 单位:us
- {
- while(--i);
- }
- void Reset_DS18B20() //复位DS18B20
- {
- DQ=1;
- DQ=0;
- delay_us(494); //低电平至少保持480——960us
- DQ=1; //释放总线,准备接收存在脉冲---低电平
- delay_us(200);
- }
- void write_DS18B20(uchar value) //写DS18B20指令
- {
- uchar i = 0;
- for(i = 0; i < 8; i ++)
- {
- DQ = 1;
- DQ = 0;
- delay_us(10);
- DQ = value & 0x01;
- delay_us(40);
- DQ = 1;
- value >>= 1;
- }
- }
- uchar Read_DS18B20(void) //读DS18b20
- {
- uchar i=0;
- uchar value=0;
- for(i=0;i<8;i++)
- {
- DQ=1;
- DQ=0;
- delay_us(10);
- DQ=1; //将总线释放,以便读取数值
- delay_us(2);
- value>>=1;
- if(DQ)
- value |=0x80; //先读MSB,后读LSB
- delay_us(34);//在60us内完成读取
- }
- return value;
- }
- uchar Read_temp()
- {
- uchar temp;
- uchar temp_L,temp_H;
- Reset_DS18B20();
- write_DS18B20(0xCC);
- write_DS18B20(0x44);
- Reset_DS18B20();
- write_DS18B20(0xCC);
- write_DS18B20(0xBE);
- temp_L=Read_DS18B20(); //读温度的低8位
- temp_H=Read_DS18B20(); //再读温度高8位
-
- temp=(temp_H*256+temp_L)/16;//将十六进制转化为10进制
- return temp; //返回当前温度的值
- /**注:没对复位度进行处理**/
- }
复制代码
- #include"temp.h"
- uchar code table_[]={0x00,0x00,0x00,0x00,0x30,0x00,0x48,0x00,0x48,
- 0x70,0x30,0x88,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x80,
- 0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //自定义字符:显示℃
- uchar HZ[]="温度曲线";
- /*******************************************************************************
- * 函数名 : Delay1ms
- * 函数功能 : 延时函数
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- void Delay1ms(unsigned int y)
- {
- unsigned int x;
- for(y;y>0;y--)
- for(x=110;x>0;x--);
- }
- /*******************************************************************************
- * 函数名 : Ds18b20Init
- * 函数功能 : 初始化
- * 输入 : 无
- * 输出 : 初始化成功返回1,失败返回0
- *******************************************************************************/
- unsigned char Ds18b20Init()
- {
- unsigned int i;
- DSPORT=0; //将总线拉低480us~960us
- i=70;
- while(i--);//延时642us
- DSPORT=1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
- i=0;
- while(DSPORT) //等待DS18B20拉低总线
- {
- i++;
- if(i>5000)//等待>5MS
- return 0;//初始化失败
- }
- return 1;//初始化成功
- }
- /*******************************************************************************
- * 函数名 : Ds18b20WriteByte
- * 函数功能 : 向18B20写入一个字节
- * 输入 : com
- * 输出 : 无
- *******************************************************************************/
- void Ds18b20WriteByte(unsigned char dat)
- {
- unsigned int i,j;
- for(j=0;j<8;j++)
- {
- DSPORT=0; //每写入一位数据之前先把总线拉低1us
- i++;
- DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
- i=6;
- while(i--); //延时68us,持续时间最少60us
- DSPORT=1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
- dat>>=1;
- }
- }
- /*******************************************************************************
- * 函数名 : Ds18b20ReadByte
- * 函数功能 : 读取一个字节
- * 输入 : com
- * 输出 : 无
- *******************************************************************************/
- unsigned char Ds18b20ReadByte()
- {
- unsigned char byte,bi;
- unsigned int i,j;
- for(j=8;j>0;j--)
- {
- DSPORT=0;//先将总线拉低1us
- i++;
- DSPORT=1;//然后释放总线
- i++;
- i++;//延时6us等待数据稳定
- bi=DSPORT; //读取数据,从最低位开始读取
- /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
- byte=(byte>>1)|(bi<<7);
- i=4; //读取完之后等待48us再接着读取下一个数
- while(i--);
- }
- return byte;
- }
- /*******************************************************************************
- * 函数名 : Ds18b20ChangTemp
- * 函数功能 : 让18b20开始转换温度
- * 输入 : com
- * 输出 : 无
- *******************************************************************************/
- void Ds18b20ChangTemp()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0x44); //温度转换命令
- // Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
-
- }
- /*******************************************************************************
- * 函数名 : Ds18b20ReadTempCom
- * 函数功能 : 发送读取温度命令
- * 输入 : com
- * 输出 : 无
- *******************************************************************************/
- void Ds18b20ReadTempCom()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0xbe); //发送读取温度命令
- }
- /*******************************************************************************
- * 函数名 : Ds18b20ReadTemp
- * 函数功能 : 读取温度
- * 输入 : com
- * 输出 : 无
- *******************************************************************************/
- //int Ds18b20ReadTemp()
- //{
- // int temp=0;
- // unsigned char tmh,tml;
- // Ds18b20ChangTemp(); //先写入转换命令
- // Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
- // tml=Ds18b20ReadByte(); //读取温度值共16位,先读低字节
- // tmh=Ds18b20ReadByte(); //再读高字节
- // temp=tmh;
- // temp<<=8;
- // temp|=tml;
- // return temp;
- //}
- int Ds18b20ReadTemp()
- {
- int temp=0;
- float tp;
- unsigned char tmh,tml;
- Ds18b20ChangTemp(); //先写入转换命令
- Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
- tml=Ds18b20ReadByte(); //读取温度值共16位,先读低字节
- tmh=Ds18b20ReadByte(); //再读高字节
- temp=tmh;
- temp<<=8;
- temp |=tml;
- if(temp < 0)
- {
- temp=temp-1;
- temp=~temp;
- tp=temp;
- temp=tp*0.0625*100+0.5;
- }
- else
- {
- tp=temp;
- temp=tp*0.0625*100+0.5;
- }
- return temp/100;
- }
复制代码- /***********************************************************
- 功能:12864显示温度曲线
- 接线:DS18B20输出管脚接到P37
- ************************************************************/
- #include"bmp.h"
- #include"12864.h"
- #include"temp.h"
- void main()
- {
- uchar i,j,color=1,num;
- init_LCD();
- while(1)
- {
- write_LCD_command(0x30);
- write_LCD_command(0x80);//基本指令,显示汉字
- for(i=0;i<8;i++)
- {
- write_LCD_data(HZ[i]);
- }
- Clear_GDRAM(); //清楚GDRAM区的内容
- write_LCD_command(0x36);//扩展指令,绘图打开
- display_BMP(ZB);//显示坐标
- for(i=13;i<128;i++) //打点
- {
- j=70-Ds18b20ReadTemp();
- Draw_dots(i,j,1);
- delay(40000);
- delay(40000);
- write_LCD_command(0x30);
- write_LCD_command(0x84); //切换到基本指令显示字符
- write_LCD_data(':');
- write_LCD_data(Ds18b20ReadTemp()/100+0x30);
- write_LCD_data(Ds18b20ReadTemp()/10+0x30);
- write_LCD_data(Ds18b20ReadTemp()%10+0x30);//显示温度的值
-
- write_LCD_command(0x40); //自定义第一个CGRAM,编码为0000
- for(num=0;num<32;num++)
- {
- write_LCD_data(table_[num]);
- }
- write_LCD_command(0x86); //规定CGRAM字符显示位置
- write_LCD_data(0x00);
- write_LCD_data(0x00);
- }
- }
-
- }
复制代码
Keil代码下载:
12864液晶显示温度.zip
(57.24 KB, 下载次数: 229)
|