金牌会员
- 积分
- 1108
- 金钱
- 1108
- 注册时间
- 2018-11-6
- 在线时间
- 240 小时
|
发表于 2020-5-26 21:14:47
|
显示全部楼层
本帖最后由 霸王猫 于 2020-5-27 13:12 编辑
- //DMA中断方式
- void USART1_IRQHandler(void)
- {
- uint16_t ch;
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
- if (USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)
- {
- USART_ClearITPendingBit(USART1 , USART_IT_IDLE); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
- ch = USART_ReceiveData(USART1);
- DMA_Cmd(DMA1_Channel5 , DISABLE); //关闭DMA,防止处理其间有数据
- DMA_ClearFlag(DMA1_FLAG_GL5 | DMA1_FLAG_TC5 | DMA1_FLAG_HT5 | DMA1_FLAG_TE5);
- ch = USART1_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA1_Channel5);
- if (ch > 0) //表示收到数据
- {
- MB_USART1.receCount = ch;
- memcpy(MB_USART1.mscomm_buffer , USART1_DMA_RX_Buffer , MB_USART1.receCount);
- }
- DMA_SetCurrDataCounter(DMA1_Channel5 , USART1_DMA_RX_BUFFER_MAX_LENGTH);
- DMA_Cmd(DMA1_Channel5, ENABLE);
- xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);//释放信号量
- portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
- }
- }
- void MODBUS_Task(void *pvParameters)
- {
- BaseType_t xResult;
- BaseType_t xStatus;
- const TickType_t xTicksToWait = pdMS_TO_TICKS( 100UL );
- uint8_t Buffer[10];
- uint8_t Over = 0x00;
- USART1_Configuration();
- USART1_DMA_Tx_Configuration();
- USART1_DMA_Rx_Configuration();
- while(1)
- {
- xResult = xSemaphoreTake(xSemaphore, portMAX_DELAY); //读取信号量
- if(xResult == pdTRUE) //读取信号时成功
- {
- 解析报文
- }//if(xResult == pdTRUE)
- }//while(1)
- }
|
|