找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2852|回复: 2
收起左侧

STM32单片机HAL库+STM32cubeMX+LCD1602 驱动显示程序

[复制链接]
ID:326261 发表于 2023-5-6 13:10 | 显示全部楼层 |阅读模式
显示效果图(可显示第一、第二行、光标显示):
文内放不下,见图示

首先是IO口,使用STM32CUBEMX配置
1.png


下载.png

LCD1602显示效果图

然后STM32单片机驱动程序:
.h文件配置资源使用
/* LCD驱动引脚 */
#define LCD_CS(a)                        HAL_GPIO_WritePin(GPIOA, LCD_EN_Pin, a)      //EN 脉冲使能引脚
#define LCD_RW(a)                  HAL_GPIO_WritePin(GPIOB, LCD_RW_Pin, a)      //RW 读写引脚
#define LCD_RS(a)                        HAL_GPIO_WritePin(GPIOB, LCD_RS_Pin, a)      //RS 命令or数据引脚

/* LCD数据引脚 */


/* 供外部调用的函数声明 */
void LCD1602_Init(void);                //LCD初始化
void LCD1602_ShowString(uint8_t ucRow, uint8_t ucColumn, char *str);      //显示字符串
void LCD1602_Cur(uint8_t ucRow, uint8_t ucColumn);          //显示光标位置
void LCD1602_Clear(void);                                 //清屏显示

void LCD1602_WriteCmd(uint8_t uccmd);         //写指令
void LCD1602_WriteData(uint8_t ucdata);       //写数据


.c文件
static void LCD1602_dataIOmode(char cmode);
static void LCD1602_WriteIOData(uint8_t ucdat);
static uint8_t LCD1602_ReadData(void);
static void LCD1602_Busywait(void);

