[STM32F7] stm32驱动 LCD1602

[复制链接]
 楼主| 狗啃模拟 发表于 2022-5-29 12:58 | 显示全部楼层 |阅读模式
一、LCD1602简介
LCD1602液晶显示器是广泛使用的一种字符型液晶显示模块。它是由字符型液晶显示屏(LCD)、控制驱动主电路HD44780及其扩展驱动电路HD44100,以及少量电阻、电容元件和结构件等装配在PCB板上而组成。
LCD1602应用很广泛,无论是各大电子公司的产品上还是各大高校电赛的比赛作品上都能看到它的身影,下面来细说一下怎么驱动这块1602液晶屏。

 楼主| 狗啃模拟 发表于 2022-5-29 12:59 | 显示全部楼层
二、LCD1602引脚
677826292fd96edd3d.png

 楼主| 狗啃模拟 发表于 2022-5-29 13:00 | 显示全部楼层
图1:引脚说明
999526292fdc171b39.png
 楼主| 狗啃模拟 发表于 2022-5-29 13:00 | 显示全部楼层
一、我们重点关注几个引脚:

液晶显示偏压:VL对应原理图V0引脚,作用是调整1602的显示对比度,可外接电位器进行调节对比度,上图原理图接地引脚电压为0这时候对比度最高。
数据/命令选择端:RS对应原理图RES引脚,引脚高电平:进行数据字节传输,引脚低电平:进行命令字节传输。
读/写选择端:R/W对应原理图R/W引脚,引脚高电平:对1602进行读数据,引脚低电平:对1602进行写数据,一般应用都是直接拉低只进行写数据。
使能信号:E对应原理图E引脚,该引脚上升沿代表对1602开始数据传输,下降沿代表数据传输结束。
 楼主| 狗啃模拟 发表于 2022-5-29 13:01 | 显示全部楼层
背光控制:原理图K+引脚,该引脚高电平:背光关闭,引脚低电平:背光打开。
二、我们只需要写数据给1602显示,只看写操作时序:

2写指令跟4写数据对比可看出RW读写引脚为低电平,E为高电平,D0~D7为传输的数据是命令/数据,RS数据/命令选择端(高:数据 , 低:命令)。
 楼主| 狗啃模拟 发表于 2022-5-29 13:01 | 显示全部楼层
 楼主| 狗啃模拟 发表于 2022-5-29 13:02 | 显示全部楼层
 楼主| 狗啃模拟 发表于 2022-5-29 13:03 | 显示全部楼层
三、常用的写指令如下,其他指令可去查1602的datasheet:
 楼主| 狗啃模拟 发表于 2022-5-29 13:04 | 显示全部楼层
 楼主| 狗啃模拟 发表于 2022-5-29 13:05 | 显示全部楼层
四、数据写入CGRAM指令:

此指令可以自定义显示一个字符,我们写地址的丝毫应该是0x40+Address
 楼主| 狗啃模拟 发表于 2022-5-29 13:06 | 显示全部楼层
 楼主| 狗啃模拟 发表于 2022-5-29 13:07 | 显示全部楼层
三、LCD1602驱动(11脚)
51单片机跟STM32单片机的驱动基本一致主要是引脚的配置不怎么一样,特别注意STM32驱动写指令/数据GPIO_Write(GPIOA,(GPIO_ReadOutputData(GPIOA) & 0xff00) | cmd/data)为对电平的读取再写数据,其他均与51驱动一致。
 楼主| 狗啃模拟 发表于 2022-5-29 13:07 | 显示全部楼层
1、51单片机:

  1. LCD1602.h
  2. #ifndef __LCD1602_H
  3. #define __LCD1602_H

  4. #define LCD1602_BKL_ON  0 //背光开
  5. #define LCD1602_BKL_OFF  1 //背光关

  6. #define LCD1602_DB P2   //数据端口  D0~D7
  7. sbit LCD_RES = P4^1;   //1602的数据/指令选择控制线
  8. sbit LCD_EN = P4^2;    //1602的使能控制线
  9. sbit Lcd1602_light = P0^2;      //背光引脚

  10. /*****************************************************************************/
  11. void LCD1602_Init();
  12. void LCD1602_Clear();

  13. void LCD1602_write_cmd(unsigned char cmd);
  14. void LCD1602_write_data(unsigned char date);
  15. void LCD1602_wtire_CGRAM();

  16. void LCD1602_SetCursor(unsigned char x,unsigned char y);
  17. void LCD1602_ShowChar(unsigned char xpos,unsigned char ypos,unsigned char xsz);
  18. void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p);
  19. void LCD1602_ShowNum(char x, char y, unsigned int num);

  20. void LCD1602_BKLSet(unsigned char val);
  21. unsigned char LCD1602_BKLGet();

  22. void Delay_ms(unsigned int nms);
  23. #endif
