在延迟中观察到的时间不正确。

huangapple go评论64阅读模式
英文:

Incorrect timing in delay is observed

问题

以下是代码的翻译部分:

以下代码用于生成一个具有两种不同定时的单行信号,如果在vTaskDelay()中设置超过10毫秒,即观察到完美的延迟,但如果设置小于10毫秒,则无法观察到完美的信号。请在这方面帮助我。

#include <stdio.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include <stdbool.h>
#include "esp_err.h"
#include "sdkconfig.h"
#include "driver/timer.h"

#define GPIO_OUTPUT_IO 2
#define TIMER_GROUP 0
#define TIMER_INDEX 0

uint64_t task_counter_value;
void turn_off(void *pvParameters);
void turn_on(void *pvParameters);

void turn_on(void *pvParameters)
{
    gpio_set_level(GPIO_OUTPUT_IO, 1);
    vTaskDelay(pdMS_TO_TICKS(30));
    xTaskCreate(&turn_off, "turn_off", 2048, NULL, 10, NULL);
    vTaskDelete(NULL);
}

void turn_off(void *pvParameters)
{
    gpio_set_level(GPIO_OUTPUT_IO, 0);
    vTaskDelay(pdMS_TO_TICKS(8));
    xTaskCreate(&turn_on, "turn_on", 2048, NULL, 10, NULL);
    vTaskDelete(NULL);
}

void app_main(void)
{
    timer_config_t config;
    config.alarm_en = false;
    config.auto_reload = true;
    config.counter_dir = TIMER_COUNT_UP;
    config.intr_type = TIMER_INTR_LEVEL;
    config.counter_en = TIMER_PAUSE;
    config.divider = 80;

    gpio_config_t io_conf;
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = (1ULL << GPIO_OUTPUT_IO);
    io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 0;
    gpio_config(&io_conf);

    timer_init(TIMER_GROUP, TIMER_INDEX, &config);
    xTaskCreate(&turn_on, "turn_on", 2048, NULL, 10, NULL);

    while (true)
    {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
英文:

The below code is to generate a single line signal with two different timings, doing good if we give more than 10ms in vTaskDelay(), i.e getting perfect delay is observed but if we give lesser than 10ms the perfect signal is not observed. Please help me in this regard.

#include &lt;stdio.h&gt;
#include &quot;esp_timer.h&quot;
#include &quot;freertos/FreeRTOS.h&quot;
#include &quot;freertos/task.h&quot;
#include &quot;esp_log.h&quot;
#include &quot;driver/gpio.h&quot;
#include &lt;stdbool.h&gt;
#include &quot;esp_err.h&quot;
#include &quot;sdkconfig.h&quot;
#include &quot;driver/timer.h&quot;

#define GPIO_OUTPUT_IO 2
#define TIMER_GROUP 0
#define TIMER_INDEX 0

uint64_t task_counter_value;
void turn_off(void *pvParameters);
void turn_on(void *pvParameters);

void turn_on(void *pvParameters)
{
gpio_set_level(GPIO_OUTPUT_IO, 1);
vTaskDelay(pdMS_TO_TICKS(30));
xTaskCreate(&amp;turn_off, &quot;turn_off&quot;, 2048, NULL, 10, NULL);
vTaskDelete(NULL);
}

void turn_off(void *pvParameters)
{
gpio_set_level(GPIO_OUTPUT_IO, 0);
vTaskDelay(pdMS_TO_TICKS(8));
xTaskCreate(&amp;turn_on, &quot;turn_on&quot;, 2048, NULL, 10, NULL);
vTaskDelete(NULL);
}

void app_main(void)
{
timer_config_t config;
config.alarm_en = false;
config.auto_reload = true;
config.counter_dir = TIMER_COUNT_UP;
config.intr_type = TIMER_INTR_LEVEL;
config.counter_en = TIMER_PAUSE;
config.divider = 80;

gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL &lt;&lt; GPIO_OUTPUT_IO);
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&amp;io_conf);

 timer_init(TIMER_GROUP, TIMER_INDEX, &amp;config);
 xTaskCreate(&amp;turn_on, &quot;turn_on&quot;, 2048, NULL, 10, NULL);

 while (true)
{
  vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

答案1

得分: 0

我认为在这里更好的选择是使用时间控制外设,而不是依赖任务调度器的时钟计时器。
我的意思是,例如在ESP32目标上,你可以使用LEDC外设,通过硬件轻松获得所需的频率。我认为你需要50%的占空比。LEDC是一个PWM硬件控制器。我没有使用过,但我认为你可以将其配置为50%的占空比。
这里有更多信息,当然还有来自Espressif的示例代码。

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html

希望这对你有帮助。

英文:

I believe it is much better to use a time control peripheral here, and not to rely on task scheduler tick timer.
I mean, for example on ESP32 target you can use LEDC peripheral, and by hardware you will easily get frequencies in the order you need. I thinkyou need 50% duty cycle. LEDC is a PWM hardware controller. I didn´t use that but I think you can configure it for a 50% duty cycle.
Here you have more information, and of course you have sample code from Espresif.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html

Hope this helps you.

huangapple
  • 本文由 发表于 2023年2月14日 20:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定