LCD1602温湿显示,全部在附件里。
我家原先有个温湿度显示器,用了几年坏了;我想自己做一个,在网上套淘了STH10传感器,用STC15F2K60S2单片机组成最小系统,用C语言编程调试完成。显示温湿度效果还可以见图片。 编者:ROCKER(石头)
单片机源程序如下:
- /****************************************************************
- ;功能说明:sht11和1602的温湿度显示
- ;微处理器:STC15f2k60s2
- ;编译环境:Keil uVision V4.0
- ;创建日期:2025-9-24
- ;版 本:V1.0
- ;修改日期:2026.5.25 OK
- **********************************************************/
- /*************定义接口********************
- P0.x------DB0~DB7(LCD1602)
- P2.6------RS (LCD1602)
- P2.5------RW (LCD1602)
- P2.4------E (LCD1602)
- P2.1------DATA (SHT1x/7x)
- P2.2------SCK (SHT1x/7x)
- *****************************************/
- #include "stc15f.h"
- #include <intrins.h>
- #include <math.h>
- #include <stdio.h>
- //***************第一部分LCD1602设置 START**********************
- #define LCD_DB P0
- sbit LCD_RS=P2^6;
- sbit LCD_RW=P2^5;
- sbit LCD_E=P2^4;
- /******定义函数****************/
- #define uchar unsigned char
- #define uint unsigned int
- bit lcd_busy();
- void LCD_init(void); //初始化函数
- void LCD_write_command(uchar command); //写指令函数
- void LCD_write_data(uchar dat);
- void LCD_disp_char(uchar x,uchar y,uchar dat);
- void LCD_disp_str(uchar x,uchar y,uchar *str);
- void delay_n10us(uint n);
- bit lcd_busy()
- {
- bit result;
- LCD_RS= 0;
- LCD_RW=1;
- LCD_E=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- result = (bit)(P0 & 0x80);
- LCD_E=0;
- return result;
- }
- /*--------------------------------------
- ;模块名称:LCD_init();
- ;功 能:初始化LCD1602
- ;-------------------------------------*/
- void LCD_init(void)
- {
- delay_n10us(10);
- LCD_write_command(0x38);
- delay_n10us(10);
- LCD_write_command(0x0c);//整体显示,关光标,不闪烁
- delay_n10us(10);
- LCD_write_command(0x06);//设定输入方式,增量不移位
- delay_n10us(10);
- LCD_write_command(0x01);
- delay_n10us(100);
- }
- /*--------------------------------------
- ;模块名称:LCD_write_command();
- ;功 能:LCD1602写指令函数
- ;参数说明:dat为写命令参数
- ;-------------------------------------*/
- void LCD_write_command(uchar dat)
- {
- while(lcd_busy());
- delay_n10us(10);
- LCD_RS=0; //指令
- LCD_RW=0; //写入
- LCD_E=1; //允许
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- /*--------------------------------------
- ;模块名称:LCD_write_data();
- ;功 能:LCD1602写数据函数
- ;参数说明:dat为写数据参数
- ;-------------------------------------*/
- void LCD_write_data(uchar dat)
- {
- while(lcd_busy());
- delay_n10us(10);
- LCD_RS=1; //数据
- LCD_RW=0; //写入
- LCD_E=1; //允许
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- /*--------------------------------------
- ;模块名称:LCD_disp_char();
- ;功 能:LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
- ;-------------------------------------*/
- void LCD_disp_char(uchar x,uchar y,uchar dat)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- LCD_write_data(dat);
- }
- /*--------------------------------------
- ;模块名称:LCD_disp_str();
- ;功 能:LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y(1-2)}上显示一个字符串。
- ;-------------------------------------*/
- void LCD_disp_str(uchar x,uchar y,uchar *str)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- while(*str!='\0')
- {
- LCD_write_data(*str);
- str++;
- }
- }
- /*--------------------------------------
- ;模块名称:delay_n10us();
- ;功 能:延时函数,延时约n个10us
- ;修改说明:修改为较精确的延时函数,"_nop_()"延时1us@12M晶振
- ;-------------------------------------*/
- void delay_n10us(uint n)
- {
- uint i;
- for(i=n;i>0;i--)
- {
- _nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();
- }
- }
- //*********************第二部分sht10设置 START****************************************
- sbit SCK = P2^2;
- sbit DATA = P2^1;
- typedef union
- { unsigned int i;
- float f;
- }
- value;
- enum {TEMP,HUMI};
-
- #define noACK 0 //用于判断是否结束通讯
- #define ACK 1 //结束数据传输
- //adr command r/w
- #define STATUS_REG_W 0x06 //000 0011 0
- #define STATUS_REG_R 0x07 //000 0011 1
- #define MEASURE_TEMP 0x03 //000 0001 1
- #define MEASURE_HUMI 0x05 //000 0010 1
- #define RESET 0x1e //000 1111 0
- /****************定义函数****************/
- void s_transstart(void);
- void s_connectionreset(void);
- char s_write_byte(unsigned char value);
- char s_read_byte(unsigned char ack);
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数
- void calc_dht90(float *p_humidity ,float *p_temperature);//温湿度补偿
- /*--------------------------------------
- ;模块名称:s_transstart();
- ;功 能:启动传输函数
- ;-------------------------------------*/
- void s_transstart(void)
- // generates a transmission start
- {
- DATA=1; SCK=0;
- _nop_();
- SCK=1;
- _nop_();
- DATA=0;
- _nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();
- DATA=1;
- _nop_();
- SCK=0;
- }
- /*--------------------------------------
- ;模块名称:s_connectionreset();
- ;功 能:连接复位函数
- ;-------------------------------------*/
- void s_connectionreset(void)
-
- {
- unsigned char i;
- DATA=1; SCK=0;
- for(i=0;i<9;i++)
- {
- SCK=1;
- SCK=0;
- }
- s_transstart();
- }
- /*--------------------------------------
- ;模块名称:s_write_byte();
- ;功 能:SHT1x/7x写函数
- ;-------------------------------------*/
- char s_write_byte(unsigned char value)
- //----------------------------------------------------------------------------------
-
- {
- unsigned char i,error=0;
- for (i=0x80;i>0;i/=2)
- {
- if (i & value) DATA=1;
- else DATA=0;
- SCK=1;
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- DATA=1;
- SCK=1;
- error=DATA;
- _nop_();_nop_();_nop_();
- SCK=0;
- DATA=1;
- return error;
- }
-
- /*--------------------------------------
- ;模块名称:s_read_byte();
- ;功 能:SHT1x/7x读函数
- ;-------------------------------------*/
- char s_read_byte(unsigned char ack)
-
- {
- unsigned char i,val=0;
- DATA=1;
- for (i=0x80;i>0;i/=2)
- { SCK=1;
- if (DATA) val=(val | i);
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- if(ack==1)DATA=0;
- else DATA=1;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();_nop_();_nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- DATA=1;
- return val;
- }
-
- /*--------------------------------------
- ;模块名称:s_measure();
- ;功 能:测量温湿度函数
- ;-------------------------------------*/
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
-
- {
- unsigned error=0;
- unsigned int i;
-
- s_transstart();
- switch(mode)
- {
- case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
- case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
- default : break;
- }
- for (i=0;i<65535;i++) if(DATA==0) break;
- if(DATA) error+=1;
- *(p_value) =s_read_byte(ACK);
- *(p_value+1)=s_read_byte(ACK);
- *p_checksum =s_read_byte(noACK);
- return error;
- }
-
- /*--------------------------------------
- ;模块名称:calc_dht90();
- ;功 能:温湿度补偿函数
- ;-------------------------------------*/
- void calc_dht90(float *p_humidity ,float *p_temperature)
- { const float C1=-4.0;
- const float C2=+0.0405;
- const float C3=-0.0000028;
- const float T1=+0.01;
- const float T2=+0.00008;
- float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
- float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
- float rh_lin; // rh_lin: Humidity linear
- float rh_true; // rh_true: Temperature compensated humidity
- float t_C; // t_C : Temperature [C]
- t_C=t*0.01 - 40; //calc. temperature from ticks to [C]
- rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
- if(rh_true>100)rh_true=100;
- if(rh_true<0.1)rh_true=0.1;
- *p_temperature=t_C;
- *p_humidity=rh_true;
- }
- //*********************第二部分SHT1x/7x设置 END****************************************
- //*********主函数*****************
- void main(void)
- {
- value humi_val,temp_val;
- unsigned char error,checksum;
- unsigned int wendu,shidu;
- LCD_init();
- s_connectionreset();
- LCD_disp_str(0,1,"TE ");
- LCD_disp_str(0,2,"RH ");
- //*********初始化温度显示区*********
- LCD_disp_str(2,1, "TTT.T \xDF\C");
- //*********初始化湿度显示区*********
- LCD_disp_str(2,2, "RRR.R%RH");
- delay_n10us(20000); //延时0.2s
- while(1)
- { error=0;
- error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
- error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
- if(error!=0) s_connectionreset();
- else
- { humi_val.f=(float)humi_val.i;
- temp_val.f=(float)temp_val.i;
- calc_dht90(&humi_val.f,&temp_val.f);
- wendu=10*temp_val.f;
- LCD_disp_char(2,1,wendu/1000+'0'); //显示温度百位
- LCD_disp_char(3,1,(wendu%1000)/100+'0'); //显示温度十位
- LCD_disp_char(4,1,(wendu%100)/10+'0'); //显示温度个位
- LCD_disp_char(6,1,(wendu%10)+'0'); //显示温度小数点后第一位
- shidu=10*humi_val.f;
- LCD_disp_char(2,2,shidu/1000+'0'); //显示湿度百位
- LCD_disp_char(3,2,(shidu%1000)/100+'0'); //显示湿度十位
- LCD_disp_char(4,2,(shidu%100)/10+'0'); //显示湿度个位
- LCD_disp_char(6,2,(shidu%10)+'0'); //显示湿度小数点后第一位
- }
- //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
- delay_n10us(80000); //延时约0.8s
- }
- }
- //end 2026-5-25 OK
复制代码
程序 原理图:
LCD1602温度湿度显示.7z
(49.4 KB, 下载次数: 0)
|