16_单片机_LCD1602

1. LCD 1602涉及到芯片是HH44780,参考如下数据表

Gravitech LCD-16X2Y :https://www.mouser.cn/ProductDetail/Gravitech/LCD-16X2Y?qs=fkzBJ5HM%252BdBGmlQExG2rEA%3D%3D

2. 名词解释

 注:RAM可读写,掉电丢失;ROM只能读取,掉电不丢失。

 DDRAM:Display Data RAM,显示数据RAM

 CGRAM:Character Generator RAM,字符产生器RAM

 CDROM:Character Generator ROM,字符产生器ROM

3. LCD 1602

 LCD 1602,DDRAM显示地址

 

 注意:第一行结束地址 和 第二行开始地址 是不连续的。如下案例显示“8字符 * 2行”

 

 如下显示“16字符 * 2行”

 

 4. 字符显示 (5*8点阵模式 或 5*10点阵模式)

 

5. LCD 1602 接线图如下

6. 芯片寄存器选择,即LCD 1602基本操作

 

 7. 操作步骤

 a. 清屏,display clear  

  

 b. 功能设置,Function set

  

  

   DL:设置接口数据长度。当DL为1时,以8位长度(DB7到DB0)发送或接收数据;当DL为0时,以4位长度(DB7到DB4)表示。当选择4位长度时,必须发送数据或收到两次。
   N: 设置显示行数。
   F: 设置字符字体。

 c. 显示控制设置,Display on/off control

  

  D: 当D为1时,显示器打开,当D为0时,显示器关闭

  C: 当C为1时显示光标,而当C为0时不显示光标

     B: 当B为1时,光标指示的字符闪烁

 d. 设置进入模式,Entry mode set

  

   I/D:当为1时,地址加1;为0时,地址减1

  S:当为0时,显示不移动;为1时,I/D=0,向右移动,I/D=1,向左移动

8. 实验:LCD1602 显示两行数据

 a. LCD1602_源文件

  1 #include "lcd1602.h"
  2 
  3 /****************************************************
  4  * Function        : Lcd1602_Read_Busy
  5  * Description    : 从DB7读取Buzy状态,1为禁止
  6  * Parameters    : None
  7  * Return Value  : None
  8 *****************************************************/
  9 void Lcd1602_Read_Busy(void)
 10 {
 11     uchar state_DB = 0;
 12     // DB0~DB7默认为0xFF
 13     LCD1602_DB = 0xFF;
 14     
 15     LCD1602_RS = 0;
 16     LCD1602_RW = 1;
 17     
 18     do
 19     {
 20         LCD1602_EN = 1;
 21         state_DB = LCD1602_DB;
 22         LCD1602_EN = 0;
 23     }while(state_DB & 0x80);
 24 }
 25 
 26 /****************************************************
 27  * Function        : Lcd1602_Write_CMD
 28  * Description    : 向指令寄存器写入
 29  * Parameters    : cmd(8bit字符)
 30  * Return Value  : None
 31 *****************************************************/
 32 void Lcd1602_Write_CMD(uchar cmd)
 33 {
 34     // 检测busy,若处于busy状态,停留,直到busy为0
 35     Lcd1602_Read_Busy();
 36     
 37     // 指令写入条件
 38     LCD1602_RS = 0;
 39     LCD1602_RW = 0;
 40     
 41     LCD1602_DB = cmd;
 42     
 43     LCD1602_EN = 1;
 44     LCD1602_EN = 0;
 45 }
 46 
 47 /****************************************************
 48  * Function        : Lcd1602_Write_CMD
 49  * Description    : 向数据寄存器写入
 50  * Parameters   : dat(8bit字符)
 51  * Return Value  : None
 52 *****************************************************/
 53 void Lcd1602_Write_Data(uchar dat)
 54 {
 55     // 检测busy,若处于busy状态,停留,直到busy为0
 56     Lcd1602_Read_Busy();
 57     
 58     LCD1602_RS = 1;
 59     LCD1602_RW = 0;
 60     
 61     LCD1602_DB = dat;
 62     
 63     LCD1602_EN = 1;
 64     LCD1602_EN = 0;
 65 }
 66 
 67 /****************************************************
 68  * Function        : Lcd_Set_Cursor
 69  * Description    : 根据Y,选择第1行还是第2行
 70             然后根据X,选择字符位置
 71  * Parameters    : x, y
 72  * Return Value  : None
 73 *****************************************************/
 74 void Lcd_Set_Cursor(uchar x, uchar y)
 75 {
 76     uchar addr = 0;
 77     if(y==0)
 78     {
 79         addr = 0x00 + x;
 80     }
 81     else
 82     {
 83         addr = 0x40 + x;
 84     }
 85     
 86     Lcd1602_Write_CMD(addr|0x80);
 87 }
 88 
 89 /****************************************************
 90  * Function        : Lcd_Show_Str
 91  * Description    : LCD指定位置显示字符
 92  * Parameters    : 坐标x, 坐标y,显示字符
 93  * Return Value  : None
 94 *****************************************************/
 95 void Lcd_Show_Str(uchar x, uchar y, uchar *str)
 96 {
 97     Lcd_Set_Cursor(x, y);
 98     
 99     while(*str != '\0')
100     {
101         Lcd1602_Write_Data(*str++);
102     }
103 }
104 
105 void Lcd_Init(void)
106 {
107     Lcd1602_Write_CMD(0x01);    // Display Clear
108     Lcd1602_Write_CMD(0x0f);    // Set Cursor
109     Lcd1602_Write_CMD(0x38);    // Function Set
110     Lcd1602_Write_CMD(0x06);    // Entry mode set
111 }
View Code

 b. LCD1602_头文件

 1 #ifndef __LCD1602_H__
 2 #define __LCD1602_H__
 3 
 4 #include <reg51.h>
 5 
 6 #define uchar unsigned char
 7 
 8 /***************定义I/O***************/
 9 #define LCD1602_DB P0
10 
11 sbit LCD1602_RS = P2^6;
12 sbit LCD1602_RW = P2^5;
13 sbit LCD1602_EN = P2^7;
14 
15 /***************函数声明***************/
16 void Lcd_Init(void);
17 void Lcd_Show_Str(uchar x, uchar y, uchar *str);
18 
19 #endif
View Code

 c. main

1 #include "lcd1602.h"
2 
3 
4 void main(void)
5 {
6     Lcd_Init();
7     Lcd_Show_Str(0, 0, "www.home.com");
8     Lcd_Show_Str(0, 1, "www.h1xxe.xyz");
9 }

记录~~~

posted on 2023-06-25 13:07  Ivan2023  阅读(18)  评论(0编辑  收藏  举报