STM32 是一款基于ARM Cortex-M3內核的32位MCU,主頻最高可達72M。最近因為要在車機上集成TPMS功能, 便開始著手STM32的開發工作,STM32F10x系列共有5個串口(USART1~USART5),支持DMA方式通信,DMA方式由于不需要CPU的參與,而是直接由DMA控制器完成串口數據的讀寫,因而可以很大程度的提高CPU的利用率。在使用STM32串口之前需要做一系列的初始化工作:
1.RCC(復位和時鐘控制寄存器)初始化,啟用GPIO、DMA、USART時鐘。
2.NVIC(嵌套向量中斷控制寄存器)初始化,完成各個硬件中斷的配置。
3.USART初始話,配置串口,設置DMA通道等。
4.DMA初始化,完成DMA的配置。
源代碼:
配置:
//---------------------串口功能配置---------------------
//打開串口對應的外設時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
//啟動DMA時鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//DMA發送中斷設置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA1通道4配置
DMA_DeInit(DMA1_Channel4);
//外設地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1-》DR);
//內存地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;
//dma傳輸方向單向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//設置DMA在傳輸時緩沖區的長度
DMA_InitStructure.DMA_BufferSize = 100;
//設置DMA的外設遞增模式,一個外設
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//設置DMA的內存遞增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外設數據字長
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//內存數據字長
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//設置DMA的傳輸模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//設置DMA的優先級別
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//設置DMA的2個memory中的變量互相訪問
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
//使能通道4
//DMA_Cmd(DMA1_Channel4, ENABLE);
//初始化參數
//USART_InitStructure.USART_BaudRate = DEFAULT_BAUD;
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_InitStructure.USART_BaudRate = DEFAULT_BAUD;
//初始化串口
USART_Init(USART1,&USART_InitStructure);
//TXE發送中斷,TC傳輸完成中斷,RXNE接收中斷,PE奇偶錯誤中斷,可以是多個
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置UART1中斷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道設置為串口1中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //中斷占先等級0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中斷響應優先級0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開中斷
NVIC_Init(&NVIC_InitStructure); //初始化
//采用DMA方式發送
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
//啟動串口
USART_Cmd(USART1, ENABLE);
//設置IO口時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//串口1的管腳初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管腳9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //選擇GPIO響應速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管腳10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //選擇GPIO響應速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
//設置IO口時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管腳9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //選擇GPIO響應速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
中斷函數:
//串口1DMA方式發送中斷
void DMA1_Channel4_IRQHandler(void)
{
//清除標志位
DMA_ClearFlag(DMA1_FLAG_TC4);
//DMA_ClearITPendingBit(DMA1_FLAG_TC4);
//DMA1-》IFCR |= DMA1_FLAG_TC4;
//關閉DMA
DMA_Cmd(DMA1_Channel4,DISABLE);
//DMA1_Channel4-》CCR &= ~(1《《0);
//允許再次發送
Flag_Uart_Send = 0;
}
發送測試:
//串口DMA發送測試
Uart_Send_Buffer[0] = 1;
Uart_Send_Buffer[1] = 2;
Uart_Send_Buffer[2] = 3;
Uart_Send_Buffer[3] = 4;
Uart_Send_Buffer[4] = 5;
i = 1;
while (1)
{
//檢查串口是否可以發送
while (Flag_Uart_Send);
Flag_Uart_Send = 1;
//設置傳輸數據長度
DMA_SetCurrDataCounter(DMA1_Channel4,i);
//打開DMA
DMA_Cmd(DMA1_Channel4,ENABLE);
i++;
if (i 》 5)
{
i = 1;
}
}
評論
查看更多