STM32fo51k8驱动1.44,lcd 屏和ESP8266在屏幕上面显示实时时间,用STM32CubeMX生成详细代码,写出所以代码
时间: 2024-03-08 10:51:34 浏览: 68
基于STM32、ESP8266和DS1302的实时时间天气串口屏显示(桌面屏幕+倒计时报警)
5星 · 资源好评率100%
好的,以下是基于STM32CubeMX生成的详细代码,实现将ESP8266和1.44寸LCD屏幕连接到STM32F051K8上,同时显示实时时间的功能:
首先,需要配置STM32F051K8的GPIO口和时钟:
```c
// 定义引脚号
#define LCD_CE_Pin GPIO_PIN_4
#define LCD_CE_GPIO_Port GPIOA
#define LCD_RST_Pin GPIO_PIN_5
#define LCD_RST_GPIO_Port GPIOA
#define LCD_DC_Pin GPIO_PIN_6
#define LCD_DC_GPIO_Port GPIOA
#define LCD_DIN_Pin GPIO_PIN_7
#define LCD_DIN_GPIO_Port GPIOA
#define LCD_CLK_Pin GPIO_PIN_0
#define LCD_CLK_GPIO_Port GPIOB
#define ESP8266_TX_Pin GPIO_PIN_6
#define ESP8266_TX_GPIO_Port GPIOB
#define ESP8266_RX_Pin GPIO_PIN_7
#define ESP8266_RX_GPIO_Port GPIOB
// 使能GPIO口时钟
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// 配置LCD屏幕引脚为输出
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LCD_CE_Pin|LCD_RST_Pin|LCD_DC_Pin|LCD_DIN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LCD_CE_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LCD_CLK_Pin;
HAL_GPIO_Init(LCD_CLK_GPIO_Port, &GPIO_InitStruct);
// 配置ESP8266引脚为复用推挽输出
GPIO_InitStruct.Pin = ESP8266_TX_Pin|ESP8266_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF0_USART1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// 使能USART1时钟
__HAL_RCC_USART1_CLK_ENABLE();
```
然后,需要配置STM32F051K8的USART1串口和时钟:
```c
// 定义串口号
#define ESP8266_UART huart1
// 使能USART1时钟
__HAL_RCC_USART1_CLK_ENABLE();
// 配置USART1
ESP8266_UART.Instance = USART1;
ESP8266_UART.Init.BaudRate = 115200;
ESP8266_UART.Init.WordLength = UART_WORDLENGTH_8B;
ESP8266_UART.Init.StopBits = UART_STOPBITS_1;
ESP8266_UART.Init.Parity = UART_PARITY_NONE;
ESP8266_UART.Init.Mode = UART_MODE_TX_RX;
ESP8266_UART.Init.HwFlowCtl = UART_HWCONTROL_NONE;
ESP8266_UART.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&ESP8266_UART);
```
接下来,需要编写一些控制LCD屏幕的函数:
```c
void lcd_cmd(uint8_t cmd) {
LCD_CE_Low();
LCD_DC_Low();
for (int i = 0; i < 8; i++) {
if (cmd & 0x80) {
LCD_DIN_High();
} else {
LCD_DIN_Low();
}
cmd <<= 1;
LCD_CLK_Low();
LCD_CLK_High();
}
LCD_CE_High();
}
void lcd_data(uint8_t data) {
LCD_CE_Low();
LCD_DC_High();
for (int i = 0; i < 8; i++) {
if (data & 0x80) {
LCD_DIN_High();
} else {
LCD_DIN_Low();
}
data <<= 1;
LCD_CLK_Low();
LCD_CLK_High();
}
LCD_CE_High();
}
void lcd_init() {
LCD_RST_High();
HAL_Delay(10);
LCD_RST_Low();
HAL_Delay(10);
LCD_RST_High();
HAL_Delay(10);
lcd_cmd(0x21);
lcd_cmd(0xB1);
lcd_cmd(0x04);
lcd_cmd(0x14);
lcd_cmd(0x0C);
lcd_cmd(0x20);
lcd_cmd(0x0C);
}
void lcd_set_cursor(uint8_t x, uint8_t y) {
lcd_cmd(0x80 | x);
lcd_cmd(0x40 | y);
}
void lcd_clear() {
for (int i = 0; i < 84; i++) {
for (int j = 0; j < 6; j++) {
lcd_set_cursor(i, j);
lcd_data(0x00);
}
}
}
void lcd_write_char(char c) {
for (int i = 0; i < 5; i++) {
lcd_data(font[c - 0x20][i]);
}
lcd_data(0x00);
}
```
其中,`lcd_cmd`和`lcd_data`函数用于向LCD屏幕发送指令和数据,`lcd_init`函数用于初始化LCD屏幕,`lcd_set_cursor`函数用于设置光标位置,`lcd_clear`函数用于清空屏幕,`lcd_write_char`函数用于显示字符。
最后,需要编写一个函数来获取当前时间并在LCD屏幕上显示:
```c
void display_time() {
RTC_TimeTypeDef time;
HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
char hour[3];
char minute[3];
char second[3];
sprintf(hour, "%02d", time.Hours);
sprintf(minute, "%02d", time.Minutes);
sprintf(second, "%02d", time.Seconds);
lcd_set_cursor(0, 0);
lcd_write_char(hour[0]);
lcd_write_char(hour[1]);
lcd_write_char(':');
lcd_write_char(minute[0]);
lcd_write_char(minute[1]);
lcd_write_char(':');
lcd_write_char(second[0]);
lcd_write_char(second[1]);
}
```
在这个函数中,我们使用了STM32F051K8的RTC模块来获取当前时间,并将它显示在LCD屏幕上。
完整代码如下:
阅读全文