您的位置:首页 > 其它

STM32 UART1 DMA 发送数据

2013-07-18 09:13 423 查看
/**************************************

2013-7-17 18:28:27 auto create

McuCode 2.1 Help you! hehui000@163.com

**************************************/

#define Uart_c

#include "include.h"

INT8U gUartBuf[16];

//DMA的配置

static void _Uart_DMA(void)

{

DMA_InitTypeDef DMA_InitStructure;

/* 允许 DMA1 时钟 */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

/* DMA通道4*/

DMA_DeInit(DMA1_Channel4);

//指定DMA外设基地址

DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)( &(USART1->DR));   //USART1的DR

//设定DMA内存基地址

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)gUartBuf;      //获取gUartBuf的数组

//外设作为数据传输的来源

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;      //片内外设作目的地

//指定DMA通道的DMA缓存大小

DMA_InitStructure.DMA_BufferSize = 16;              //每次最多16个数据

//外设地址不递增(不变)

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不增加

//内存地址不递增(不变)

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;     //内存地址增加

//设定外设数据宽度为16位

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //Byte

DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;    //Byte

//设定DMA的工作模式普通模式,还有一种是循环模式

DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;         //普通模式

//设定DMA通道的软件优先级

DMA_InitStructure.DMA_Priority = DMA_Priority_High;        //高优先级

//使能DMA内存到内存的传输,此处没有内存到内存的传输

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;         //非内存到内存

DMA_Init(DMA1_Channel4, &DMA_InitStructure);

DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, DISABLE);         //DMA通道4传输完成中断??

/* DISABLE DMA1 channel1 */

DMA_Cmd(DMA1_Channel4, DISABLE);

}

void fn_Uart_Init(INT32U BaudRate)

{

//ToDo: Add your code Here:

  USART_InitTypeDef USART_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;  

 //uart PA IO

 {

   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);

   // Configure USART0 Rx (PA.10) as input floating

   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         

   GPIO_Init(GPIOA, &GPIO_InitStructure);

 }

       

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);//串口时钟配置RCC_APB2Periph_USART1

  _Uart_DMA();//不能放错位置

 USART_InitStructure.USART_BaudRate = BaudRate;

 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_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//使能UART1的DMA方式

 // 使能 USART1 

 USART_Cmd(USART1, ENABLE);

}

void fn_Uart_Send(INT8U *buf,INT8U num)

{

//ToDo: Add your code Here:

    INT8U i;

   DMA_Cmd(DMA1_Channel4, DISABLE);

 for(i=0;i<num;i++)

  gUartBuf[i]=buf[i];

  DMA_SetCurrDataCounter(DMA1_Channel4,num); 

  DMA_Cmd(DMA1_Channel4,ENABLE);

}

 

/*

说明:初始化前后位置重要,以上程序可改成不用DMA的,需要把DMA的相关部份删除

调试此功能,建议:先调试无DMA功能的串口,然后再增加DMA功能,否则发送不成功,您不知道是串口还是DMA出的问题。

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STM32 UART1 DMA 发送数据