ESP32-外设PWM

好记心不如烂笔头

文章目录

      • 函数定义
      • 使用

函数定义

#include 
#include 
//#include 
#include "sdkconfig.h"//sdk配置
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"//任务
#include "freertos/message_buffer.h" //使用消息缓冲区必须要有这个头文件
#include "esp_chip_info.h" //芯片_信息
#include "esp_flash.h"  //内存
#include "esp_system.h" //系统
#include "esp_spi_flash.h"
#include "esp_log.h"  //日志

#include "gatts_table_creat_demo.h"//ble蓝牙

#include "driver/gpio.h" //GPIO
#include "driver/ledc.h" //LEDPWM
#include "driver/mcpwm.h"//电机LEDPWM


static const char *TAG = "example1";

#define led_pin GPIO_NUM_2
#define led_deng_dai GPIO_NUM_12

/*LEDPWM初始化*/
void led_pwm_Config()
{
    ledc_timer_config_t ledc_timer_Structure;
    ledc_timer_Structure.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_timer_Structure.timer_num = LEDC_TIMER_0;
    ledc_timer_Structure.duty_resolution = 13; // 分辨率
    ledc_timer_Structure.freq_hz = 5000;
    ledc_timer_Structure.clk_cfg = LEDC_APB_CLK;
    ledc_timer_config(&ledc_timer_Structure);
    ledc_channel_config_t ledc_channel_Structure;
    ledc_channel_Structure.gpio_num = led_deng_dai ; // gpio 12
    ledc_channel_Structure.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_channel_Structure.channel = LEDC_CHANNEL_0;
    ledc_channel_Structure.intr_type = LEDC_INTR_DISABLE;
    ledc_channel_Structure.timer_sel = 0;
    ledc_channel_Structure.duty = 0;
    ledc_channel_Structure.hpoint = 0;
    ledc_channel_config(&ledc_channel_Structure);
    // 若想改变占空比,用这个。
    // ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 8190 * 0.4); // 负占空比,负值为50/256=19.5%; 0-8190
    // ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
}
/*发送LEDPWM方波*/
void ledPwmTianYao(float zhanKongBi)
{
    ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 8190 * zhanKongBi); // 负占空比,负值为50/256=19.5%; 0-8190
    ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
}

/*MCPWM方波*/
void ,cpPwmTianYao(float zhanKongBi)
{
    mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, led_deng_dai_gao); // 一台 MCPWM 发生器就足够了
    mcpwm_config_t pwm_config = {
        .frequency = 2000, // 频率=2kHz
        .cmpr_a = 0,       // PWMxA 的占空比 = 0
        .counter_mode = MCPWM_UP_COUNTER,
        .duty_mode = MCPWM_DUTY_MODE_0,
    };
    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
    float aaa = 0;
    bool ac = true;
    while (ac)
    {
        ESP_LOGI(TAG, "现在的占空比为: %f", aaa);
        ESP_ERROR_CHECK(mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_GEN_A, aaa));
        if (aaa > 98)
        {
            ac = false;
        }
        aaa = aaa + 10;
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

// GPIO初始化
void gpioInit(void)
{
    gpio_reset_pin(led_pin); // 清除 GPIO 默认的状态
    gpio_reset_pin(led_deng_dai);

    // gpio_set_direction(led_deng_dai_gao, GPIO_MODE_OUTPUT); // GPIO 输出模式
    gpio_set_direction(led_deng_dai, GPIO_MODE_INPUT_OUTPUT); // GPIO 输出模式
}

使用

void app_main(void)
{
	gpioInit();
    led_pwm_Config();
    //调用其中一个
	ledPwmTianYao(60.5);
	tianYao(80);
}

你可能感兴趣的:(ESP32,ESP32)