stm32的DMA串口應(yīng)用(以串口1為例)
1.定義宏常量
#define BuffSize 12
#define DATA_HEAD 0xAA
#define DATA_END 0x55
#define USART1_DR_Address (u32)0x40013804
2.聲明DMA初始化函數(shù)
void DMA_Configuration(void);
3.打開DMA1時(shí)鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
4.調(diào)用初始化函數(shù)
DMA_Configuration();
5.DMA初始化函數(shù)
void DMA_Configuration(void)
{
// DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
}
6.為要發(fā)送的數(shù)據(jù)賦值
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.在適當(dāng)?shù)牡胤秸{(diào)用DMA發(fā)送命令
// 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;
評(píng)論