基于stc12c560s2单片机 收集到土壤湿度传感器的数据并显示在 lcd1602的代码
时间: 2023-07-29 08:11:25 浏览: 56
以下是基于stc12c560s2单片机收集土壤湿度传感器数据并显示在lcd1602的代码:
```c
#include <REG52.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^6; //LCD1602的RS引脚,连接P2.6
sbit RW = P2^5; //LCD1602的RW引脚,连接P2.5
sbit EN = P2^4; //LCD1602的EN引脚,连接P2.4
sbit DQ = P3^4; //土壤湿度传感器的数据引脚,连接P3.4
void delay(uint i)
{
while(i--);
}
void init() //LCD1602初始化函数
{
RS = 0;
RW = 0;
EN = 0;
delay(1000);
P0 = 0x38; //8位数据总线,2行显示,5x7点阵字符,不显示光标,不移动屏幕
EN = 1;
_nop_();
EN = 0;
delay(1000);
P0 = 0x0c; //8位数据总线,2行显示,5x7点阵字符,不显示光标,不移动屏幕,显示开
EN = 1;
_nop_();
EN = 0;
delay(1000);
P0 = 0x01; //清屏
EN = 1;
_nop_();
EN = 0;
delay(1000);
}
void write_com(uchar com) //写命令函数
{
RS = 0;
RW = 0;
EN = 0;
P0 = com;
EN = 1;
_nop_();
EN = 0;
delay(100);
}
void write_data(uchar dat) //写数据函数
{
RS = 1;
RW = 0;
EN = 0;
P0 = dat;
EN = 1;
_nop_();
EN = 0;
delay(100);
}
void main()
{
uchar hum, temp;
init();
while(1)
{
DQ = 1; //开始采集数据
delay(1000);
DQ = 0; //读取数据
delay(20);
DQ = 1;
while(!DQ); //等待数据稳定
delay(5);
if(!DQ) //读取数据
{
hum = P3; //湿度数据存储在P3口
write_com(0x80); //将光标移动到第一行第一个位置
write_data('H');
write_data('U');
write_data('M');
write_data(':');
write_data(hum/10+'0'); //将湿度数据转换为字符显示
write_data(hum%10+'0');
delay(1000);
}
else
{
write_com(0x80); //将光标移动到第一行第一个位置
write_data('E');
write_data('R');
write_data('R');
write_data('O');
write_data('R');
delay(1000);
}
}
}
```
注意:上述代码仅为参考,实际使用时需要根据具体的电路和传感器型号进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)