STM32F051K8U6控制esp8266控制led灯的亮灭,urat2,并实时返回灯的状态,STM32CubeMX代码
时间: 2024-03-02 17:52:16 浏览: 102
STM32控制LED亮灭-HAL库
好的,下面是基于STM32CubeMX的代码实现:
```
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "string.h"
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* Private user code ---------------------------------------------------------*/
#define LED_GPIO_Port GPIOA
#define LED_Pin GPIO_PIN_0
#define ESP8266_AT_Cmd "AT+CIPSEND=0,5\r\n"
#define ESP8266_Send_Cmd "LED\r\n"
void ESP8266_Init()
{
HAL_Delay(2000);
HAL_UART_Transmit(&huart2, (uint8_t *)"AT+RST\r\n", strlen("AT+RST\r\n"), 1000);
HAL_Delay(3000);
HAL_UART_Transmit(&huart2, (uint8_t *)"AT+CWMODE=1\r\n", strlen("AT+CWMODE=1\r\n"), 1000);
HAL_Delay(1000);
HAL_UART_Transmit(&huart2, (uint8_t *)"AT+CIPMUX=0\r\n", strlen("AT+CIPMUX=0\r\n"), 1000);
HAL_Delay(1000);
}
void ESP8266_Send_Cmd_Data()
{
HAL_UART_Transmit(&huart2, (uint8_t *)ESP8266_AT_Cmd, strlen(ESP8266_AT_Cmd), 1000);
HAL_Delay(500);
HAL_UART_Transmit(&huart2, (uint8_t *)ESP8266_Send_Cmd, strlen(ESP8266_Send_Cmd), 1000);
HAL_Delay(500);
}
void LED_On()
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
void LED_Off()
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
void LED_Toggle()
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
ESP8266_Init();
while (1)
{
ESP8266_Send_Cmd_Data();
uint8_t ESP8266_RecvData[10] = {0};
HAL_UART_Receive(&huart2, ESP8266_RecvData, 10, 500);
if (strstr((char *)ESP8266_RecvData, "LEDON"))
{
LED_On();
HAL_Delay(500);
HAL_UART_Transmit(&huart2, (uint8_t *)"ON\r\n", strlen("ON\r\n"), 1000);
}
else if (strstr((char *)ESP8266_RecvData, "LEDOFF"))
{
LED_Off();
HAL_Delay(500);
HAL_UART_Transmit(&huart2, (uint8_t *)"OFF\r\n", strlen("OFF\r\n"), 1000);
}
HAL_Delay(1000);
}
}
/* System Clock Configuration */
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_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USART2 init function */
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();
}
}
/* GPIO init function */
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}
/* Error_Handler function */
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
/* assert_failed function */
void assert_failed(char *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */
```
在代码中,我们使用了USART2与ESP8266通信,并且使用了AT指令来控制ESP8266,然后通过串口接收ESP8266返回的数据,根据数据来控制LED灯的亮灭。你可以根据自己的实际需求修改代码,比如修改LED灯的GPIO口、修改ESP8266的通信波特率等。
阅读全文