https://docs.espressif.com/projects/esp ... tml#adjust
Code: Select all
[env:esp32dev]
platform = espressif32@4.3
board = esp32dev
framework = espidf
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/mcpwm.h"
- #include "soc/mcpwm_periph.h"
- void mcpwm_example_gpio_initialize(void)
- {
- printf("initializing mcpwm gpio...\n");
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, 18); // Channel 0: GPIO 18
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, 15); // Channel 1: GPIO 15
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1A, 21); // Channel 2: GPIO 21
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, 5); // Channel 3: GPIO 5
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM2A, 4); // Channel 4: GPIO 4
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM2B, 19); // Channel 5: GPIO 19
- }
- void app_main()
- {
- // Initialize MCPWM GPIO
- mcpwm_example_gpio_initialize();
- // Configuration for MCPWM
- mcpwm_config_t pwm_config = {
- .frequency = 800, // Frequency (Hz) for channels
- .cmpr_a = 50.0, // Duty cycle for channels 0
- .cmpr_b = 50.0, // Duty cycle for channels 1
- .counter_mode = MCPWM_UP_COUNTER,
- .duty_mode = MCPWM_DUTY_MODE_0
- };
- // Initialize MCPWM units and timers
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); // Timer 0 for channels 0, 1,
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config); // Timer 1 for channels 2, 3
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_2, &pwm_config); // Timer 2 for channels 3, 4
- //I need to synchronize all 3 of Unit 0's timers (i.e. sync timer1 and timer2 to timer0)
- mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, 1, 0);
- mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_2, 1, 0);
- }