复制代码
 楼主| 狗啃模拟 发表于 2022-5-29 13:08 | 显示全部楼层
2、51单片机:

  1. LCD1602.h
  2. #include "LCD1602.h"

  3. /******************************************************************************
  4. * 函数名称:void Delay_ms(unsigned int nms)           *
  5. * 函数功能:写命令函数                     *
  6. * 输入参数: //nms为要延时的ms数                *
  7. * 返回值  :无                         *
  8. * 其他说明:                         *
  9. ******************************************************************************/
  10. void Delay_ms(unsigned int nms)  //@11.0592MHz{
  11. unsigned char i, j; //用单片机小工具根据自己的单片机类型及晶振直接生成对应的延时函数即可
  12. while(nms--)
  13. {
  14.   i = 15;
  15.   j = 90;
  16.   do
  17.   {
  18.    while (--j);
  19.   } while (--i);
  20. }
  21. }
  22. /******************************************************************************
  23. * 函数名称:void LCD1602_write_cmd(unsigned char cmd)           *
  24. * 函数功能:写命令函数                     *
  25. * 输入参数: cmd 命令                 *
  26. * 返回值  :无                         *
  27. * 其他说明:                         *
  28. ******************************************************************************/
  29. void LCD1602_write_cmd(unsigned char cmd) {
  30. LCD1602_DB=cmd;
  31. LCD_RES=0;
  32. LCD_EN=1;
  33. Delay_ms(10);
  34. LCD_EN=0;
  35. }
  36. /******************************************************************************
  37. * 函数名称:void LCD1602_write_data(unsigned char date)            *
  38. * 函数功能:写数据函数                     *
  39. * 输入参数: date 数据                   *
  40. * 返回值  :无                        *
  41. * 其他说明:                         *
  42. ******************************************************************************/

  43. void LCD1602_write_data(unsigned char date) {
  44. LCD1602_DB=date;
  45. LCD_RES=1;
  46. LCD_EN=1;
  47. Delay_ms(10);
  48. LCD_EN=0;
  49. }
  50. /******************************************************************************
  51. * 函数名称:void LCD1602_Init(void)                           *
  52. * 函数功能:1602初始化函数                   *
  53. * 输入参数: 无                     *
  54. * 返回值  :无                        *
  55. * 其他说明:                         *
  56. ******************************************************************************/

  57. void LCD1602_Init() {
  58. LCD1602_BKLSet(LCD1602_BKL_ON); //背光开启
  59. LCD1602_write_cmd(0x01);  //显示清屏
  60. LCD1602_write_cmd(0x38); //显示模式设置
  61. LCD1602_write_cmd(0x0C); //显示开及光标设置
  62. LCD1602_write_cmd(0x06); //显示光标移动位置
  63. }
  64. /******************************************************************************
  65. * 函数名称:void LCD1602_Clear(void)                         *
  66. * 函数功能:1602清屏函数                   *
  67. * 输入参数: 无                    *
  68. * 返回值  :无                        *
  69. * 其他说明:                         *
  70. ******************************************************************************/
  71. void LCD1602_Clear(){
  72. LCD1602_write_cmd(0x01);
  73. }

  74. /******************************************************************************
  75. * 函数名称:void LCD1602_BKLSet(unsigned char val)                  *
  76. * 函数功能:打开1602背光函数                   *
  77. * 输入参数: LCD1602_BKL_ON 开LCD1602_BKL_OFF 关       *   
  78. * 返回值  :无                        *
  79. * 其他说明:                         *
  80. ******************************************************************************/
  81. void LCD1602_BKLSet(unsigned char val){
  82. Lcd1602_light = val;
  83. }
  84. /******************************************************************************
  85. * 函数名称:unsigned char LCD1602_BKLGet()                        *
  86. * 函数功能:获取1602背光函数                   *
  87. * 输入参数:无                      *
  88. * 返回值  : 0 开1 关               *
  89. * 其他说明:                         *
  90. ******************************************************************************/
  91. unsigned char LCD1602_BKLGet(){
  92. return Lcd1602_light;
  93. }
  94. /******************************************************************************
  95. * 函数名称:void LCD1602_SetCursor(unsigned char x,unsigned char y)   *
  96. * 函数功能:设置1602位置函数                   *
  97. * 输入参数:x 横坐标 y 纵坐标                   *
  98. * 返回值  : 无                        *
  99. * 其他说明:                         *
  100. ******************************************************************************/
  101. void LCD1602_SetCursor(unsigned char x,unsigned char y){
  102. unsigned char addr;
  103. if(y==0)
  104.   addr=0x00+x;
  105. else
  106.   addr=0x40+x;
  107. LCD1602_write_cmd(addr | 0x80);
  108. }
  109. /******************************************************************************
  110. * 函数名称:void LCD1602_ShowNum(char x, char y, unsigned int num)    *
  111. * 函数功能:指定位置显示数字函数                   *
  112. * 输入参数:x 横坐标 y 纵坐标  num 数字             *
  113. * 返回值  : 无                        *
  114. * 其他说明:                     *
  115. ******************************************************************************/
  116. void LCD1602_ShowNum(char x, char y, unsigned int num){
  117. unsigned int i,j,k,l,n;
  118. i=num/10000;
  119. j=(num-10000*i)/1000;
  120. k=(num-10000*i-1000*j)/100;
  121. l=(num-10000*i-1000*j-100*k)/10;
  122. n=num%10;
  123. LCD1602_SetCursor(x,y);
  124. if(i!=0)LCD1602_write_data(i+0x30);
  125. if((i!=0)||(j!=0))LCD1602_write_data(j+0x30);
  126. if((i!=0)||(j!=0)||(k!=0))LCD1602_write_data(k+0x30);
  127. if((i!=0)||(j!=0)||(k!=0)||(l!=0))LCD1602_write_data(l+0x30);
  128. LCD1602_write_data(n+0x30);
  129. }
  130. /******************************************************************************
  131. * 函数名称:void LCD1602_ShowChar(unsigned char xpos,unsigned char ypos,char xsz)  *
  132. * 函数功能:指定位置显示字符函数                   *
  133. * 输入参数:xpos 横坐标 ypos 纵坐标  xsz 字符             *
  134. * 返回值  : 无                        *
  135. * 其他说明:                     *
  136. ******************************************************************************/
  137. void LCD1602_ShowChar(unsigned char xpos,unsigned char ypos,char xsz) {
  138. ypos%=2;
  139. if(ypos==0)
  140. {
  141.   LCD1602_write_cmd(0x80+xpos);
  142. }
  143. else
  144. {
  145.   LCD1602_write_cmd(0x80+0x40+xpos);
  146. }
  147. LCD1602_write_data(xsz);
  148. }
  149. /******************************************************************************
  150. * 函数名称:void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p) *
  151. * 函数功能:指定位置显示字符串函数                   *
  152. * 输入参数:xpos 横坐标 ypos 纵坐标  *p 字符串             *
  153. * 返回值  : 无                        *
  154. * 其他说明:                     *
  155. ******************************************************************************/
  156. void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p){
  157. if(ypos>1)return;
  158. while(*p!='\0')
  159. {
  160.   LCD1602_ShowChar(xpos++,ypos,*p++);
  161.   if(*p=='\n')
  162.   {
  163.    xpos=0;
  164.    ypos++;
  165.    p++;
  166.   }
  167. }
  168. }
  169. unsigned char code code_Y[8]={0x11,0x0A,0x04,0x04,0x04,0x04,0x04,0x00};//类似Y的字模
  170. /******************************************************************************
  171. * 函数名称:void LCD1602_wtire_CGRAM(void) *
  172. * 函数功能:自定义显示字符函数                   *
  173. * 输入参数:   无             *
  174. * 返回值  : 无                        *
  175. * 其他说明:                     *
  176. ******************************************************************************/
  177. void LCD1602_wtire_CGRAM(){
  178.     unsigned char num;
  179.     LCD1602_write_cmd(0x40);    //对CGRAM第一个自定义字符操作,若是第二个则为0x48,
  180.            //其次类推(上面有对顶的关系)
  181.     for(num=0;num<8;num++)
  182.     {
  183.        LCD1602_write_data(code_Y[num]);
  184.     }
  185.     LCD1602_write_cmd(0x80);    //规定显示在第一行第一个位置
  186.     LCD1602_write_data(0x00);   //显示第一个自定义字符 (0x40对应第一个:0x00)

  187. }
  188. /********************************************************************/
