在做试验之前,首要有必要理解什么是DMA,DMA的效果又体现在哪里。
{
DMA_InitTypeDef DMA_InitStructure;
//DMA设置:
//设置DMA源:内存地址&串口数据寄存器地址
//方向:内存–>外设
//每次传输位:8bit
//传输巨细DMA_BufferSize=SENDBUFF_SIZE
//地址自增形式:外设地址不增,内存地址自增1
//DMA形式:一次传输,非循环
//优先级:中
DMA_DeInit(DMA1_Channel4);//串口1的DMA传输通道是通道4
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//外设作为DMA的意图端
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;//传输巨细
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不添加
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存地址自增1
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_Medium;//(DMA传送优先级为中等)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
vu8 SendBuff[SENDBUFF_SIZE];
SendBuff[i] = i%10+0;
}
4、开端DMA传输(使能对应的DMA通道)
DMA_Cmd(DMA1_Channel4, ENABLE);
{
LED_1_REV; //LED翻转
Delay(); //浪费时刻
}
* 本文件完结串口发送功用(经过重构putchar函数,调用printf;或许USART_SendData()
* 这里是一个用串口完结很多数据传输的比如,运用了DMA模块进行内存到USART的传输
* 每逢USART的发送缓冲区空时,USART模块发生一个DMA事情,
* 此刻DMA模块呼应该事情,主动从预先界说好的发送缓冲区中拿出下一个字节送给USART
* 整个进程无需用户程序干涉,用户只需发动DMA传输传输即可
* 在仿真器调试时,能够在数据传输进程中暂停运转,此刻DMA模块并没有中止
* 串口仍然发送,标明DMA传输是一个独立的进程。
* 一起舱位接纳中止,在串口中止中将数据存入缓冲区,在main主循环中处理
* 作者:jjldc(九九)
* 代码硬件根据万利199元的EK-STM32F开发板,CPU=STM32F103VBT6
*******************************************************************************/
#include “stm32f10x_lib.h”
#include “stdio.h”
/* Private define ————————————————————*/
#define USART1_DR_Base 0x40013804
/* Private variables ———————————————————*/
#define SENDBUFF_SIZE 10240
vu8 SendBuff[SENDBUFF_SIZE];
vu8 RecvBuff[10];
vu8 recv_ptr;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void DMA_Configuration(void);
void USART1_Configuration(void);
void Delay(void);
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
u16 i;
#ifdef DEBUG
debug();
#endif
recv_ptr = 0;
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
DMA_Configuration();
USART1_Configuration();
printf(“\r\nSystem Start…\r\n”);
printf(“Initialling SendBuff… \r\n”);
for(i=0;i
SendBuff[i] = i%10+0;
}
printf(“Initial success!\r\nWaiting for transmission…\r\n”);
//发送去数据现已准备好,按下按键即开端传输
while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3));
printf(“Start DMA transmission!\r\n”);
//这里是开端DMA传输前的一些准备作业,将USART1模块设置成DMA方法作业
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//开端一次DMA传输!
DMA_Cmd(DMA1_Channel4, ENABLE);
//等候DMA传输完结,此刻咱们来做别的一些事,点灯
//实践运用中,传输数据期间,能够碑文别的的使命
while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET)
{
// LED_1_REV; //LED翻转
Delay(); //浪费时刻
}
//DMA传输完毕后,主动封闭了DMA通道,而无需手动封闭
//下面的查办被注释
//DMA_Cmd(DMA1_Channel4, DISABLE);
printf(“\r\nDMA transmission successful!\r\n”);
while (1)
{
}
}
* Function Name : 重界说体系putchar函数int fputc(int ch, FILE *f)
* Description : 串口发一个字节
* Input : int ch, FILE *f
* Output :
* Return : int ch
* 这个是运用printf的要害
*******************************************************************************/
int fputc(int ch, FILE *f)
{
//USART_SendData(USART1, (u8) ch);
USART1->DR = (u8) ch;
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
}
* Function Name : Delay
* Description : 延时函数
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Delay(void)
{
u32 i;
for(i=0;i<0xF0000;i++);
return;
}
* Function Name : RCC_Configuration
* Description : 体系时钟设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_HSEConfig(RCC_HSE_ON);
//等候外部晶振安稳
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//假如外部晶振发动成功,则进行下一步操作
if(HSEStartUpStatus==SUCCESS)
{
//设置HCLK(AHB时钟)=SYSCLK
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
//推荐值:SYSCLK = 0~24MHz Latency=0
// SYSCLK = 24~48MHz Latency=1
// SYSCLK = 48~72MHz Latency=2
FLASH_SetLatency(FLASH_Latency_2);
//舱位FLASH预取指功用
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//发动PLL
RCC_PLLCmd(ENABLE);
//等候PLL安稳
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//体系时钟SYSCLK来自PLL输出
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//切换时钟后等候体系时钟安稳
while(RCC_GetSYSCLKSource()!=0x08);
//设置体系SYSCLK时钟为HSE输入
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
//等候时钟切换成功
while(RCC_GetSYSCLKSource() != 0x04);
*/
}
//发动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,\
ENABLE);
//发动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//发动USART1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//发动DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
* Function Name : GPIO_Configuration
* Description : GPIO设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//坐落PD口的3 4 11-15脚,使能设置为输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_11 | GPIO_Pin_12 |\
GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
* Function Name : NVIC_Configuration
* Description : NVIC设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Set the Vector Table base location at 0x20000000
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
// Set the Vector Table base location at 0x08000000
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//串口接纳中止翻开
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
* Function Name : USART1_Configuration
* Description : NUSART1设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
{
DMA_InitTypeDef DMA_InitStructure;
//DMA设置:
//设置DMA源:内存地址&串口数据寄存器地址
//方向:内存–>外设
//每次传输位:8bit
//传输巨细DMA_BufferSize=SENDBUFF_SIZE
//地址自增形式:外设地址不增,内存地址自增1
//DMA形式:一次传输,非循环
//优先级:中
DMA_DeInit(DMA1_Channel4);//串口1的DMA传输通道是通道4
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//外设作为DMA的意图端
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;//传输巨细
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不添加
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存地址自增1
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal(只传送一次), DMA_Mode_Circular (不停地传送)
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//(DMA传送优先级为中等)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}