Skip to content

Instantly share code, notes, and snippets.

@CelliesProjects
Created December 4, 2019 20:12
Show Gist options
  • Save CelliesProjects/8234bd2d8346dfc371316229234a1fd5 to your computer and use it in GitHub Desktop.
Save CelliesProjects/8234bd2d8346dfc371316229234a1fd5 to your computer and use it in GitHub Desktop.
ESP32 - Example setting a clock signal (50% duty cycle) on a pin.
#include "driver/ledc.h"
static void enable_out_clock(uint8_t pin,double freq) {
periph_module_enable(PERIPH_LEDC_MODULE);
ledc_timer_bit_t bit_num = (ledc_timer_bit_t) 1; // 3 normally
int duty = pow(2, (int) bit_num) / 2;
ledc_timer_config_t timer_conf;
timer_conf.bit_num = bit_num;
timer_conf.freq_hz = freq;
timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
timer_conf.timer_num = LEDC_TIMER_0;
esp_err_t err = ledc_timer_config(&timer_conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_timer_config failed, rc=%x", err);
}
ledc_channel_config_t ch_conf;
ch_conf.channel = LEDC_CHANNEL_0;
ch_conf.timer_sel = LEDC_TIMER_0;
ch_conf.intr_type = LEDC_INTR_DISABLE;
ch_conf.duty = duty;
ch_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
ch_conf.gpio_num = pin; //s_config.pin_xclk;
err = ledc_channel_config(&ch_conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_channel_config failed, rc=%x", err);
}
// Set the PWM to the duty specified
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment