STM32F051K8U6控制esp8266控制led灯的亮灭,urat2,并实时返回灯的状态,STM32CubeMX代码
时间: 2024-03-03 12:53:10 浏览: 96
好的,下面是使用STM32F051K8U6控制esp8266控制led灯的亮灭,通过UART2实时返回灯的状态的STM32CubeMX代码:
1. 首先在STM32CubeMX中配置GPIO和UART2:
- GPIO:
PB0:作为ESP8266的RX引脚,设置为复用功能,选择USART2_RX
PB1:作为ESP8266的TX引脚,设置为复用功能,选择USART2_TX
PB2:作为LED灯的控制引脚,设置为输出
- UART2:
波特率:115200
数据位:8
停止位:1
校验位:无
2. 生成代码,打开生成的工程,在main.c文件中添加以下代码:
```c
#include "main.h"
#include "string.h"
#include "stdio.h"
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
char receive_data[50];
char send_data[50];
uint8_t LED_Status = 0;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
// 判断是否有数据接收
if (HAL_UART_Receive(&huart2, (uint8_t*)receive_data, 50, 1000) == HAL_OK)
{
// 判断是否收到"ON"命令
if (strstr(receive_data, "ON") != NULL)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET); // 点亮LED
LED_Status = 1; // 记录LED状态
sprintf(send_data, "LED is ON.\r\n");
HAL_UART_Transmit(&huart2, (uint8_t*)send_data, strlen(send_data), 1000); // 返回状态
}
// 判断是否收到"OFF"命令
else if (strstr(receive_data, "OFF") != NULL)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET); // 熄灭LED
LED_Status = 0; // 记录LED状态
sprintf(send_data, "LED is OFF.\r\n");
HAL_UART_Transmit(&huart2, (uint8_t*)send_data, strlen(send_data), 1000); // 返回状态
}
// 判断是否收到"STATUS"命令
else if (strstr(receive_data, "STATUS") != NULL)
{
if (LED_Status == 1)
{
sprintf(send_data, "LED is ON.\r\n");
HAL_UART_Transmit(&huart2, (uint8_t*)send_data, strlen(send_data), 1000); // 返回状态
}
else
{
sprintf(send_data, "LED is OFF.\r\n");
HAL_UART_Transmit(&huart2, (uint8_t*)send_data, strlen(send_data), 1000); // 返回状态
}
}
memset(receive_data, 0, sizeof(receive_data)); // 清空接收数组
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
/*Configure GPIO pin : PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
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);
/*Configure GPIO pins : PB0 PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
```
3. 在stm32f0xx_it.c文件中添加以下代码,以解决HAL库在使用UART2时会出现的问题:
```c
void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart2);
}
```
4. 编译、下载代码到STM32F051K8U6开发板上,连接ESP8266模块的RX引脚和TX引脚到STM32F051K8U6的PB0和PB1引脚上,将LED的控制引脚连接到STM32F051K8U6的PB2引脚上。
5. 使用串口调试软件,在COM口上打开串口,输入"ON",LED灯会点亮,串口返回"LED is ON.";输入"OFF",LED灯会熄灭,串口返回"LED is OFF.";输入"STATUS",串口返回LED的状态。
希望这个代码可以帮到你!
阅读全文