找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2900|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

STM32單片機(jī)HAL庫(kù)+STM32cubeMX+LCD1602 驅(qū)動(dòng)顯示程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:326261 發(fā)表于 2023-5-6 13:10 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
顯示效果圖(可顯示第一、第二行、光標(biāo)顯示):
文內(nèi)放不下,見(jiàn)圖示

首先是IO口,使用STM32CUBEMX配置





LCD1602顯示效果圖

然后STM32單片機(jī)驅(qū)動(dòng)程序:
.h文件配置資源使用
/* LCD驅(qū)動(dòng)引腳 */
#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數(shù)據(jù)引腳

/* LCD數(shù)據(jù)引腳 */


/* 供外部調(diào)用的函數(shù)聲明 */
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);          //顯示光標(biāo)位置
void LCD1602_Clear(void);                                 //清屏顯示

void LCD1602_WriteCmd(uint8_t uccmd);         //寫指令
void LCD1602_WriteData(uint8_t ucdata);       //寫數(shù)據(jù)


.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);

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_ShowString
*        功能說(shuō)明: 1602顯示字符串
*        形    參: ucRow:縱向行數(shù)(0第一行,1第二行)
*           ucColumn:橫向位置(對(duì)應(yīng)第幾個(gè)字符,最大16)
*           str:對(duì)應(yīng)顯示字符串
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
void LCD1602_ShowString(uint8_t ucRow, uint8_t ucColumn, char *str)
{
  uint8_t i = 0;
  /*  設(shè)置起始位置  */
  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);
  }
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_Cur
*        功能說(shuō)明: 1602顯示光標(biāo)位置
*        形    參: ucRow:縱向行數(shù)(0第一行,1第二行)
*           ucColumn:橫向位置(對(duì)應(yīng)第幾個(gè)字符,最大16)
*           str:對(duì)應(yīng)顯示字符串
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
void LCD1602_Cur(uint8_t ucRow, uint8_t ucColumn)
{
  /*  設(shè)置起始位置  */
  if(ucRow == 0)
    LCD1602_WriteCmd(0x80 | ucColumn);
  else if(ucRow == 1)
    LCD1602_WriteCmd(0xC0 | ucColumn);

  LCD1602_WriteCmd(0x0D);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_Clear
*        功能說(shuō)明: 1602顯示清除屏幕所有顯示
*        形    參: 無(wú)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
void LCD1602_Clear(void)
{
  LCD1602_WriteCmd(0x01);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_Init
*        功能說(shuō)明: LCD1602初始化
*        形    參: 無(wú)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_Busywait
*        功能說(shuō)明: LCD1602忙等待
*        形    參: 無(wú)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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);            //等待直到允許操作
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_WriteCmd
*        功能說(shuō)明: LCD1602寫指令
*        形    參: uccmd:指令
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_WriteData
*        功能說(shuō)明: LCD1602寫數(shù)據(jù)
*        形    參: ucdata:數(shù)據(jù)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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();
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_dataIOmode
*        功能說(shuō)明: 數(shù)據(jù)IO口模式方向
*        形    參: cmode:I:輸入、O:輸出
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_ReadData
*        功能說(shuō)明: LCD1602讀取當(dāng)前狀態(tài)
*        形    參: 無(wú)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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;
}

/*
*********************************************************************************************************
*        函 數(shù) 名: LCD1602_WriteCmd
*        功能說(shuō)明: LCD1602寫指令
*        形    參: 無(wú)
*        返 回 值: 無(wú)
*********************************************************************************************************
*/
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);
  }
}

后面調(diào)用自己操作,有問(wèn)題自己調(diào)、在評(píng)論說(shuō),不支持給源文件,各位同仁一起學(xué)習(xí)、一起進(jìn)步。

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:326261 發(fā)表于 2023-5-6 13:16 | 只看該作者
關(guān)于LCD1602的知識(shí)點(diǎn)學(xué)習(xí)參考
LCD1602中文資料http://www.torrancerestoration.com/bbs/dpj-38656-1.html
回復(fù)

使用道具 舉報(bào)

板凳
ID:326261 發(fā)表于 2023-5-6 13:23 | 只看該作者
本帖最后由 工學(xué)院陳偉霆 于 2023-5-8 08:37 編輯

后續(xù)還會(huì)有UI設(shè)計(jì)和按鍵搭配
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表