Search code examples
esp32freertos

ESP32 xTaskGetTickCount doesn't return correct values


why does this simple code not working, and if xTaskGetTickCount doesn't work, what should be alternative?

static void PrintTextEvery8sec(void *pvParameters)
{


                TickType_t time_start = xTaskGetTickCount();

                while(1){

                    if( ( (xTaskGetTickCount() - time_start)/portTICK_PERIOD_MS) > 8000){
                        ESP_LOGI(TAG, "8 seconds has passed...!");
                        time_start = xTaskGetTickCount();
                    }

                    vTaskDelay(100 / portTICK_PERIOD_MS);
                }
 }

Solution

  • I have solved it doing the following:

    static void PrintTextEvery8sec(void *pvParameters)
    {
    
    
                    TickType_t time_start = xTaskGetTickCount();
    
                    while(1){
    
                        /*  pdTICKS give correct calculation */
                        /*  \/                               */
                        if( pdTICKS_TO_MS(xTaskGetTickCount() - time_start))** > 8000){
                            ESP_LOGI(TAG, "8 seconds has passed...!");
                            time_start = xTaskGetTickCount();
                        }
    
                        vTaskDelay(100 / portTICK_PERIOD_MS);
                    }
     }