写出52单片机与esp8266实现串口通信传输数据的代码
时间: 2023-06-18 13:07:01 浏览: 101
基于52单片机的串口通信
4星 · 用户满意度95%
以下是基于STM32F103C8T6单片机和ESP8266模块的串口通信传输数据的代码,供参考:
```c
#include "stm32f10x.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define USART1_GPIO_RCC RCC_APB2Periph_GPIOA
#define USART1_RCC RCC_APB2Periph_USART1
#define USART1_GPIO GPIOA
#define USART1_TX GPIO_Pin_9
#define USART1_RX GPIO_Pin_10
#define USART2_GPIO_RCC RCC_APB2Periph_GPIOA
#define USART2_RCC RCC_APB1Periph_USART2
#define USART2_GPIO GPIOA
#define USART2_TX GPIO_Pin_2
#define USART2_RX GPIO_Pin_3
USART_InitTypeDef USART_InitStructure;
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(USART1_GPIO_RCC, ENABLE);
RCC_APB2PeriphClockCmd(USART2_GPIO_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART1_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
RCC_APB2PeriphClockCmd(USART1_RCC, ENABLE);
RCC_APB1PeriphClockCmd(USART2_RCC, ENABLE);
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
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_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void USART1_SendStr(char *data)
{
while (*data)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, *data++);
}
}
void USART2_SendStr(char *data)
{
while (*data)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, *data++);
}
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
static uint8_t index = 0;
static uint8_t data[1024] = {0};
uint8_t rxData = USART_ReceiveData(USART2);
data[index++] = rxData;
if (rxData == '\n')
{
data[index] = '\0';
USART1_SendStr((char *)data);
index = 0;
memset(data, 0, sizeof(data));
}
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
while (1)
{
USART2_SendStr("AT\r\n"); // 发送AT指令给ESP8266模块
delay_ms(1000);
}
}
```
以上代码中,我们使用了USART1和USART2两个串口,其中USART1用于与电脑进行串口通信,USART2用于与ESP8266模块进行串口通信。在USART2接收到数据时,我们将数据存储到data数组中,并在接收到'\n'时触发读取数据的操作,并将数据发送给USART1进行输出。
阅读全文