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_ITConfig(DMA1_Channel4, DMA_IT_TC, 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;