/*
*********************************************************************************************************
*        函 数 名: LCD1602_ShowString
*        功能说明: 1602显示字符串
*        形    参: ucRow:纵向行数(0第一行,1第二行)
*           ucColumn:横向位置(对应第几个字符,最大16)
*           str:对应显示字符串
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_ShowString(uint8_t ucRow, uint8_t ucColumn, char *str)
{
  uint8_t i = 0;
  /*  设置起始位置  */
  if(ucRow == 0)
    LCD1602_WriteCmd(0x80 | ucColumn);
  else if(ucRow == 1)
    LCD1602_WriteCmd(0xC0 | ucColumn);

  /*  输出字符串  */
  for(i = 0; ((i < 16) && (str[ i] != '\0')); i++)
  {
    LCD1602_WriteData(str[ i]);
    HAL_Delay(2);
  }
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_Cur
*        功能说明: 1602显示光标位置
*        形    参: ucRow:纵向行数(0第一行,1第二行)
*           ucColumn:横向位置(对应第几个字符,最大16)
*           str:对应显示字符串
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_Cur(uint8_t ucRow, uint8_t ucColumn)
{
  /*  设置起始位置  */
  if(ucRow == 0)
    LCD1602_WriteCmd(0x80 | ucColumn);
  else if(ucRow == 1)
    LCD1602_WriteCmd(0xC0 | ucColumn);

  LCD1602_WriteCmd(0x0D);
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_Clear
*        功能说明: 1602显示清除屏幕所有显示
*        形    参: 无
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_Clear(void)
{
  LCD1602_WriteCmd(0x01);
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_Init
*        功能说明: LCD1602初始化
*        形    参: 无
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_Init(void)
{
  HAL_Delay(15);
  LCD1602_WriteCmd(0x38);
  HAL_Delay(5);
  LCD1602_WriteCmd(0x38);
  HAL_Delay(5);
  LCD1602_WriteCmd(0x38);
  HAL_Delay(2);
  LCD1602_WriteCmd(0x01);
  HAL_Delay(2);
  LCD1602_WriteCmd(0x06);
  HAL_Delay(2);
  LCD1602_WriteCmd(0x0C);
  HAL_Delay(2);
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_Busywait
*        功能说明: LCD1602忙等待
*        形    参: 无
*        返 回 值: 无
*********************************************************************************************************
*/
static void LCD1602_Busywait(void)
{
        uint8_t status = 0;
        LCD1602_dataIOmode('I');
  LCD_RS(GPIO_PIN_RESET);
  LCD_RW(GPIO_PIN_SET);

        do
        {
    LCD_CS(GPIO_PIN_SET);
                status = LCD1602_ReadData();
    LCD_CS(GPIO_PIN_RESET);
        }while(status & 0X80);            //等待直到允许操作
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_WriteCmd
*        功能说明: LCD1602写指令
*        形    参: uccmd:指令
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_WriteCmd(uint8_t uccmd)
{
  LCD1602_Busywait();
        LCD1602_dataIOmode('O');

  LCD_RS(GPIO_PIN_RESET);
  LCD_RW(GPIO_PIN_RESET);

  LCD1602_WriteIOData(uccmd);

  LCD_CS(GPIO_PIN_SET);
  HAL_Delay(2);
  LCD_CS(GPIO_PIN_RESET);
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_WriteData
*        功能说明: LCD1602写数据
*        形    参: ucdata:数据
*        返 回 值: 无
*********************************************************************************************************
*/
void LCD1602_WriteData(uint8_t ucdata)
{
//  LCD1602_Busywait();
        LCD1602_dataIOmode('O');
  LCD1602_WriteIOData(ucdata);

  LCD_RS(GPIO_PIN_SET);
  LCD_RW(GPIO_PIN_RESET);

  LCD_CS(GPIO_PIN_SET);
  HAL_Delay(1);
  LCD_CS(GPIO_PIN_RESET);

  LCD1602_Busywait();
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_dataIOmode
*        功能说明: 数据IO口模式方向
*        形    参: cmode:I:输入、O:输出
*        返 回 值: 无
*********************************************************************************************************
*/
static void LCD1602_dataIOmode(char cmode)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, LCD_EN_Pin|LCD_DB7_Pin|LCD_DB6_Pin|LCD_DB5_Pin
                          |LCD_DB4_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, LCD_DB3_Pin|LCD_DB1_Pin|LCD_DB0_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LCD_DB2_GPIO_Port, LCD_DB2_Pin, GPIO_PIN_SET);


  /*Configure GPIO pins : PAPin PAPin PAPin
                           PAPin */
  GPIO_InitStruct.Pin = LCD_DB7_Pin|LCD_DB6_Pin|LCD_DB5_Pin
                          |LCD_DB4_Pin;

  if(cmode == 'I')
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  else if(cmode == 'O')
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PBPin PBPin PBPin */
  GPIO_InitStruct.Pin = LCD_DB3_Pin|LCD_DB1_Pin|LCD_DB0_Pin;

  if(cmode == 'I')
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  else if(cmode == 'O')
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = LCD_DB2_Pin;

  if(cmode == 'I')
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  else if(cmode == 'O')
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LCD_DB2_GPIO_Port, &GPIO_InitStruct);
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_ReadData
*        功能说明: LCD1602读取当前状态
*        形    参: 无
*        返 回 值: 无
*********************************************************************************************************
*/
static uint8_t LCD1602_ReadData(void)
{
  uint8_t ucdat = 0;
  if(HAL_GPIO_ReadPin(GPIOB, LCD_DB0_Pin) == GPIO_PIN_SET)
    ucdat |= 0X01;
  if(HAL_GPIO_ReadPin(GPIOB, LCD_DB1_Pin) == GPIO_PIN_SET)
    ucdat |= 0X02;
  if(HAL_GPIO_ReadPin(LCD_DB2_GPIO_Port, LCD_DB2_Pin) == GPIO_PIN_SET)
    ucdat |= 0X04;
  if(HAL_GPIO_ReadPin(GPIOB, LCD_DB3_Pin) == GPIO_PIN_SET)
    ucdat |= 0X08;
  if(HAL_GPIO_ReadPin(GPIOA, LCD_DB4_Pin) == GPIO_PIN_SET)
    ucdat |= 0X10;
  if(HAL_GPIO_ReadPin(GPIOA, LCD_DB5_Pin) == GPIO_PIN_SET)
    ucdat |= 0X20;
  if(HAL_GPIO_ReadPin(GPIOA, LCD_DB6_Pin) == GPIO_PIN_SET)
    ucdat |= 0X40;
  if(HAL_GPIO_ReadPin(GPIOA, LCD_DB7_Pin) == GPIO_PIN_SET)
    ucdat |= 0X80;

  return ucdat;
}

/*
*********************************************************************************************************
*        函 数 名: LCD1602_WriteCmd
*        功能说明: LCD1602写指令
*        形    参: 无
*        返 回 值: 无
*********************************************************************************************************
*/
static void LCD1602_WriteIOData(uint8_t ucdat)
{
  if(ucdat & 0x01)
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB0_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB0_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x02)
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB1_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB1_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x04)
  {
    HAL_GPIO_WritePin(LCD_DB2_GPIO_Port, LCD_DB2_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(LCD_DB2_GPIO_Port, LCD_DB2_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x08)
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB3_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOB, LCD_DB3_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x10)
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB4_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB4_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x20)
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB5_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB5_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x40)
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB6_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB6_Pin, GPIO_PIN_RESET);
  }
  if(ucdat & 0x80)
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB7_Pin, GPIO_PIN_SET);
  }
  else
  {
    HAL_GPIO_WritePin(GPIOA, LCD_DB7_Pin, GPIO_PIN_RESET);
  }
}

后面调用自己操作,有问题自己调、在评论说,不支持给源文件,各位同仁一起学习、一起进步。

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:326261 发表于 2023-5-6 13:16 | 显示全部楼层
关于LCD1602的知识点学习参考
LCD1602中文资料http://www.51hei.com/bbs/dpj-38656-1.html
回复

使用道具 举报

ID:326261 发表于 2023-5-6 13:23 | 显示全部楼层
本帖最后由 工学院陈伟霆 于 2023-5-8 08:37 编辑

后续还会有UI设计和按键搭配
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表