Skip to content

Instantly share code, notes, and snippets.

@simonliu009
Last active July 1, 2021 05:35
Show Gist options
  • Save simonliu009/8ba35d1bae8ec126171b06afe3a9645b to your computer and use it in GitHub Desktop.
Save simonliu009/8ba35d1bae8ec126171b06afe3a9645b to your computer and use it in GitHub Desktop.
ledc test code
/* LEDC (LED Controller) fade example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "driver/uart.h"
#define LEDC_HS_TIMER LEDC_TIMER_0
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
#define LEDC_HS_CH0_GPIO (2)
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
#define LEDC_TEST_CH_NUM (1)
#define LEDC_TEST_DUTY (8191)
#define LEDC_TEST_FADE_TIME (3000)
#define LEDC_DUTY_RESOLUTION (LEDC_TIMER_8_BIT)
#define LEDC_FREQ (20000)
void app_main()
{
uart_set_baudrate(0,115200);
int ch;
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_DUTY_RESOLUTION, // resolution of PWM duty
.freq_hz = LEDC_FREQ, // frequency of PWM signal
.speed_mode = LEDC_HS_MODE, // timer mode
.timer_num = LEDC_HS_TIMER // timer index
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.channel = LEDC_HS_CH0_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH0_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
};
ledc_channel_config(&ledc_channel);
ledc_fade_func_install(0);
while (1) {
printf("\n1. LEDC fade to duty = %d, duty resolution = %d, frequency = %d\n", LEDC_TEST_DUTY, LEDC_DUTY_RESOLUTION, LEDC_FREQ);
ledc_set_fade_with_time(ledc_channel.speed_mode,
ledc_channel.channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel.speed_mode,
ledc_channel.channel, LEDC_FADE_NO_WAIT);
vTaskDelay(6000 / portTICK_PERIOD_MS);
printf("\n2. LEDC set to duty = %d, duty resolution = %d, frequency = %d\n", LEDC_TEST_DUTY/2, LEDC_DUTY_RESOLUTION, LEDC_FREQ);
ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, LEDC_TEST_DUTY/2);
ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel);
vTaskDelay(6000 / portTICK_PERIOD_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment