环境:
主机:WINXP
开发环境:MDK4.23
MCU:STM32F103CBT6
阐明:
串口能够装备成用DMA的办法接纳数据,不过DMA需求定长才干发生接纳中止,怎么接纳可变长度的数据呢?
办法有以下3种:
1.将RX脚与一路时钟外部引脚相连,当串口一帧发完,即可使用此定时器发生超时中止.这个实时性较高,能够做到1个字节实时监测.
2.不改变硬件,舱位一个定时器监控DMA接纳,假如超时则发生中止.这个实时性不高,由于超时时刻必需求大于需求接纳帧的时刻,精度欠好操控.
3.STM32单片机有的串口能够监测总线是否处于闲暇,假如闲暇则发生中止.能够用它来监测DMA接纳是否结束.这种办法实时性很高.
本文选用第3种办法.在波特率576000下大数据包冲击证明可行.
源代码:
//串口接纳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); //串口发DMA装备 //发动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);//串口收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在传输时缓冲区的长度DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;//设置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_MemoryDataSize_Byte;//设置DMA的传输形式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//设置DMA的优先等级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);//初始化参数 //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);//中止装备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; //中止呼应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //翻开中止 NVIC_Init(&NVIC_InitStructure); //选用DMA办法发送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];//发动串口状态机usart_state_run(); }//设置传输数据长度DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);//翻开DMADMA_Cmd(DMA1_Channel5,ENABLE);} __nop(); }
测验成果:
条件:单片机运转于72M,与PC通讯速率为460800.PC每隔100ms发送一个9个字节的包:c5 5c 6 0 6F 10 5 4e f7.
测验:单片机每次收到此包,一个IO作电平跳转,然后处理回来一包.
示波器显现:
扩大显现: