实验09 DS18B20及LCD 1602(温度检测与显示) 您所在的位置:网站首页 DS18b20传感器在仿真时温度显示过高 实验09 DS18B20及LCD 1602(温度检测与显示)

实验09 DS18B20及LCD 1602(温度检测与显示)

2024-03-14 04:10| 来源: 网络整理| 查看: 265

文末有源文件!

实验内容:单片机开机读取 ds18b20 检测到的温度,并显示在 1602 上,精确到0.1度;能显示超过100的温度和零下温度。温度值后面要加上符号:℃

提示:在lcd 1602上总共需要6位显示温度值: ???. ?℃

其中的.和℃都是固定位置的固定符号,可在程序里写好。

另外4个?号处的温度值要实时的从18b20读取。

当温度为零下时,最高位?号处显示为-号;

当温度为0及以上时,最高位不显示+号,且不为0;其中当温度值>=100,该处显示1。

 

1.首先是LCD1602模块,用于把读出的温度进行展示,LcdStar()用于对显示器的初始化,可以按照所需进行定制初始化。LcdWriteDat()表示向LCD写入字节类型的数据,LcdSetCursor(unsigned char x, unsigned char y)表示写入字节的位置,即屏幕坐标。LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)就是对两个函数的综合。代码如下。 #include #define LCD1602_DB P0 sbit LCD1602_RS = P2^0; sbit LCD1602_RW = P2^1; sbit LCD1602_E = P2^2; void InitLcd1602(); int n=0; int i=0; int j=0; int l=0; int ii=0; void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str); void LcdWriteCmd(unsigned char cmd); void delay(unsigned int j); void LcdStar() { unsigned char str1[] = "Welcome to aau"; unsigned char str2[] = "No .5 "; unsigned char str3[] = " "; unsigned char str4[] = " "; // unsigned char tab[]="Temp="; InitLcd1602();/* 初始化 1602 液晶 */ // LcdShowStr(2, 0, str); // LcdShowStr(1, 1, tab); LcdShowStr(9, 1, "...");//默认初始化温度00 // LcdShowStr(12, 1,0XDF); LcdShowStr(14, 1,"C");//添加C温度 // LcdShowStr(1, 1, str2); for(i=0;i>8); //定时器重载值拆分为高低字节 T0RL = (unsigned char)tmp; TMOD &= 0xF0; //清零 T0 的控制位 TMOD |= 0x01; //配置 T0 为模式 1 TH0 = T0RH; //加载 T0 重载值 TL0 = T0RL; ET0 = 1; //使能 T0 中断 TR0 = 1; //启动 T0 } /* T0 中断服务函数,完成 1 秒定时 */ void InterruptTimer0() interrupt 1 { static unsigned char tmr1s = 0; TH0 = T0RH; //重新加载重载值 TL0 = T0RL; tmr1s++; if (tmr1s >= 100) //定时 1s { tmr1s = 0; flag1s = 1; } }

原理图以及hex文件连接如下:

链接:https://pan.baidu.com/s/1WR04LHYcTlr2i-M_9fbwnA?pwd=h4e0  提取码:h4e0  闫老师可以提问问题:负小数是怎么显示出来的:对读出的十一位数据位进行反码加一,转换成再十进制,代码中表示如下:

转换成十进制后,如果小数小于零,就对其进行十减,整数部分进行取反。

整个工程如下添加头文件操作如下: 

添加时,头文件和主函数在同一个目录,编译后就会变成下面。 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有