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

標(biāo)題: STM32單片機(jī)HAL庫+STM32cubeMX+LCD1602 驅(qū)動顯示程序 [打印本頁]

作者: 工學(xué)院陳偉霆    時間: 2023-5-6 13:10
標(biāo)題: STM32單片機(jī)HAL庫+STM32cubeMX+LCD1602 驅(qū)動顯示程序
顯示效果圖(可顯示第一、第二行、光標(biāo)顯示):
文內(nèi)放不下,見圖示

首先是IO口,使用STM32CUBEMX配置





LCD1602顯示效果圖

然后STM32單片機(jī)驅(qū)動程序:
.h文件配置資源使用
/* LCD驅(qū)動引腳 */
#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
*        功能說明: 1602顯示字符串
*        形    參: ucRow:縱向行數(shù)(0第一行,1第二行)
*           ucColumn:橫向位置(對應(yīng)第幾個字符,最大16)
*           str:對應(yīng)顯示字符串
*        返 回 值: 無
*********************************************************************************************************
*/
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
*        功能說明: 1602顯示光標(biāo)位置
*        形    參: ucRow:縱向行數(shù)(0第一行,1第二行)
*           ucColumn:橫向位置(對應(yīng)第幾個字符,最大16)
*           str:對應(yīng)顯示字符串
*        返 回 值: 無
*********************************************************************************************************
*/
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
*        功能說明: 1602顯示清除屏幕所有顯示
*        形    參: 無
*        返 回 值: 無
*********************************************************************************************************
*/
void LCD1602_Clear(void)
{
  LCD1602_WriteCmd(0x01);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: 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);
}

/*
*********************************************************************************************************
*        函 數(shù) 名: 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);            //等待直到允許操作
}

/*
*********************************************************************************************************
*        函 數(shù) 名: 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);
}

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

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

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

作者: 工學(xué)院陳偉霆    時間: 2023-5-6 13:16
關(guān)于LCD1602的知識點(diǎn)學(xué)習(xí)參考
LCD1602中文資料http://www.zg4o1577.cn/bbs/dpj-38656-1.html

作者: 工學(xué)院陳偉霆    時間: 2023-5-6 13:23
本帖最后由 工學(xué)院陳偉霆 于 2023-5-8 08:37 編輯

后續(xù)還會有UI設(shè)計和按鍵搭配




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 国产精品美女久久久久久免费 | 7777在线| 亚洲日本中文 | 国产成人免费 | 欧美一级免费看 | 日韩精品在线一区 | 亚洲字幕在线观看 | 日韩精品在线观看一区二区 | 911精品美国片911久久久 | 在线国产小视频 | 欧美一a | 日本涩涩网 | 天天精品综合 | 日本一区二区在线视频 | 中文字幕久久久 | 亚洲人在线 | 亚洲a在线视频 | 国产欧美日韩视频 | 国产91av视频 | 亚洲精品国产成人 | 91亚洲欧美 | 日韩在线成人 | 成人欧美一区二区三区黑人孕妇 | 日韩精品一区二区三区四区视频 | 中文字幕亚洲一区 | 亚洲欧美日本在线 | 日本久久一区二区三区 | 日韩成人在线网站 | 国产精品欧美日韩 | 亚洲欧洲一区 | 亚洲日本成人 | 日本成人中文字幕在线观看 | 一区二区在线免费观看 | 高清一区二区 | 欧美黑人巨大videos精品 | 成人亚洲 | 五月婷婷激情 | 欧美在线资源 | 国产视频线观看永久免费 | 毛片一区二区三区 | 欧美日韩大片 |