标签:start getc 优先 mode 功能 ini get 结果 www.
STM32的串口采用DMA方式接收数据测试
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
参考链接:http://www.amobbs.com/forum.PHP?mod=viewthread&tid=5511863&highlight=dma%E6%8E%A5%E6%94%B6
环境:
主机:WINXP
开发环境:MDK4.23
MCU:STM32F103CBT6
说明:
串口可以配置成用DMA的方式接收数据,不过DMA需要定长才能产生接收中断,如何接收可变长度的数据呢?
方法有以下3种:
1.将RX脚与一路时钟外部引脚相连,当串口一帧发完,即可利用此定时器产生超时中断.这个实时性较高,可以做到1个字节实时监测.
2.不改变硬件,开启一个定时器监控DMA接收,如果超时则产生中断.这个实时性不高,因为超时时间必须要大于需要接收帧的时间,精度不好控制.
3.STM32单片机有的串口可以监测总线是否处于空闲,如果空闲则产生中断.可以用它来监测DMA接收是否完毕.这种方式实时性很高.
本文采用第3种方式.在波特率576000下大数据包冲击证明可行.
源代码:
- #define UART_RX_LEN 128
- extern uint8_t Uart_Rx[UART_RX_LEN];
- uint8_t Uart_Rx[UART_RX_LEN] = {0};
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
-
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
-
- 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);
-
- DMA_DeInit(DMA1_Channel4);
-
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
-
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;
-
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
-
- DMA_InitStructure.DMA_BufferSize = 100;
-
- 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_PeripheralDataSize_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);
-
-
-
-
-
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
-
- DMA_DeInit(DMA1_Channel5);
-
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
-
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;
-
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
-
- DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;
-
- 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_VeryHigh;
-
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel5,&DMA_InitStructure);
-
-
- DMA_Cmd(DMA1_Channel5,ENABLE);
-
-
-
-
- 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);
-
-
-
-
- USART_ITConfig(USART1,USART_IT_TC,DISABLE);
- USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
- USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
-
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
-
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
-
- USART_Cmd(USART1, ENABLE);
- void USART1_IRQHandler(void)
- {
- uint32_t temp = 0;
- uint16_t i = 0;
-
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
- {
-
- temp = USART1->SR;
- temp = USART1->DR;
- 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];
-
- usart_state_run();
- }
-
-
- DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);
-
- DMA_Cmd(DMA1_Channel5,ENABLE);
- }
-
- __nop();
- }
测试结果:
条件:单片机运行于72M,与PC通信速率为460800.PC每隔100ms发送一个9个字节的包:c5 5c 6 0 6F 10 5 4e f7.
测试:单片机每次收到此包,一个IO作电平跳转,然后处理返回一包.
示波器显示:
放大显示:
STM32的串口采用DMA方式接收数据测试(转)
标签:start getc 优先 mode 功能 ini get 结果 www.
原文地址:http://www.cnblogs.com/759222924lele/p/7341403.html