复制代码
 楼主| 狗啃模拟 发表于 2022-5-29 13:09 | 显示全部楼层
3、

  1. STM32:LCD1602.h
  2. #ifndef __LCD1602_H
  3. #define __LCD1602_H

  4. /***************************根据自己的硬件引脚做修改*****************************/
  5. #define LCD_RS_Set() GPIO_SetBits( GPIOB, GPIO_Pin_12 )//1602的数据/指令选择控制线
  6. #define LCD_RS_Clr() GPIO_ResetBits( GPIOB, GPIO_Pin_12 )

  7. #define LCD_RW_Set() GPIO_SetBits( GPIOB, GPIO_Pin_13 )//1602的读写控制线
  8. #define LCD_RW_Clr() GPIO_ResetBits( GPIOB, GPIO_Pin_13 )

  9. #define LCD_EN_Set() GPIO_SetBits( GPIOB, GPIO_Pin_14 )//1602的使能控制线
  10. #define LCD_EN_Clr() GPIO_ResetBits( GPIOB, GPIO_Pin_14 )

  11. #define DATAOUT( x ) GPIO_Write( GPIOA, x ) //1602的8条数据控制线

  12. void GPIO_Configuration();

  13. void LCD1602_Init();

  14. void LCD1602_Wait_Ready();

  15. void LCD1602_Write_Cmd( u8 cmd );

  16. void LCD1602_Write_Dat( u8 data );

  17. void LCD1602_ClearScreen();

  18. void LCD1602_Set_Cursor( u8 x, u8 y );

  19. void LCD1602_Show_Str( u8 x, u8 y, u8 *str );

  20. #endif
