MCPWM (not for motor) for 1 mhz

redowl
Posts: 5
Joined: Fri Jul 12, 2024 4:52 pm

MCPWM (not for motor) for 1 mhz

Postby redowl » Sun Oct 27, 2024 8:37 pm

mcpwm to full control a mosfet@1mhz, the bridge used in a power control a transformer 1Mhz (not khz)
the mcpwm not generate frequency to 1mhz
100khz

1mhz
for this usage 5 bit pwm
4 wire

have idea to increrase frequency to 1mhz ?

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iomanip>
  6. #include <iostream>
  7. using namespace std;
  8. #include <WString.h>
  9. #include <Arduino.h>
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #include <ArduinoJson.h>
  17. #include "driver/uart.h"
  18. #include "driver/temp_sensor.h"
  19. #include "driver/gpio.h"
  20. #include "driver/ledc.h"
  21.  
  22.  
  23.  
  24.  
  25. #include "esp_attr.h"
  26.  
  27. #include "driver/mcpwm.h"
  28. #include "soc/mcpwm_periph.h"
  29.  
  30. #include "freertos/FreeRTOS.h"
  31. #include "freertos/task.h"
  32. #include "esp_system.h"
  33. #include "esp_log.h"
  34. #include "esp_err.h"
  35. #include "SD.h"
  36. #include "FS.h"
  37. #include "SPI.h"
  38.  
  39.  
  40. /********************************** SD **********************************************************/
  41. #define SD_CS                       (GPIO_NUM_41)   // SD_CS     41
  42. #define SDMMC_CMD                   (GPIO_NUM_40)   // SD_MOSI   40
  43. #define SDMMC_CLK                   (GPIO_NUM_39)   // SD_CLK    39
  44. #define SDMMC_D0                    (GPIO_NUM_38)   // SD_MISO   38
  45. uint64_t SD_CARD_INFO[5]            = {0,0,0,0,0}; //type and present, size, totalbyte, used, freebyte
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. /** @brief ESP32 Phae-Shift-PWM Example
  57.  *
  58.  * Uses the driver for the MCPWM hardware modules on the Espressif ESP32
  59.  * or ESP32-S3 SoC for generating a Phase-Shift-PWM waveform between
  60.  * two pairs of hardware pins. (Not compatible with ESP32-S2)
  61.  *
  62.  * Application in power electronics, e.g. Zero-Voltage-Switching (ZVS)
  63.  * Full-Bridge-, Dual-Active-Bridge- and LLC converters.
  64.  *
  65.  * 2021-05-21 Ulrich Lukas
  66.  */
  67.  
  68.  
  69.  
  70. #define H_BRIDGE_H0                 (GPIO_NUM_20)       // PWM0A
  71. #define H_BRIDGE_L0                 (GPIO_NUM_21)       // PWM0B
  72. #define H_BRIDGE_D0                 (GPIO_NUM_42)       // disable
  73.  
  74.  
  75. #define H_BRIDGE_H1                 (GPIO_NUM_35)       // PWM1A
  76. #define H_BRIDGE_L1                 (GPIO_NUM_36)       // PWM1B
  77. #define H_BRIDGE_D1                 (GPIO_NUM_37)       // disable
  78.  
  79.  
  80. #define H_BRIDGE_FREQUENCY          27000 // max frequency is 1e5 i requred 1e6
  81. #define H_BRIDGE_PWM_BIT            10 //not used
  82. #define H_BRIDGE_PWM_PARTIAL        33
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. //************************************************************
  94. void buck_Enable(){
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. mcpwm_config_t pwm_config;                                       //Timer configuration structure
  103.     pwm_config.frequency = H_BRIDGE_FREQUENCY;    //frequency = 1000Hz
  104.     pwm_config.cmpr_a = 0;
  105.     pwm_config.cmpr_b = 0;
  106.     pwm_config.counter_mode = MCPWM_UP_COUNTER;
  107.     pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
  108.    
  109.  
  110.    MCPWM0.clk_cfg.clk_prescale = ( ( ( APB_CLK_FREQ / 1000000 ) * 2 ) - 1 ); //GROUP CLOCK_IN IS 160MHz, my target is 1us x tick
  111.  
  112.  
  113.  
  114. MCPWM0.timer[0].timer_cfg0.timer_prescale = 4; //THIS IS NOT WORKING!!!
  115. MCPWM0.timer[0].timer_cfg1.timer_mod = 0;
  116. MCPWM0.timer[0].timer_cfg0.timer_period = 5;
  117.  
  118.    // mcpwm_set_frequency(MCPWM_UNIT_0, MCPWM_TIMER_0,H_BRIDGE_FREQUENCY);
  119.  
  120.    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);         //Timers 0 and 1 initialised with same setup
  121.    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config);
  122.  
  123.    mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_0, &pwm_config);         //Timers 0 and 1 initialised with same setup
  124.    mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_1, &pwm_config);
  125.  
  126.  
  127.    mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 0 deadtime setup
  128.    mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 1 deadtime setup
  129.  
  130.    mcpwm_deadtime_enable(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 0 deadtime setup
  131.    mcpwm_deadtime_enable(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE,0,0); //Timer 1 deadtime setup
  132.    
  133.  
  134.   //Star using the PWM Duty values such that both driver inputs are set LOW i.e both Half bridge transistos are off
  135.   mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,0);      //set to 0%
  136.   mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,100);    //start up set to 100% PWM
  137.  
  138.   mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,0);      //set to 0%
  139.   mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,100);    //start up set to 100% PWM
  140.  
  141.  
  142.  
  143.  
  144. //Sync all timers. Generate a sync output of timer0 by invoking mcpwm_set_timer_sync_output()
  145. //and selecting desired event to generate sync output from:-
  146.    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
  147.    
  148. //Sync all timers
  149.    mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 0);   // sync source is from TIMER_0
  150.    mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 0);    //creates 2nd phase on Pin13, a full cycle is 1000
  151.   // mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 500);    //creates 3rd phase on Pin14,    
  152.   // mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 500);    //creates 3rd phase on Pin14,    
  153.    
  154.    mcpwm_set_timer_sync_output(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SWSYNC_SOURCE_TEZ);
  155.    mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_0,MCPWM_SELECT_TIMER0_SYNC, 0);
  156.    mcpwm_sync_enable(MCPWM_UNIT_1, MCPWM_TIMER_1,MCPWM_SELECT_TIMER0_SYNC, 0);      
  157.  
  158.    mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, H_BRIDGE_H0);   // PWM generation commences following init commands
  159.    mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, H_BRIDGE_L0);
  160.  
  161.    mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM0A, H_BRIDGE_H1);   // PWM generation commences following init commands
  162.    mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM1B, H_BRIDGE_L1);
  163.  
  164. /*
  165.   mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_0);   //Saw comments that stop-start is recommended but doesnt seem to be needed??
  166.   // vTaskDelay(pdMS_TO_TICKS(10));
  167.   mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0);  //give a clean cycle start to PWM generator, no part duty waveforms
  168.  
  169.   mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_1);  
  170.   // vTaskDelay(pdMS_TO_TICKS(10));
  171.   mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_1);  
  172.  */
  173.  
  174.  // digitalWrite(GPIO_TOGGLE_OUT, HIGH);     //Toggle a pin..for scope diagnostic purposes
  175.  
  176.  }
  177.  
  178. //****************************************************************
  179. void Set_PWM(float duty){
  180.     if(duty>4){      
  181.         mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,duty);
  182.         mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,duty);
  183.  
  184.         mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,duty);
  185.         mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,duty);
  186.     }
  187.     else{   //Disable the driver inputs by setting both inputs LOW
  188.       mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_GEN_A,0);   //0%PWM = A LOW
  189.       mcpwm_set_duty(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_GEN_A,100); //100% PWM, B compliment = LOW
  190.  
  191.       mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_0,MCPWM_GEN_A,0);   //0%PWM = A LOW
  192.       mcpwm_set_duty(MCPWM_UNIT_1,MCPWM_TIMER_1,MCPWM_GEN_A,100); //100% PWM, B compliment = LOW
  193.     }
  194. }
  195. //*****************************************************************
  196.  
  197. void setup() {
  198.   pinMode(H_BRIDGE_D1, OUTPUT);    // sets the digital pin 13 as output
  199.   digitalWrite(H_BRIDGE_D1, HIGH); // sets the status monitor pin low
  200.   pinMode(H_BRIDGE_D0, OUTPUT);    // sets the digital pin 13 as output
  201.   digitalWrite(H_BRIDGE_D0, HIGH); // sets the status monitor pin low
  202.  
  203.  
  204.   buck_Enable();
  205.   Set_PWM(H_BRIDGE_PWM_PARTIAL);
  206.   delay(500);
  207.   digitalWrite(H_BRIDGE_D0,LOW); // sets the status monitor pin low  
  208.   digitalWrite(H_BRIDGE_D1,LOW); // sets the status monitor pin low  
  209.  
  210.  
  211.  
  212. }
  213.  
  214. //**********************************************************************
  215. void loop(){
  216.  
  217.  
  218.  
  219. }

