cubemx环境
芯片:STM32F103RET6
GPIO配置
1602共使用11个引脚:
RW:PA11
RS:PC6
EN:PB4
DB0-4:PB5-9
DB5-7:PC0-2
GPIO配置:输出推挽,端口速率LOW,开启AFIO,关闭JTAG
/*main.c*/
static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = { 0 };
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_AFIO_REMAP_SWJ_NOJTAG();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC,LCD_DB5_Pin | LCD_DB6_Pin | LCD_DB7_Pin | LCD_RS_Pin,GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LCD_RW_GPIO_Port, LCD_RW_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB,LCD_EN_Pin | LCD_DB0_Pin | LCD_DB1_Pin | LCD_DB2_Pin | LCD_DB3_Pin| LCD_DB4_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : LCD_DB5_Pin LCD_DB6_Pin LCD_DB7_Pin LCD_RS_Pin */
GPIO_InitStruct.Pin = LCD_DB5_Pin | LCD_DB6_Pin | LCD_DB7_Pin | LCD_RS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : LCD_RW_Pin PA12 */
GPIO_InitStruct.Pin = LCD_RW_Pin | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : LCD_EN_Pin LCD_DB0_Pin LCD_DB1_Pin LCD_DB2_Pin
LCD_DB3_Pin LCD_DB4_Pin */
GPIO_InitStruct.Pin = LCD_EN_Pin | LCD_DB0_Pin | LCD_DB1_Pin | LCD_DB2_Pin | LCD_DB3_Pin | LCD_DB4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/*
lcd1602.c
*/
/***** GPIOB3 H:data;L:command ******/
#define LCD1602_RS_DATA HAL_GPIO_WritePin(GPIOC,LCD_RS_Pin,GPIO_PIN_SET)
#define LCD1602_RS_CMD HAL_GPIO_WritePin(GPIOC,LCD_RS_Pin,GPIO_PIN_RESET)
/***** GPIOB4 H:read;L:write ******/
#define LCD1602_RW_READ HAL_GPIO_WritePin(GPIOA,LCD_RW_Pin,GPIO_PIN_SET)
#define LCD1602_RW_WRITE HAL_GPIO_WritePin(GPIOA,LCD_RW_Pin,GPIO_PIN_RESET)
/***** GPIOB5 ******/
#define LCD1602_E_HIGH HAL_GPIO_WritePin(GPIOB,LCD_EN_Pin,GPIO_PIN_SET)
#define LCD1602_E_LOW HAL_GPIO_WritePin(GPIOB,LCD_EN_Pin,GPIO_PIN_RESET)
LCD1602相关
/*1602.c*/
void LCD1602_DATA(unsigned char byte)
{
unsigned short value = 0;
//GPIOB->ODR=(GPIOB->ODR & 0x00ff) | (x<<8)
value = GPIOB->ODR;
value &= ~(0x001F << 5);
value |= (byte & 0x001F) << 5;
GPIOB->ODR = value;
LCD_Delay_us(10);
value = GPIOC->ODR;
value &= ~(0x0007 << 0);
value |= (byte & 0x00E0) >> 5;
GPIOC->ODR = value;
LCD_Delay_us(10);
//LCD初始化
void LCD_Init(void)
{
#if 0
/* Set cursor pointer to beginning for LCD */
LCD_Opts.currentX = 0;
LCD_Opts.currentY = 0;
LCD_Opts.DisplayFunction = LCD_8BITMODE | LCD_5x8DOTS | LCD_1LINE;
if (_LCD_ROWS > 1)
LCD_Opts.DisplayFunction |= LCD_2LINE;
/* Set # lines, font size,8BITMODE,etc. */
LCD_Cmd(LCD_FUNCTIONSET | LCD_Opts.DisplayFunction);
/* Turn the display on with no cursor or blinking default */
LCD_Opts.DisplayControl = LCD_DISPLAYON;
LCD_DisplayOn();
/* Default font directions 文字不动,地址自动+1*/
LCD_Opts.DisplayMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
LCD_Cmd(LCD_ENTRYMODESET | LCD_Opts.DisplayMode);
LCD_Clear();
LCD_Delay_ms(5);
#endif
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_AFIO_REMAP_SWJ_NOJTAG();
LCD_Delay_ms(15);
LCD_Cmd(0x38);
LCD_Delay_ms(5);
LCD_Cmd(0x38);
LCD_Delay_ms(5);
LCD_Cmd(0x38);
LCD_Cmd(0x38);
LCD_Delay_ms(10);
LCD_Cmd(0x08);
LCD_Delay_ms(10);
LCD_Cmd(0x01);
LCD_Delay_ms(10);
LCD_Cmd(0x06);
LCD_Delay_ms(10);
LCD_Cmd(0x0c);
LCD_Delay_ms(10);
LCD1602_E_LOW;
}
}
/*main.c*/
LCD_Init(); //初始化LCD1602
LCD_Puts(0, 0, "1234567890123456");
LCD_Puts(0, 1, "abcdefghijklmnop");
HAL_Delay(1000);