stm32f103 usart1 dma中断接收发送
时间: 2023-09-26 09:05:37 浏览: 118
好的,针对您的问题,我可以给您提供一些思路:
1. 初始化USART1和DMA,将USART1的接收和发送都使用DMA进行传输。
2. 在USART1的接收DMA中断处理函数中,可以将接收到的数据存储到一个缓冲区中,并且在缓冲区已满或接收到特定字符时触发一个自定义的事件。
3. 在USART1的发送DMA中断处理函数中,可以检查是否有新的数据需要发送,并将其从缓冲区中拿出来进行发送。
4. 在主程序中,可以开启USART1的接收和发送,并且注册自定义的事件处理函数,在事件触发时进行相应的处理。
5. 需要注意的是,在使用DMA时,需要配置好DMA的通道和缓冲区大小,以及DMA的传输模式(单次传输或循环传输)等参数。
希望以上的思路可以帮助您实现基于STM32F103的USART1 DMA中断接收和发送。
相关问题
stm32f103 usart1 dma中断接收发送配置及实现函数
以下是使用STM32F103的USART1,通过DMA方式接收和发送数据的配置及实现函数:
1. 配置USART1
首先需要配置USART1的引脚、波特率、数据位、停止位等参数。在这里我们假设USART1的引脚已经正确连接。以下是典型的USART1配置代码:
```c
// 使能USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// USART1 GPIO 配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1 配置
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);
// 使能USART1
USART_Cmd(USART1, ENABLE);
```
2. 配置DMA
接下来需要配置DMA,以便能够使用DMA方式进行数据传输。在这里我们使用DMA1的通道4来接收数据,使用通道5来发送数据。以下是DMA配置代码:
```c
// 使能DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// 配置DMA接收
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)rx_buf; // 接收缓冲区
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel5, ENABLE);
// 配置DMA发送
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)tx_buf; // 发送缓冲区
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0; // 初始发送长度为0
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_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); // 使能发送完成中断
```
3. 编写DMA中断服务函数
在使用DMA进行数据传输时,需要编写DMA中断服务函数。在这里我们需要编写发送完成中断服务函数和接收完成中断服务函数。以下是中断服务函数的代码:
```c
// DMA1 Channel4 发送完成中断服务函数
void DMA1_Channel4_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC4) != RESET)
{
// 清除中断标志位
DMA_ClearITPendingBit(DMA1_IT_TC4);
// 关闭DMA发送通道
DMA_Cmd(DMA1_Channel4, DISABLE);
// 发送完成回调函数
if (tx_complete_callback != NULL)
tx_complete_callback();
}
}
// DMA1 Channel5 接收完成中断服务函数
void DMA1_Channel5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC5) != RESET)
{
// 清除中断标志位
DMA_ClearITPendingBit(DMA1_IT_TC5);
// 接收完成回调函数
if (rx_complete_callback != NULL)
rx_complete_callback();
}
}
```
4. 实现发送和接收函数
最后需要实现发送和接收函数,以便能够使用DMA方式进行数据传输。以下是典型的发送和接收函数代码:
```c
// 发送函数
void usart1_dma_send(uint8_t *buf, uint16_t len)
{
if (len == 0)
return;
// 等待DMA发送通道空闲
while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET);
// 设置发送长度
DMA_SetCurrDataCounter(DMA1_Channel4, len);
// 设置发送缓冲区地址
DMA_SetMemoryBaseAddr(DMA1_Channel4, (uint32_t)buf);
// 使能DMA发送通道
DMA_Cmd(DMA1_Channel4, ENABLE);
}
// 接收函数
uint16_t usart1_dma_receive(uint8_t *buf, uint16_t len)
{
uint16_t rx_len = RX_BUF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);
if (rx_len == 0)
return 0;
if (rx_len > len)
rx_len = len;
// 复制接收缓冲区数据
memcpy(buf, rx_buf, rx_len);
// 重新启动DMA接收通道
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel5, RX_BUF_SIZE);
DMA_Cmd(DMA1_Channel5, ENABLE);
return rx_len;
}
```
以上就是使用STM32F103的USART1,通过DMA方式接收和发送数据的配置及实现函数。注意,以上代码仅供参考,具体实现需要结合实际应用场景进行相应的修改。
stm32f103 usart1 使用dma中断接收发送实现
下面是使用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中断,可以进行下一次发送。
阅读全文