stm32的DMA串口应用(以串口1为例)
1.定义宏常量
#define BuffSize 12
#define DATA_HEAD 0xAA
#define DATA_END 0x55
#define USART1_DR_Address (u32)0x40013804
2.声明DMA初始化函数
void DMA_Configuration(void);
3.打开DMA1时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
4.调用初始化函数
DMA_Configuration();
5.DMA初始化函数
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize =BuffSize;
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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
// DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
//USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
}
6.为要发送的数据赋值
TxBuffer[0]=DATA_HEAD;
TxBuffer[1]=DATA_HEAD;
TxBuffer[2]=PH;
TxBuffer[3]=PL;
TxBuffer[4]=pressureh;
TxBuffer[5]=pressurem;
TxBuffer[6]=pressurel;
TxBuffer[7]=TH;
TxBuffer[8]=TL;
TxBuffer[9]=rh;
TxBuffer[10]=DATA_END;
TxBuffer[11]=DATA_END;
7.在适当的地方调用DMA发送命令
// USART_Cmd(USART1, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET){}
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET){}
// USART_Cmd(USART1, DISABLE);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_ClearFlag(DMA1_FLAG_TC4);
USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);
DMA1_Channel4->CNDTR = BuffSize;
DMA1_Channel4->CMAR = (u32)TxBuffer;