redowl
Posts: 5
Joined: Fri Jul 12, 2024 4:52 pm

Re: MCPWM (not for motor) for 1 Mhz or 1hz

Postby redowl » Thu Oct 31, 2024 12:59 pm

generate 1hz
  1.  
  2.  
  3. #define H_BRIDGE_FREQUENCY          1
  4. #define H_BRIDGE_PWM_PARTIAL        50
  5. #define NW_RESOLUTION 1e3         // porto la risoluzione dei bit da 1e3 a (1hz)
generate 1Mhz
  1.  
  2. #define H_BRIDGE_FREQUENCY          1e6
  3. #define H_BRIDGE_PWM_PARTIAL        50
  4. #define NW_RESOLUTION 1e8         // porto la risoluzione dei bit da 1e8 a (1Mhz)
  1. void buck_Enable(){
  2.  
  3. //configures two MCPWM Operators to create complimentary PWM signals with a set dead time on rising edges...
  4.  
  5. mcpwm_timer_set_resolution(MCPWM_UNIT_0, MCPWM_TIMER_0, NW_RESOLUTION);
  6. mcpwm_timer_set_resolution(MCPWM_UNIT_1, MCPWM_TIMER_1, NW_RESOLUTION);  //Found necessary to apply this to all timers
  7. mcpwm_group_set_resolution(MCPWM_UNIT_0, NW_RESOLUTION);
  8. mcpwm_group_set_resolution(MCPWM_UNIT_1, NW_RESOLUTION);
  9.  
  10. .....

Who is online

Users browsing this forum: No registered users and 51 guests