the mcpwm not generate frequency to 1mhz
100khz
1mhz
for this usage 5 bit pwm
4 wire
have idea to increrase frequency to 1mhz ?
Code: Untitled.cpp Select all
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <iostream>
using namespace std;
#include <WString.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "driver/uart.h"
#include "driver/temp_sensor.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_attr.h"
#include "driver/mcpwm.h"
#include "soc/mcpwm_periph.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_err.h"
#include "SD.h"
#include "FS.h"
#include "SPI.h"
/********************************** SD **********************************************************/
#define SD_CS (GPIO_NUM_41) // SD_CS 41
#define SDMMC_CMD (GPIO_NUM_40) // SD_MOSI 40
#define SDMMC_CLK (GPIO_NUM_39) // SD_CLK 39
#define SDMMC_D0 (GPIO_NUM_38) // SD_MISO 38
uint64_t SD_CARD_INFO[5] = {0,0,0,0,0}; //type and present, size, totalbyte, used, freebyte
/** @brief ESP32 Phae-Shift-PWM Example
*
* Uses the driver for the MCPWM hardware modules on the Espressif ESP32
* or ESP32-S3 SoC for generating a Phase-Shift-PWM waveform between
* two pairs of hardware pins. (Not compatible with ESP32-S2)
*
* Application in power electronics, e.g. Zero-Voltage-Switching (ZVS)
* Full-Bridge-, Dual-Active-Bridge- and LLC converters.
*
* 2021-05-21 Ulrich Lukas
*/
#define H_BRIDGE_H0 (GPIO_NUM_20) // PWM0A
#define H_BRIDGE_L0 (GPIO_NUM_21) // PWM0B
#define H_BRIDGE_D0 (GPIO_NUM_42) // disable
#define H_BRIDGE_H1 (GPIO_NUM_35) // PWM1A
#define H_BRIDGE_L1 (GPIO_NUM_36) // PWM1B
#define H_BRIDGE_D1 (GPIO_NUM_37) // disable
#define H_BRIDGE_FREQUENCY 27000 // max frequency is 1e5 i requred 1e6
#define H_BRIDGE_PWM_BIT 10 //not used
#define H_BRIDGE_PWM_PARTIAL 33
//************************************************************
void buck_Enable(){
mcpwm_config_t pwm_config; //Timer configuration structure
pwm_config.frequency = H_BRIDGE_FREQUENCY; //frequency = 1000Hz
pwm_config.cmpr_a = 0;
pwm_config.cmpr_b = 0;
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
MCPWM0.clk_cfg.clk_prescale = ( ( ( APB_CLK_FREQ / 1000000 ) * 2 ) - 1 ); //GROUP CLOCK_IN IS 160MHz, my target is 1us x tick
MCPWM0.timer[0].timer_cfg0.timer_prescale = 4; //THIS IS NOT WORKING!!!
MCPWM0.timer[0].timer_cfg1.timer_mod = 0;
MCPWM0.timer[0].timer_cfg0.timer_period = 5;
// mcpwm_set_frequency(MCPWM_UNIT_0, MCPWM_TIMER_0,H_BRIDGE_FREQUENCY);
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Timers 0 and 1 initialised with same setup
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config);
mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_0, &pwm_config); //Timers 0 and 1 initialised with same setup
mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_1, &pwm_config);
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 0 deadtime setup
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 1 deadtime setup
mcpwm_deadtime_enable(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 0 deadtime setup
mcpwm_deadtime_enable(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 1 deadtime setup
//Star using the PWM Duty values such that both driver inputs are set LOW i.e both Half bridge transistos are off
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,0); //set to 0%
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,100); //start up set to 100% PWM
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,0); //set to 0%
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,100); //start up set to 100% PWM
//Sync all timers. Generate a sync output of timer0 by invoking mcpwm_set_timer_sync_output()
//and selecting desired event to generate sync output from:-
mcpwm_set_timer_sync_output(MCPWM_UNIT_0, MCPWM_TIMER_0,MCPWM_SWSYNC_SOURCE_TEZ); //'MCPWM_SWSYNC_SOURCE_TEZ' = the sync signal is generated when Timer0 counts to zero
//Sync all timers
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 0); // sync source is from TIMER_0
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 0); //creates 2nd phase on Pin13, a full cycle is 1000
// mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 500); //creates 3rd phase on Pin14,
// mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 500); //creates 3rd phase on Pin14,
mcpwm_set_timer_sync_output(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SWSYNC_SOURCE_TEZ);
mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 0);
mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 0);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, H_BRIDGE_H0); // PWM generation commences following init commands
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, H_BRIDGE_L0);
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM0A, H_BRIDGE_H1); // PWM generation commences following init commands
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM1B, H_BRIDGE_L1);
/*
mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_0); //Saw comments that stop-start is recommended but doesnt seem to be needed??
// vTaskDelay(pdMS_TO_TICKS(10));
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0); //give a clean cycle start to PWM generator, no part duty waveforms
mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_1);
// vTaskDelay(pdMS_TO_TICKS(10));
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_1);
*/
// digitalWrite(GPIO_TOGGLE_OUT, HIGH); //Toggle a pin..for scope diagnostic purposes
}
//****************************************************************
void Set_PWM(float duty){
if(duty>4){
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,duty);
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,duty);
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,duty);
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,duty);
}
else{ //Disable the driver inputs by setting both inputs LOW
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,0); //0%PWM = A LOW
mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,100); //100% PWM, B compliment = LOW
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,0); //0%PWM = A LOW
mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,100); //100% PWM, B compliment = LOW
}
}
//*****************************************************************
void setup() {
pinMode(H_BRIDGE_D1, OUTPUT); // sets the digital pin 13 as output
digitalWrite(H_BRIDGE_D1, HIGH); // sets the status monitor pin low
pinMode(H_BRIDGE_D0, OUTPUT); // sets the digital pin 13 as output
digitalWrite(H_BRIDGE_D0, HIGH); // sets the status monitor pin low
buck_Enable();
Set_PWM(H_BRIDGE_PWM_PARTIAL);
delay(500);
digitalWrite(H_BRIDGE_D0,LOW); // sets the status monitor pin low
digitalWrite(H_BRIDGE_D1,LOW); // sets the status monitor pin low
}
//**********************************************************************
void loop(){
}