STM3固件库2串口接收大于16位的字符串不使用中断
时间: 2024-03-22 10:42:27 浏览: 60
利用stm32的hal库编写的基于中断方式的16进制数据接收
在STM32固件库V2中,可以使用DMA方式来接收串口数据,从而避免使用中断方式接收大于16位的字符串,同时又不会导致CPU占用率过高。以下是一个简单的示例代码,可以接收最多256个字节的数据:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>
#define USART_RX_BUF_SIZE 256
volatile uint8_t usart_rx_buf[USART_RX_BUF_SIZE];
volatile uint16_t usart_rx_buf_head = 0;
volatile uint16_t usart_rx_buf_tail = 0;
void USART2_DMA_Configuration(void)
{
// 初始化DMA
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)usart_rx_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = USART_RX_BUF_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
// 初始化中断
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
// 启动DMA
DMA_Cmd(DMA1_Stream5, ENABLE);
}
void DMA1_Stream5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5) != RESET)
{
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
uint16_t tail = usart_rx_buf_tail;
uint16_t head = USART_RX_BUF_SIZE - DMA_GetCurrDataCounter(DMA1_Stream5);
uint16_t count = (head >= tail) ? (head - tail) : (USART_RX_BUF_SIZE - tail + head);
// 处理接收到的数据
for (uint16_t i = 0; i < count; i++)
{
// 读取一个字节
uint8_t data = usart_rx_buf[tail];
tail = (tail + 1) % USART_RX_BUF_SIZE;
// 处理数据
printf("Received: %c\n", data);
}
usart_rx_buf_tail = tail;
}
}
int main(void)
{
// 初始化串口
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, 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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
// 配置DMA
USART2_DMA_Configuration();
while (1)
{
// do something
}
}
```
在此示例代码中,我们使用了DMA方式来接收串口数据,并使用循环缓冲区来存储接收到的数据。当DMA传输完成时,中断会触发并处理接收到的数据。由于使用了DMA方式,CPU不需要等待数据的到达,可以在数据到达时进行其他任务的处理,提高系统的实时性。
阅读全文