编写代码:用USART2 实现与PC通信,实现PC向STM32发送数据,STM32将数据回传给PC波特率 195200,数据位8位。采用中断方式接收数据。
时间: 2024-03-25 22:36:08 浏览: 75
好的,这是一个典型的USART2串口通信的应用场景。我们需要配置USART2的时钟、引脚、波特率、数据位、中断等参数,并编写中断服务函数。下面是一个简单的例子供你参考:
```c
#include "stm32f10x.h"
#define USART_RX_BUF_SIZE 64
#define USART_TX_BUF_SIZE 64
volatile uint8_t USART_RX_BUF[USART_RX_BUF_SIZE];
volatile uint8_t USART_RX_STA = 0;
volatile uint8_t USART_TX_BUF[USART_TX_BUF_SIZE];
volatile uint8_t USART_TX_STA = 0;
void USART2_Init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baudrate;
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(USART2, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART2);
if ((USART_RX_STA & 0x80) == 0)
{
if (data == 0x0d)
{
USART_RX_STA |= 0x80;
}
else
{
USART_RX_BUF[USART_RX_STA & 0x3f] = data;
USART_RX_STA++;
if (USART_RX_STA > (USART_RX_BUF_SIZE - 1))
{
USART_RX_STA = 0;
}
}
}
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
if (USART_TX_STA & 0x80)
{
USART_TX_STA = 0;
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
else
{
USART_SendData(USART2, USART_TX_BUF[USART_TX_STA]);
USART_TX_STA++;
if (USART_TX_STA > (USART_TX_BUF_SIZE - 1))
{
USART_TX_STA |= 0x80;
}
}
USART_ClearITPendingBit(USART2, USART_IT_TXE);
}
}
void USART2_SendString(uint8_t *str)
{
while (*str)
{
while ((USART_TX_STA & 0x80) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != SET));
USART_TX_BUF[USART_TX_STA & 0x3f] = *str;
USART_TX_STA++;
if (USART_TX_STA > (USART_TX_BUF_SIZE - 1))
{
USART_TX_STA = 0;
}
USART_SendData(USART2, *str++);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
}
int main(void)
{
USART2_Init(195200);
while (1)
{
if (USART_RX_STA & 0x80)
{
USART_RX_BUF[USART_RX_STA & 0x3f] = 0;
USART_RX_STA = 0;
USART2_SendString((uint8_t*)"Received: ");
USART2_SendString((uint8_t*)USART_RX_BUF);
USART2_SendString((uint8_t*)"\r\n");
}
}
}
```
这段代码主要做了以下几件事情:
1. 配置USART2的GPIO引脚和时钟,并初始化USART2的参数和中断。
2. 实现USART2的接收和发送中断服务函数,分别处理接收和发送数据。
3. 在main函数中不断循环检测接收到的数据,如果接收到了回车符,就把接收到的数据发送回去。
需要注意的几个点:
- USART_RX_BUF和USART_TX_BUF是接收和发送数据的缓冲区,分别为64字节大小。
- USART_RX_STA和USART_TX_STA是接收和发送状态的标志位,分别记录接收到的字节数和发送的字节数。
- USART2_SendString是发送字符串的函数,该函数会等待上一次发送完成后再发送下一次数据,以避免串口数据冲突。
- 在USART2_IRQHandler函数中,需要清除中断标志位,否则会一直进入中断服务函数。
- 在main函数中,需要不断循环检测接收到的数据,否则会出现接收错误。
阅读全文