顯示效果圖(可顯示第一、第二行、光標顯示):
文內放不下,見圖示
首先是IO口,使用STM32CUBEMX配置
1.png (94.57 KB, 下載次數: 53)
下載附件
2023-5-6 14:49 上傳
下載.png (2.06 MB, 下載次數: 39)
下載附件
2023-5-6 13:05 上傳
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);
}
}
后面調用自己操作,有問題自己調、在評論說,不支持給源文件,各位同仁一起學習、一起進步。
|