1,系统EVENT的实现
本项目是一个简单的系统,在这个项目中,我们采用了一种Timer3中断计时,时间到了以后,置位EVENT标志,然后再在主程序执行的方案。
- while(1)
- {
- if( newEVENT_id & EVENT_1)
- {
- newEVENT_id = newEVENT_id & (~EVENT_1);
- newEventStart(EVENT_1, 2000);
- }
- if( newEVENT_id & EVENT_2)
- {
- newEVENT_id = newEVENT_id & (~EVENT_2);
- newEventStart(EVENT_2, 3000);
- }
- }
复制代码- //-------------------------------------------------
- void newEventStart(uint32_t inputID, uint16_t endTime)
- {
- switch(inputID)
- {
- case EVENT_1:
- EVENT_1_start=1;
- EVENT_1_count=0;
- EVENT_1_endTime = endTime;
- break;
- case EVENT_2:
- EVENT_2_start=1;
- EVENT_2_count=0;
- EVENT_2_endTime = endTime;
- break;
- }
- }
复制代码
EVENT的计时,放在Timer3的中断里面:
到了EndTime以后,则置位EVENT标志
- /* USER CODE BEGIN 1 */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if (htim->Instance == htim3.Instance) //---Timer3 中断入口 ---------
- {
- newEventCount();
- }
- }
复制代码- //------------------------------------------------
- void newEventCount()
- {
- if(EVENT_1_start)
- {
- EVENT_1_count++;
- if(EVENT_1_count >= EVENT_1_endTime)
- {
- EVENT_1_start = 0;
- EVENT_1_count =0;
- newEVENT_id = newEVENT_id | EVENT_1;
- }
- }
- if(EVENT_2_start)
- {
- EVENT_2_count++;
- if(EVENT_2_count >= EVENT_2_endTime)
- {
- EVENT_2_start = 0;
- EVENT_2_count =0;
- newEVENT_id = newEVENT_id | EVENT_2;
- }
- }
- }
复制代码 这样的话,我们开启一个EVENT,来指示系统是否正常运行,就很简单了,下面的简单代码就实现了:
- //--LED1 Indicate the system running-------
- if( newEVENT_id & EVENT_1)
- {
- newEVENT_id = newEVENT_id & (~EVENT_1);
- LED1_status = LED_FLASH_oneTime;
- LED1_FLASH_count=0;
- newEventStart(EVENT_1, 2000);
- }
复制代码
2,串口的接收
串口接收按以下思路进行:
1,打开串口接收中断,每接收到一个字节即中断一次;
2,在中断里,把收到的字节移入接收数组里,同时启动一个EVENT,如果不断的有新的字节收到,即不断进入中断,即不断重新启动EVENT;
3,当没有新的数据进来的时候,EVENT会不断计数到终止;
4,EVENT计数到终止的时候,会置位串口收到数据的标志;
5,在主程序里面处理收到的数据;
这样串口接收的好处,不需要知道来的数据是否有特殊标志、是否有特殊长度,它实际上是判断如果6MS没有新的数据,即表示当前数据接收完毕。
- HAL_UART_Receive_IT(&huart1, aRxBuffer1, 1); // Enable the USART1 Interrupt
复制代码- //------------------------
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- //----UART1收到1个字节后 中断后来到这里----------
- if(huart->Instance == huart1.Instance )
- {
- UART1_received =1; //--设置UART1收到数据的标志
- UART1_receiveData[UART1_receive_len] = aRxBuffer1[0]; //--把数据放在UART1接收数据的数组里
- UART1_receive_len++;
- if ( UART1_receive_len >= UART1_receiveBufLen)
- {
- UART1_receive_len=0;
- }
- UART1_receive_count=0; //--清零收到数据的计数,该计数在TIMER3中断里面累加,如果累加到6,则认为这次串口接收数据结束
- }
- }
复制代码- /* USER CODE BEGIN 1 */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if (htim->Instance == htim3.Instance) //---Timer3 中断入口 ---------
- {
- LED1_FLASH();
- LED2_FLASH();
- newEventCount();
- HAL_UART_ReceivedCount();
- //HAL_I2C_ReceivedCount();
- }
- }
复制代码- //-------------------------------
- void HAL_UART_ReceivedCount()
- {
- //---如果UART1收到了数据,则计数累加,到6表明UART1串口接收结束------
- if ( UART1_received )
- {
- UART1_receive_count++;
- if (UART1_receive_count >= 6) //---Default is 6
- {
- EVENT_DO_UART = EVENT_DO_UART | EVENT_uart1_Received;
- UART1_received=0;
- UART1_receive_count=0;
- }
- }
- }
复制代码
- //----UART1 Received Data Completed-----------------------------------
- if(EVENT_DO_UART & EVENT_uart1_Received)
- {
- EVENT_DO_UART = EVENT_DO_UART & (~EVENT_uart1_Received);
- dtk_uart1receivedData(UART1_receiveData, UART1_receive_len);
- UART1_receive_len=0;
- }
复制代码
|