首页 > 解决方案 > ESP32 xTaskGetTickCount 不返回正确的值

问题描述

为什么这个简单的代码不起作用,如果 xTaskGetTickCount 不起作用,应该有什么替代方法?

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);
                }
 }

标签: esp32freertos

解决方案


我已经通过以下方式解决了它:

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);
                }
 }

推荐阅读