写一个stm32f103的dma传输初始化代码,并且开启dma传输完成中断
时间: 2023-05-24 09:02:49 浏览: 248
stm32f103代码
4星 · 用户满意度95%
以下是stm32f103的DMA传输初始化代码,并开启DMA传输完成中断:
#include "stm32f10x.h"
void DMA1_Stream7_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream7, DMA_IT_TCIF7))
{
DMA_ClearITPendingBit(DMA1_Stream7, DMA_IT_TCIF7);
// DMA传输完成中断处理程序
}
}
void DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能DMA1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
// DMA1-Stream7通道配置
DMA_DeInit(DMA1_Stream7);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_Tx_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = USART1_Tx_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_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_Stream7, &DMA_InitStructure);
// 使能DMA1-Stream7传输完成中断
DMA_ITConfig(DMA1_Stream7, DMA_IT_TC, ENABLE);
// 配置DMA1-Stream7传输完成中断优先级
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
// 初始化DMA传输
DMA_Init();
// 启动DMA传输
DMA_Cmd(DMA1_Stream7, ENABLE);
while (1)
{
// 用户程序
}
}
阅读全文