新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > stm32的DMA串口應(yīng)用(以串口1為例)

stm32的DMA串口應(yīng)用(以串口1為例)

作者: 時(shí)間:2016-11-26 來(lái)源:網(wǎng)絡(luò) 收藏
stm32DMA串口應(yīng)用(以串口1為例)
1.定義宏常量
#define BuffSize 12
#define DATA_HEAD0xAA
#define DATA_END 0x55
#define USART1_DR_Address (u32)0x40013804
2.聲明DMA初始化函數(shù)
void DMA_Configuration(void);
3.打開(kāi)DMA1時(shí)鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
4.調(diào)用初始化函數(shù)
DMA_Configuration();
5.DMA初始化函數(shù)
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.為要發(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;


關(guān)鍵詞: stm32DMA串口應(yīng)

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