#include <stdio.h> #include "driver/ledc.h" #include "esp_err.h" #define LEDC_TIMER LEDC_TIMER_0 //LEDC定时器 #define LEDC_MODE LEDC_LOW_SPEED_MODE //LEDC的速度模式 #define LEDC_OUTPUT_IO (12) //LEDC绑定引脚 #define LEDC_CHANNEL LEDC_CHANNEL_0 //LEDC通道 #define LEDC_DUTY_RES LEDC_TIMER_13_BIT //LEDC占空比分辨率 #define LEDC_DUTY (4095) //LEDC占空比 #define LEDC_FREQUENCY (5000) //LEDC频率 5 kHz static void example_ledc_init(void) { //LEDC定时器配置 ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_MODE, //设置定时器低速模式 .timer_num = LEDC_TIMER, //设置定时器0 .duty_resolution = LEDC_DUTY_RES, //设置定时器分辨率 13位 .freq_hz = LEDC_FREQUENCY, //设置定时器频率 5kHz .clk_cfg = LEDC_AUTO_CLK //设置定时器时钟选择 }; ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); //设置定时器配置 //配置LEDC通道 ledc_channel_config_t ledc_channel = { .speed_mode = LEDC_MODE, //设置通道模式低速模式 .channel = LEDC_CHANNEL, //设置LEDC通道0 .timer_sel = LEDC_TIMER, //设置LEDC定时器 .intr_type = LEDC_INTR_DISABLE, //设置LEDC中断类型 .gpio_num = LEDC_OUTPUT_IO, //设置LEDCGPIO引脚 .duty = 0, // Set duty to 0% //设置占空比 .hpoint = 0 //LEDC通道hpoint值 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));//配置LEDC通道参数 } void app_main(void) { // 配置LEDC外设 example_ledc_init(); // 设置占空比(LEDC的速度模式,LEDC通道,4095+1) 50% 2^13 = 8192 4096即占一半,占空比50% ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY)); // 更新占空比 ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); }
标签:定时器,LEDC,ledc,设置,占空比,define From: https://www.cnblogs.com/ycp666/p/17016900.html