STM32 UART1 DMA 發送數據

/**************************************
2013-7-17 18:28:27 auto create
McuCode 2.1 Help you! [email protected]

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

#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出的問題。

*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章