STM32G031 wireless -5 open source projects temperature and humidity, and the system serial receive EVENT

Keywords: CubeMX, CubeIDE, STM32G031C8T6, AHT10, DRF1609H

1, system implementation EVENT

This project is a simple system, in this project, we used a Timer3 interrupt timing, the time arrived, set EVENT flag, then execute the program in the main program.

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 timing, Timer3 interrupt on the inside:
the future to EndTime, the flag is set 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;
   	   }
   }
}

In this case, we open a EVENT, to indicate whether the system is operating normally, very simple, the following simple code is realized:

		//--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, the reception serial

Serial reception along the following lines:
1, open the serial port receive interrupt, each byte is received that is interrupted once;
2, at the break, the received byte into the receiving array, and initiates an EVENT, if ever there new byte is received, i.e., continue to enter the interrupt, i.e. keeps restarting the EVENT;
. 3, when there is no new data coming in, EVENT will continue to count termination;
. 4, EVENT terminating the counted time, closing the serial port bit will be set the flag data;
5, the main program processing the received data;
benefits of this serial received, whether there is not need to know the data specific to the flag, whether a special length, it is actually determines if there is no new data 6MS , it means that the current data has been received.

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;
		}
Released eight original articles · won praise 7 · views 557

Guess you like

Origin blog.csdn.net/yihua2009/article/details/104295192