复制代码
 楼主| 狗啃模拟 发表于 2022-5-29 13:11 | 显示全部楼层
4、STM32:LCD1602.c
#include  "LCD1602.h"
/******************************************************************************
* 函数名称:void GPIO_Configuration()            *
* 函数功能:LCD1602引脚初始化                    *
* 输入参数:无                  *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
/*******************根据自己的硬件引脚做修改*****************************************/
void GPIO_Configuration(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//选择工作频率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//设置工作模式
GPIO_Init( GPIOB, &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |            GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//设置工作模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//选择工作频率
GPIO_Init( GPIOA, &GPIO_InitStructure );
}
/******************************************************************************
* 函数名称:void LCD1602_Init()            *
* 函数功能:LCD1602初始化                    *
* 输入参数:无                  *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
void LCD1602_Init(){
GPIO_Configuration();   //初始化引脚

LCD1602_Write_Cmd( 0x38 );      //显示模式设置
delay_ms( 5 );
LCD1602_Write_Cmd( 0x0c );      //显示开及光标设置
delay_ms( 5 );
LCD1602_Write_Cmd( 0x06 );      //显示光标移动位置
delay_ms( 5 );
LCD1602_Write_Cmd( 0x01 );      //显示清屏
delay_ms( 5 );
}
/******************************************************************************
* 函数名称:void LCD1602_Write_Cmd(u8 cmd)            *
* 函数功能:写命令函数                     *
* 输入参数: cmd 命令                   *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
void LCD1602_Write_Cmd( u8 cmd ){
LCD_RS_Clr();
LCD_RW_Clr();
LCD_EN_Set();

GPIO_Write( GPIOA, (GPIO_ReadOutputData( GPIOA ) & 0xff00) | cmd );//对电平的读取

DATAOUT( cmd );
delay_ms( 5 );
LCD_EN_Clr();
}

/******************************************************************************
* 函数名称:void LCD1602_Write_Dat(u8 date)            *
* 函数功能:写数据函数                     *
* 输入参数: date 数据                   *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
void LCD1602_Write_Dat( u8 data ){
LCD_RS_Set();
LCD_RW_Clr();
LCD_EN_Set();

GPIO_Write( GPIOA, (GPIO_ReadOutputData( GPIOA ) & 0xff00) | data );//对电平的读取

delay_ms( 5 );
LCD_EN_Clr();
}

/******************************************************************************
* 函数名称:void LCD1602_ClearScreen()            *
* 函数功能:1602清屏函数                     *
* 输入参数:无                   *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
void LCD1602_ClearScreen(){
LCD1602_Write_Cmd( 0x01 );
}

/******************************************************************************
* 函数名称:void LCD1602_Set_Cursor(u8 x, u8 y)            *
* 函数功能:设置1602位置函数                   *
* 输入参数:x 横坐标 y 纵坐标                   *
* 返回值  :无                        *
* 其他说明:                         *
******************************************************************************/
void LCD1602_Set_Cursor( u8 x, u8 y ){
u8 addr;

if ( y == 0 )
  addr = 0x00 + x;
else
  addr = 0x40 + x;
LCD1602_Write_Cmd( addr | 0x80 );
}

/******************************************************************************
* 函数名称:void LCD1602_Show_Str( u8 x, u8 y, u8 *str )            *
* 函数功能:指定位置显示字符串函数                   *
* 输入参数:x 横坐标 y 纵坐标  *str 字符串             *
* 返回值  : 无                       *
* 其他说明:                         *
******************************************************************************/
void LCD1602_Show_Str( u8 x, u8 y, u8 *str ){
LCD1602_Set_Cursor( x, y );
while ( *str != '\0' )
{
  LCD1602_Write_Dat( *str++ );
}
}

 楼主| 狗啃模拟 发表于 2022-5-29 13:13 | 显示全部楼层
四、LCD1602 STM32驱动(6线)
LCD1602 的接口形式是并行的,它有 8 条数据线、3 条控制线。这样就需要 11 条线来控制它的正常工作。虽然它还可以工作在 4 位数据线的形式,最精简的形式是 6 条线。

我们可以用74HC595 进行串-并转换,74HC595 是“串入并出”的移位寄存器芯片,它需要用 3 条线控制数据的输入,才能正常的输出 8 位数据,总共6条线即可进行LCD1602的控制。

这里多用了一块芯片但是省了5个IO口,对于某些项目单片机引脚不够用的情况下可以应用上。
 楼主| 狗啃模拟 发表于 2022-5-29 13:14 | 显示全部楼层
原理图如下:
5320662930121e3e47.png
 楼主| 狗啃模拟 发表于 2022-5-29 13:15 | 显示全部楼层
 楼主| 狗啃模拟 发表于 2022-5-29 13:16 | 显示全部楼层
1、STM32:LCD1602.h
#ifndef __LCD1602_H
#define __LCD1602_H  
  
//1602液晶指令/数据或选择引脚
#define LCD_RS_PORT    GPIOB
#define LCD_RS_PIN   GPIO_Pin_10

#define  LCD_RS_0    GPIO_ResetBits(LCD_RS_PORT, LCD_RS_PIN)
#define  LCD_RS_1    GPIO_SetBits(LCD_RS_PORT, LCD_RS_PIN)


//1602液晶使能引脚
#define LCD_EN_PORT    GPIOB
#define LCD_EN_PIN   GPIO_Pin_11     //PB11

#define  LCD_EN_0    GPIO_ResetBits(LCD_EN_PORT, LCD_EN_PIN)
#define  LCD_EN_1    GPIO_SetBits(LCD_EN_PORT, LCD_EN_PIN)

//1602液晶数据端口
#define MOSIO PBout(0)
#define R_CLK PCout(5)
#define S_CLK PCout(4)

/*********************函数声明*****************/
void LCD1602_Init(void);
void LCD1602_Clear(void);
void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p);
void LCD1602_BKLSet(unsigned char on);
unsigned char LCD1602_BKLGet(void);
void LCD1602_ShowLineStr(unsigned char xpos,unsigned char ypos,char *p);
void LCD1602_ShowState(unsigned char Signal, unsigned char GprsState);
/**********************************************/
#endif  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

67

主题

877

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部