stm32f103 dma空闲中断代码
时间: 2023-10-07 16:12:10 浏览: 117
STM32F103USART+DMA收发空闲中断方式 - 副本.zip
以下是stm32f103 dma空闲中断代码的示例:
```c
#include "stm32f10x.h"
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint8_t dma_buffer[256];
void DMA1_Channel6_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC6)) // DMA transfer complete
{
DMA_ClearITPendingBit(DMA1_IT_GL6); // Clear global flag
DMA_Cmd(DMA1_Channel6, DISABLE); // Disable DMA channel
}
}
int main(void)
{
// Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// Configure DMA1 Channel6
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; // Peripheral address
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)dma_buffer; // Memory address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // Peripheral to memory
DMA_InitStructure.DMA_BufferSize = sizeof(dma_buffer); // Buffer size
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // Peripheral address not incremented
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // Memory address incremented
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; // Peripheral data size
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // Memory data size
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // Normal mode
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; // Medium priority
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // Memory to memory disabled
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
// Enable DMA1 Channel6 interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable DMA1 Channel6
DMA_Cmd(DMA1_Channel6, ENABLE);
while(1)
{
// Wait for DMA transfer complete
while(DMA_GetFlagStatus(DMA1_FLAG_TC6) == RESET);
DMA_ClearFlag(DMA1_FLAG_TC6); // Clear transfer complete flag
// Process DMA buffer
}
}
```
在上面的示例中,我们使用了DMA1通道6,它是USART1的数据寄存器(DR)的目标。我们设置了一个256字节的缓冲区dma_buffer,然后将DMA配置为将USART1的数据发送到该缓冲区中。在main()函数中,我们启用DMA1时钟,配置DMA通道6,并启用DMA1通道6中断。然后,我们等待DMA传输完成,并在处理DMA缓冲区中的数据后重复此过程。在DMA1_Channel6_IRQHandler()函数中,我们检查DMA传输完成标志,然后清除全局标志和禁用DMA通道。
阅读全文