You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STM32集成FreeRTOS红外发送任务执行一次后无法重复响应求助

问题:STM32 FreeRTOS任务红外发送模式仅首次执行正常,再次按键无响应

在STM32中添加了FreeRTOS任务,该任务首次执行无问题,但再次按键触发时出现异常:学习模式可多次响应按键,无故障;但发送红外的模式执行一次后,再次按键无响应。

将发送任务替换为闪灯测试代码SendLight_ON(); HAL_Delay(1000); SendLight_OFF();SendLight_ON(); HAL_Delay(1000); SendLight_OFF();,可正常重复执行。

任务代码

/* USER CODE BEGIN Header_StartSendLearnTask */
/**
* @brief Function implementing the SendLearnTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartSendLearnTask */
void StartSendLearnTask(void const * argument)
{
  /* USER CODE BEGIN StartSendLearnTask */
  osEvent event;
  uint8_t bykey = 0;
  /* Infinite loop */
  for(;;)
  {
    
    event = osMessageGet(keyCodeQueueHandle, osWaitForever); 
        if(event.status == osEventMessage)
        {
            bykey = event.value.v;
            
            switch(bykey)
            {
              case 1 : //learning mode
                LearnLight_ON();
                //Open learning record
                Re_Record_ON();
                
                //waiting for study to end
                while(infrared.IR_Status == IR_RECEIVING);
                TIM3CH4_CAP_STA = 0;
                TIM3CH4_CAP_VAL = 0;
                
                for(int i = 0; i < infrared.Count; i++) {
                  printf("infrared.KeepTime:%ld us\r\n",infrared.KeepTime[i]);
                }
                printf("Count:%d \r\n",infrared.Count);
                
                //Prevent data discrepancies and do updates
                recoverData();
                
                printf("last pos:%d \r\n", pos);
                //After learning, store the learning results
                save_struct_to_flash();
                
                //Close Study Record
                Re_Record_OFF();
                
                //Store the offset of the infrared signal in each sector in the first sector
                W25QXX_Erase_Sector(0);
                //record data offset
                dataOff[index++] = pos;
                //save the offset array
                uint8_t tempDataOff[240];
                for(uint16_t i = 0; i < index << 2; i += 4) {
                
                  tempDataOff[i] = dataOff[i] >> 24;
                  tempDataOff[i + 1] = dataOff[i] >> 16;
                  tempDataOff[i + 2] = dataOff[i] >> 8;
                  tempDataOff[i + 3] = dataOff[i];
                
                
                }
                W25QXX_Page_Program(tempDataOff, 0, index << 2);
                //Prompt to learn to complete
                LearnDone();
                
                break;
                
            case 2: 
                //send infrared signal
                ir_send();
//              SendLight_ON();
//              HAL_Delay(1000);
//              SendLight_OFF();
                
                break;
              
              
            }
        }
    
        
    osDelay(1);
  }
    
  /* USER CODE END StartSendLearnTask */
}

红外发送函数ir_send代码

void ir_send() {
  
  
    //Prevent data discrepancies and do updates
    if(infrared.Count == 0) {
    
      recoverData();
      //No infrared signal has been stored yet
      if(index == 0)
        Error_Handler();
      
      
    }
    
    // Convert the data in the buffer to a structure type
      load_struct_from_flash();
    
  
    uint16_t Count = 1; //Send the infrared signal from the first digit, the first digit is an invalid code
    uint8_t count = *buffer;
    uint16_t tempKeep[256] = {0};
    for(uint16_t i = 0; i < count; i++) {
    
      tempKeep[i] = *(buffer + Count) | (*(buffer + Count + 1)) << 8;
      Count += 2;
    
    }
    
    
    SendLight_ON();
    __HAL_TIM_SET_COUNTER(&htim2, 0);
    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 946);   // Set the PWM to 50
    Delay_us_bySysTick(9000);
    
    uint8_t keepIndex = 1;
    while(keepIndex < count &&  infrared.IR_Status == IR_IDLE) {
      
        if((keepIndex & 1) == 0) {
            //Turn on the transmit status light
             SendLight_ON();
             __HAL_TIM_SET_COUNTER(&htim2, 0);
             __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 946);   //  Set the PWM to 50
             Delay_us_bySysTick(tempKeep[keepIndex] - 25);
             

            
        }else {
            //Turn off the transmit status light
            SendLight_OFF();
            __HAL_TIM_SET_COUNTER(&htim2, 0);
            __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);   // Set the PWM to 0
            Delay_us_bySysTick(tempKeep[keepIndex] + 25);
            
          
        }
        
        
        keepIndex++;
    
    }
    
    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);   // Set the PWM to 0
    SendLight_OFF();
    free(buffer);


}

内容的提问来源于stack exchange,提问作者KunBoy5240

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:07:51