色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32的串口采用DMA方式接收數(shù)據測試

          STM32的串口采用DMA方式接收數(shù)據測試

          作者: 時間:2016-11-19 來源:網絡 收藏
          環(huán)境:

          主機:WINXP

          本文引用地址:http://cafeforensic.com/article/201611/318289.htm

          開發(fā)環(huán)境:MDK4.23

          MCU:STM32F103CBT6

          說明:

          串口可以配置成用DMA的方式接收數(shù)據,不過DMA需要定長才能產生接收中斷,如何接收可變長度的數(shù)據呢?

          方法有以下3種:

          1.將RX腳與一路時鐘外部引腳相連,當串口一幀發(fā)完,即可利用此定時器產生超時中斷.這個實時性較高,可以做到1個字節(jié)實時監(jiān)測.

          2.不改變硬件,開啟一個定時器監(jiān)控DMA接收,如果超時則產生中斷.這個實時性不高,因為超時時間必須要大于需要接收幀的時間,精度不好控制.

          3.STM32單片機有的串口可以監(jiān)測總線是否處于空閑,如果空閑則產生中斷.可以用它來監(jiān)測DMA接收是否完畢.這種方式實時性很高.

          本文采用第3種方式.在波特率576000下大數(shù)據包沖擊證明可行.

          源代碼:

          //串口接收DMA緩存#define UART_RX_LEN		128extern uint8_t Uart_Rx[UART_RX_LEN];

          //串口接收DMA緩存uint8_t Uart_Rx[UART_RX_LEN] = {0};

          //---------------------串口功能配置---------------------//打開串口對應的外設時鐘  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //串口發(fā)DMA配置  //啟動DMA時鐘RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA發(fā)送中斷設置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在傳輸時緩沖區(qū)的長度DMA_InitStructure.DMA_BufferSize = 100;//設置DMA的外設遞增模式,一個外設DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//設置DMA的內存遞增模式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外設數(shù)據字長DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//內存數(shù)據字長DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//設置DMA的傳輸模式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//設置DMA的優(yōu)先級別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);//串口收DMA配置  //啟動DMA時鐘RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA1通道5配置DMA_DeInit(DMA1_Channel5);//外設地址DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//內存地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;//dma傳輸方向單向DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//設置DMA在傳輸時緩沖區(qū)的長度DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;//設置DMA的外設遞增模式,一個外設DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//設置DMA的內存遞增模式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外設數(shù)據字長DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//內存數(shù)據字長DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//設置DMA的傳輸模式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//設置DMA的優(yōu)先級別DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//設置DMA的2個memory中的變量互相訪問DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel5,&DMA_InitStructure);//使能通道5DMA_Cmd(DMA1_Channel5,ENABLE);//初始化參數(shù)  //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發(fā)送中斷,TC傳輸完成中斷,RXNE接收中斷,PE奇偶錯誤中斷,可以是多個   //USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//中斷配置USART_ITConfig(USART1,USART_IT_TC,DISABLE);USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);USART_ITConfig(USART1,USART_IT_IDLE,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;              //中斷響應優(yōu)先級0  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                 //打開中斷  NVIC_Init(&NVIC_InitStructure);   //采用DMA方式發(fā)送USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//采用DMA方式接收USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);//啟動串口  USART_Cmd(USART1, ENABLE); 

          //串口1接收中斷   void USART1_IRQHandler(void)                               {   uint32_t temp = 0;uint16_t i = 0;if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET){//USART_ClearFlag(USART1,USART_IT_IDLE);temp = USART1->SR;temp = USART1->DR; //清USART_IT_IDLE標志DMA_Cmd(DMA1_Channel5,DISABLE);temp = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);for (i = 0;i < temp;i++){Data_Receive_Usart = Uart_Rx[i];//啟動串口狀態(tài)機usart_state_run(); }//設置傳輸數(shù)據長度DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);//打開DMADMA_Cmd(DMA1_Channel5,ENABLE);} __nop(); } 

          測試結果:

          條件:單片機運行于72M,與PC通信速率為460800.PC每隔100ms發(fā)送一個9個字節(jié)的包:c5 5c 6 0 6F 10 5 4e f7.

          測試:單片機每次收到此包,一個IO作電平跳轉,然后處理返回一包.

          示波器顯示:


          放大顯示:




          評論


          技術專區(qū)

          關閉