STM32F407ZGT6 串口空闲中断代码
时间: 2023-07-27 07:06:32 浏览: 96
下面是一个使用STM32F407ZGT6的串口空闲中断处理的示例代码:
```c
#include "stm32f4xx.h"
#include "stdio.h"
#define UART_RX_BUFFER_SIZE 256
volatile uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
volatile uint16_t uart_rx_buffer_index = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
// 清除IDLE标志位
volatile uint32_t tmp = USART1->SR;
tmp = USART1->DR;
// 处理接收到的数据
uint16_t rx_data_len = UART_RX_BUFFER_SIZE - DMA_GetCurrDataCounter(DMA2_Stream2);
for (uint16_t i = 0; i < rx_data_len; i++)
{
uart_rx_buffer[uart_rx_buffer_index++] = DMA2_Stream2->M0AR[i];
if (uart_rx_buffer_index >= UART_RX_BUFFER_SIZE)
{
uart_rx_buffer_index = 0;
}
}
// 重新启动DMA接收
DMA_Cmd(DMA2_Stream2, DISABLE);
DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2 | DMA_FLAG_HTIF2 | DMA_FLAG_FEIF2 | DMA_FLAG_DMEIF2 | DMA_FLAG_TEIF2);
DMA_SetCurrDataCounter(DMA2_Stream2, UART_RX_BUFFER_SIZE);
DMA_Cmd(DMA2_Stream2, ENABLE);
}
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// USART1 Rx - PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
// USART1 configuration
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_Init(USART1, &USART_InitStructure);
// DMA2 Stream2 configuration
DMA_DeInit(DMA2_Stream2);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
DMA
阅读全文