stm32f103c8t6与esp8266通信代码
时间: 2023-04-01 17:03:01 浏览: 152
stm32+esp8266+阿里云+温湿度+光照-烟雾-远程显示+控制-之STM32代码
5星 · 资源好评率100%
你好,以下是stm32f103c8t6与esp8266通信的代码示例:
#include "stm32f10x.h"
#include "usart.h"
#define ESP8266_USART USART1
#define ESP8266_USART_CLK RCC_APB2Periph_USART1
#define ESP8266_USART_GPIO GPIOA
#define ESP8266_USART_GPIO_CLK RCC_APB2Periph_GPIOA
#define ESP8266_USART_TX_PIN GPIO_Pin_9
#define ESP8266_USART_RX_PIN GPIO_Pin_10
void ESP8266_USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(ESP8266_USART_CLK | ESP8266_USART_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ESP8266_USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ESP8266_USART_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ESP8266_USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(ESP8266_USART_GPIO, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(ESP8266_USART, &USART_InitStructure);
USART_Cmd(ESP8266_USART, ENABLE);
}
void ESP8266_SendData(char *data)
{
while (*data)
{
USART_SendData(ESP8266_USART, *data++);
while (USART_GetFlagStatus(ESP8266_USART, USART_FLAG_TXE) == RESET);
}
}
int main(void)
{
ESP8266_USART_Config();
while (1)
{
ESP8266_SendData("Hello, ESP8266!\r\n");
delay_ms(100);
}
}
阅读全文