英文:
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 <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);
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论