⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd1602.c

📁 基于DTH11+LCD温湿度采集
💻 C
字号:
//LCD1602文件
#include<reg52.h>
#include <stdio.h>
#include <INTRINS.H>
#include <Lcd_1602.h>
#include <Time_Delay.h>
#define LCD_DATA P0	               //LCD1602 data transfer define

#define uint unsigned int
#define uchar unsigned char
/*只由主函数调用的 有
 Init_Lcd()
 LCD_write_str(uchar X,uchar Y,uchar *s)
 LCD_value(unsigned char x,unsigned char y,float f)
*/

sbit LCD_RS = P2^5;               //1602  control  define        
sbit RW = P2^6;
sbit LCD_E = P2^7;



/**********************************************************************

*****
#define LCD_SCREEN_ON  0x0C    //显示开
#define LCD_SCREEN_OFF  0x08    //显示关   
#define LCD_CURSOR_ON  0x0A  //显示光标
#define LCD_CURSOR_OFF    0x0c    //无光标       
#define LCD_C_FLASH_ON  0x0f    //有光标,光标闪动
#define LCD_C_FLASH_OFF  0x0e    //有光标,光标不闪动
//进入模式设置指令
#define LCD_AC_UP  0x06     //新数据后光标右移
#define LCD_AC_DOWN  0x04     //新数据后光标左移
#define LCD_S_MOVE_ON  0x05     // 画面可平移
#define LCD_S_MOVE_OFF  0x04     //画面不可平移
//设定显示屏或光标移动方向指令
#define LCD_C_LEFT  0x10     //光标左移1格,且AC值减1
#define LCD_C_RIGHT  0x11     //光标右移1格,且AC值加1
#define LCD_CHAR_LEFT  0x18     //显示器上字符全部左移一格,但光标不动
#define LCD_CHAR_RIGHT  0x1C     //显示器上字符全部右移一格,但光标不动
***********************************************************************

****/
//注  有主函数调用的函数都已作说明 其他函数一般不由主函数调用

/**********************************************************************

******
* 名    称:Init_Lcd() 主函数调用
* 功    能:Lcd初始化
* 入口参数:无
* 出口参数:无
* 范    例: 在主函数中直接调用
            
***********************************************************************

*****/

void Init_Lcd()				  //LCD初始化
{

 LCD_write_char(0x38,0);
 Delay_ms(1);
 LCD_write_char(0x38,0);
 Delay_ms(1);
 LCD_write_char(0x38,0);
 Delay_ms(1);
 LCD_write_char(0x0c,0);
 Delay_ms(1);
 LCD_write_char(0x06,0);
 Delay_ms(1);
 LCD_write_char(0x0c,0);
 Delay_ms(1); 
 //
}

/**********************************************************************

******
* 名    称:LCD_write_str(uchar X,uchar Y,uchar *s)主函数调用	
* 功    能:在指定地址写一个字符串   eg:Y=0,1,2,3,4,5,6,7,8,9,10...15。 

 X=0,1。
* 入口参数:X:横坐标 Y:纵坐标 *s:字符串首地址	
* 出口参数:无
* 范    例: LCD_write_str(1,1,uchar *s)
            
***********************************************************************

*****/
void LCD_write_str(unsigned char X,unsigned char Y,unsigned char *s)
  {
    LCD_write_char(0,' ');
    LCD_set_xy( X, Y ); 	//写地址    
    while (*s)  			// 写显示字符
      {
        LCD_write_char( 0, *s );
		s ++;
      }
  }



/**********************************************************************

******
* 名    称:LCD_set_xy( uchar x, uchar y ) 	    the optic sign  

flash?
* 功    能:指定一个地址
* 入口参数:X:横坐标 Y:纵坐标 
* 出口参数:无
* 范    例: LCD_set_xy(5,1)
            
*************CD_set_xy*************************************************

**************/
void LCD_set_xy( uchar x, uchar y )  //写地址函数
  {
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else 
       address = 0xc0 + x;
    LCD_write_char( address, 0 );
  }



/**********************************************************************

******
* 名    称:LCD_en_write(void)  
* 功    能:液晶使能
* 入口参数:无
* 出口参数:无
* 范    例: 直接调用
            
*************CD_set_xy*************************************************

**************/
void LCD_en_write(void)  	//液晶使能
{
  _nop_();
  LCD_E=1;//EN=1
  _nop_();
  LCD_E=0;//EN=0
}
//------------------------------------------------

/**********************************************************************

******
* 名    称:LCD_write_char(uchar cd,uchar ab)   
* 功    能:写指令或数据  当写ab时 应使cd=0	 当cd不为0 则写cd  且ab

的赋值无效
* 入口参数:cd:指令内容	ab:数据内容	 指令常量已在上面定义  但一般不

* 出口参数:无
* 范    例: LCD_write_char( 0, ‘f’)	
            
*************LCD_set_xy************************************************

***************/
void LCD_write_char(uchar cd,uchar ab) // 写数据
{ 
 Delay_us(20);
 if(cd==0)
 {
  LCD_RS=1;  				//RS=1,写显示内容  
  LCD_byte(ab);
 }
 else
 {
  LCD_RS=0;   				//RS=0,写命令
  LCD_byte(cd);
  }
}



/**********************************************************************

******
* 名    称:LCD_byte(abc);  
* 功    能:写一个字符到 or called one byte to LCD中
* 入口参数:
* 出口参数:无

            
*************LCD_set_xy************************************************

***************/
void LCD_byte(unsigned char abc)  
{
 RW = 0;
 LCD_E = 0;
 LCD_DATA = abc;
 LCD_en_write();
}
  
//在液晶中显示浮点数函数
LCD_value(unsigned char x,unsigned char y,float f)
{
 unsigned char str[15];                  //不能定义为char* str,数组长

度一定要大于浮点数的总位数
sprintf(str,"%.1f",f);                //1表示小数位数 小数太多 自动四舍

五入
LCD_write_str( x, y, str);
return 0;
}

//主函数文件
#include <reg52.h>
#include <intrins.h>
#include <Lcd_1602.h>
#include <Time_Delay.h>
#include"DHT11.h"


extern float F16T,F16RH;   //全局变量声明
void main ()
{
  Init_Lcd();
  LCD_write_str(0,1,"abc");	 //液晶预显示测试
  LCD_value(0,0,34.345);
  Delay_ms(2000);

  Init_Lcd();
 
    while(1)
  {
 
  getDHT11();
  LCD_write_str(0,0,"T=");
  LCD_value(3,0,F16T);  LCD_write_str(8,0,"\"C");       //字符" 应用转

义格式
   LCD_write_str(0,1,"RH=");
  LCD_value(4,1,F16RH);	  LCD_write_str(9,1,"%");
  Delay_ms(500);
  }
  
}

//延时函数文件
//以下为延时函数 this is fit to old  C51  12MHz,   12 devide freqency
void Delay_ms(unsigned int n)//n毫秒延时
{ 
 unsigned char j
 while(n--)
 {
   for(j=0;j<125;j++);
 }
}

void Delay_us(unsigned char n)       //N us延时函数   	 精度 ±4us
  {
   n=n/2;
   while(--n);
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -