esp8266接收数据后通过stm32f103c8t6的usart3串口传回单片机代码实现
时间: 2023-09-06 20:09:22 浏览: 74
下面是将ESP8266接收到的数据通过STM32F103C8T6的USART3串口传回单片机的代码实现。
首先,要在STM32F103C8T6上配置USART3串口。可以使用STM32CubeMX进行配置,也可以手动编写代码进行配置。下面是手动编写代码进行配置的示例:
```c
#include "stm32f10x.h"
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//使能USART3和GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//将USART3的Tx引脚(PB10)配置为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//将USART3的Rx引脚(PB11)配置为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3的参数配置
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(USART3, &USART_InitStructure);
//使能USART3
USART_Cmd(USART3, ENABLE);
}
```
接下来,可以使用ESP8266的AT指令将ESP8266设置为透传模式,以便将接收到的数据直接传回STM32F103C8T6。
```c
void ESP8266_Setup(void)
{
USART3_Config();
//ESP8266进入透传模式
USART3_SendString("AT+CIPMODE=1\r\n");
USART3_WaitForResponse("OK\r\n");
USART3_SendString("AT+CIPSTART=\"TCP\",\"192.168.1.100\",8000\r\n");
USART3_WaitForResponse("OK\r\n");
USART3_SendString("AT+CIPSEND\r\n");
USART3_WaitForResponse(">");
}
```
在透传模式下,ESP8266会将接收到的数据直接通过TCP连接发送给指定的IP地址和端口号。在这个示例中,IP地址为192.168.1.100,端口号为8000。可以根据实际情况进行修改。
最后,可以在STM32F103C8T6的USART3接收中断中读取接收到的数据,并将数据发送回ESP8266的TCP连接。
```c
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART3);
USART_SendData(USART3, data);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
```
需要注意的是,在发送数据之前,需要先等待ESP8266发送的提示符“>”出现,表示可以开始发送数据。可以使用USART3_WaitForResponse函数来等待ESP8266的响应。
```c
void USART3_WaitForResponse(char* response)
{
char buffer[100];
uint8_t i = 0;
while(1)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART3);
buffer[i++] = data;
if(strstr(buffer, response) != NULL)
{
break;
}
}
}
}
```
完整的代码如下所示:
```c
#include "stm32f10x.h"
#include <string.h>
void USART3_Config(void);
void ESP8266_Setup(void);
void USART3_WaitForResponse(char* response);
int main(void)
{
USART3_Config();
ESP8266_Setup();
while(1);
return 0;
}
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//使能USART3和GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//将USART3的Tx引脚(PB10)配置为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//将USART3的Rx引脚(PB11)配置为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3的参数配置
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(USART3, &USART_InitStructure);
//使能USART3
USART_Cmd(USART3, ENABLE);
//使能USART3接收中断
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_EnableIRQ(USART3_IRQn);
}
void ESP8266_Setup(void)
{
USART3_SendString("AT+CIPMODE=1\r\n");
USART3_WaitForResponse("OK\r\n");
USART3_SendString("AT+CIPSTART=\"TCP\",\"192.168.1.100\",8000\r\n");
USART3_WaitForResponse("OK\r\n");
USART3_SendString("AT+CIPSEND\r\n");
USART3_WaitForResponse(">");
}
void USART3_WaitForResponse(char* response)
{
char buffer[100];
uint8_t i = 0;
while(1)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART3);
buffer[i++] = data;
if(strstr(buffer, response) != NULL)
{
break;
}
}
}
}
void USART3_SendString(char* string)
{
while(*string)
{
USART_SendData(USART3, *string++);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART3);
USART_SendData(USART3, data);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
```
阅读全文