久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2592|回復: 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.zg4o1577.cn/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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品99精品久久免费 | 日韩精品欧美精品 | 日韩中文字幕一区二区 | 亚洲精品自在在线观看 | 一区二区视频 | 狠狠撸在线视频 | 97成人精品| 久在线| 国产99久久精品一区二区永久免费 | 亚洲视频在线看 | 国产99视频精品免费播放照片 | 皇色视频在线 | 欧美日韩在线观看一区 | 精品1区2区3区 | 中文字幕二区三区 | 国产精品久久午夜夜伦鲁鲁 | 狠狠狠 | 久草在线 | 久久精品一区二区三区四区 | 久久精品亚洲一区二区三区浴池 | 日韩中文字幕在线观看视频 | 午夜欧美a级理论片915影院 | 国产在线观看不卡一区二区三区 | 欧美片网站免费 | 久久久91精品国产一区二区三区 | 亚洲国产成人精品久久久国产成人一区 | 欧美精品成人一区二区三区四区 | 欧美日韩在线高清 | 久久网国产| 欧美另类视频在线 | 亚洲精品视频网站在线观看 | 精品一区二区三区在线视频 | 成人av片在线观看 | h视频在线免费 | 国产精品中文字幕在线 | 免费视频一区二区 | 国产精品一区二区不卡 | 久久免费精品视频 | 欧洲精品视频一区 | 国产精品视频免费观看 | 成人在线视频一区 |