stm32f103 usart1 使用dma中断接收发送实现
时间: 2023-09-05 18:13:39 浏览: 255
STM32F103USART+DMA收发空闲中断方式 - 副本.zip
下面是使用DMA中断实现STM32F103 USART1接收和发送的示例代码:
```c
#include "stm32f10x.h"
#define USART1_DR_Address 0x40013804
#define BUFFER_SIZE 256
uint8_t RxBuffer[BUFFER_SIZE];
uint8_t TxBuffer[BUFFER_SIZE];
volatile uint16_t RxCounter = 0;
volatile uint16_t TxCounter = 0;
volatile uint8_t RxComplete = 0;
volatile uint8_t TxComplete = 0;
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
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_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)USART1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = BUFFER_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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)USART1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = BUFFER_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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void DMA1_Channel4_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC4) == SET) {
DMA_ClearITPendingBit(DMA1_IT_TC4);
RxComplete = 1;
}
}
void DMA1_Channel5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC5) == SET) {
DMA_ClearITPendingBit(DMA1_IT_TC5);
TxComplete = 1;
}
}
void USART1_DMA_Send(uint8_t* buffer, uint16_t size)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
DMA_Cmd(DMA1_Channel5, DISABLE);
while (DMA_GetCmdStatus(DMA1_Channel5) == ENABLE);
DMA_SetCurrDataCounter(DMA1_Channel5, size);
DMA_MemoryTargetConfig(DMA1_Channel5, (uint32_t) buffer, DMA_Memory_0);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
void USART1_DMA_Receive(uint8_t* buffer, uint16_t size)
{
DMA_Cmd(DMA1_Channel4, DISABLE);
while (DMA_GetCmdStatus(DMA1_Channel4) == ENABLE);
DMA_SetCurrDataCounter(DMA1_Channel4, size);
DMA_MemoryTargetConfig(DMA1_Channel4, (uint32_t) buffer, DMA_Memory_0);
DMA_Cmd(DMA1_Channel4, ENABLE);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
DMA_Configuration();
NVIC_Configuration();
USART1_DMA_Receive(RxBuffer, BUFFER_SIZE);
while (1) {
if (RxComplete) {
RxComplete = 0;
RxCounter = BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Channel4);
// 处理接收到的数据
// ...
USART1_DMA_Send(TxBuffer, TxCounter);
}
if (TxComplete) {
TxComplete = 0;
TxCounter = 0;
// 发送完成
}
}
}
```
代码中使用了DMA的循环模式,接收和发送缓冲区大小均为256字节,可以根据需要进行修改。接收完成后会触发DMA中断,处理接收到的数据并发送响应数据。发送完成后会触发DMA中断,可以进行下一次发送。
阅读全文