#include "dma.h"
#include "led.h"
#include "delay.h"
//DMA_Stream_TypeDef *DMA_Stream是用来指定初始化的DMA的数据流编号
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
DMA_DeInit(DMA_Streamx);
while(DMA_GetCmdStatus(DMA_Streamx)!=DISABLE);
NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = chx; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//存储器到外设模式
DMA_InitStructure.DMA_BufferSize = ndtr;//数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据长度:8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据长度:8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// 使用普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//中等优先级
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(DMA_Streamx, &DMA_InitStructure);//初始化DMA Stream
}
//开启一次DMA运输
void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx,DISABLE);
while(DMA_GetCmdStatus(DMA_Streamx)!=DISABLE);
DMA_SetCurrDataCounter(DMA_Streamx,ndtr);
DMA_Cmd(DMA_Streamx,ENABLE);
DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TCIF7);
}
void DMA2_Stream7_IRQHandler()
{
if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TCIF7)==SET)
{
LED1=!LED1;
delay_ms(100);
DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TCIF7);
}
}
主函數是
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "dma.h"
//ALIENTEK 探索者STM32F407开发板 实验0
//STM32F4工程模板-库函数版本
//技术支持:
www.openedv.com
//淘宝店铺:http://eboard.taobao.com
//广州市星翼电子科技有限公司
//作者:正点原子 @ALIENTEK
#define SEND_BUF_SIZE 260
u8 SendBuff[SEND_BUF_SIZE];
const u8 TEXT_TO_SEND[]={"KEEP SILENCE"};
int main(void)
{
u8 t=0;
u8 j,mask=0;
u16 i;
uart_init(115200);
delay_init(168);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
LED_Init();
MYDMA_Config(DMA2_Stream7,DMA_Channel_4,(u32)&USART1->DR,(u32)SendBuff,SEND_BUF_SIZE);
j=sizeof(TEXT_TO_SEND);
for(i=0;i<SEND_BUF_SIZE;i++)
{
if(t>=j)
{
if(mask)
{
SendBuff
=0x0a;
t=0;
}
else
{
SendBuff=0x0d;
mask++;
}
}
else
{
mask=0;
SendBuff=TEXT_TO_SEND[t];
t++;
}
}
i=0;
while(1)
{
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //使能串口1的DMA发送
MYDMA_Enable(DMA2_Stream7,SEND_BUF_SIZE); //开始一次DMA传输!
while(1)
{
}
}
}
一周热门 更多>