stm32f103c8t6的esp8266代码
时间: 2023-08-30 19:05:26 浏览: 95
两个stm32f103c8t6控制两个esp8266相互通信,并根据收到的数据执行相关指令
5星 · 资源好评率100%
以下是基于STM32F103C8T6的ESP8266代码,使用了HAL库:
```
#include "main.h"
#include "stdio.h"
#include "string.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
#define ESP8266_BAUDRATE 115200
#define ESP8266_RX_BUF_SIZE 50
#define ESP8266_TX_BUF_SIZE 50
uint8_t esp8266_rx_buf[ESP8266_RX_BUF_SIZE];
uint8_t esp8266_tx_buf[ESP8266_TX_BUF_SIZE];
void esp8266_send_cmd(char* cmd);
void esp8266_send_data(char* data);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1)
{
esp8266_send_cmd("AT\r\n");
HAL_Delay(1000);
esp8266_send_cmd("AT+RST\r\n");
HAL_Delay(1000);
esp8266_send_cmd("AT+CWMODE=1\r\n");
HAL_Delay(1000);
esp8266_send_cmd("AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n");
HAL_Delay(10000);
esp8266_send_data("Hello, ESP8266!\r\n");
HAL_Delay(1000);
}
}
void esp8266_send_cmd(char* cmd)
{
HAL_UART_Transmit(&huart1, (uint8_t*)cmd, strlen(cmd), 1000);
HAL_UART_Receive(&huart1, esp8266_rx_buf, ESP8266_RX_BUF_SIZE, 1000);
printf("%s", esp8266_rx_buf);
}
void esp8266_send_data(char* data)
{
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data), 1000);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses 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_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = ESP8266_BAUDRATE;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void _Error_Handler(char * file, int line)
{
while(1)
{
}
}
```
请注意,这里的代码只是一个简单的示例,你需要根据自己的需求进行修改和优化。同时,还需要将代码中的“SSID”和“PASSWORD”替换为你要连接的WiFi网络的名称和密码。
阅读全文