PWM with sync input

User avatar
tomi_isp
Posts: 2
Joined: Tue Sep 10, 2019 4:19 pm

PWM with sync input

Postby tomi_isp » Tue Sep 10, 2019 4:29 pm

Hi,

I need (on ESP32) generate PWM signal (400Hz), this is fine but I need to sync this PWM to AC mains 50Hz (via optocoupler).
Can anyone point me how to do on ESP32/Arduino ? In old design (M2560) I have connected "sync" signal to INT pin and just reset/restart Timer what generates PWM on each rising edge. How to make it on ESP32?
And second question, I also need to measure duty cycle of PWM signal (again ~400Hz), any idea/example?
All what I need to do is convert/measure input PWM signal (not stable, ~400Hz, not synced to AC) to stable 400Hz PWM synced to AC frequency. (Duty cycle at input/output is recalculated/optimized but that is not important for this question)

Thanks for any help or idea
/Tomi

arnoldg
Posts: 3
Joined: Sun Sep 01, 2019 6:07 am

Re: PWM with sync input

Postby arnoldg » Fri Sep 13, 2019 8:01 pm

Hi,

Have you looked at MCPWM, i'm using it for a AC Dimmer.
It works great, please keep in mind that if you use a optocoupler for synch use a dual schmitt trigger to get a stable signal.
i had some interupt errors.

http://www.bristolwatch.com/ele2/zcnew.htm

markiv
Posts: 6
Joined: Mon Sep 16, 2019 7:46 am

Re: PWM with sync input

Postby markiv » Mon Sep 16, 2019 7:51 am

Hey I am too trying to make a ac dimmer using mcpwm but their is lot of flickering !, I am using 4n35 optocoupler and getting a smooth wave .

User avatar
tomi_isp
Posts: 2
Joined: Tue Sep 10, 2019 4:19 pm

Re: PWM with sync input

Postby tomi_isp » Wed Sep 18, 2019 1:05 pm

Hi,

after some experiments I was able to set PWM/SYNC with mcpwm in Arudino, seems this works fine but I also try to setup Capture mode, I follow example for mcpwm but I stuck on reading/setting registers, problem is with this lines:
  1. static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
it end with error: 'mcpwm_dev_t' does not name a type

and of course this lines is affected :
  1. mcpwm_intr_status = MCPWM[MCPWM_UNIT_0]->int_st.val;
  2. MCPWM[MCPWM_UNIT_0]->int_clr.val = mcpwm_intr_status;
  3. MCPWM[MCPWM_UNIT_0]->int_ena.val = CAP0_INT_EN;
have anyone idea what is wrong or how to fix this (in Arduino) ?

Thanks
/Tomi

markiv
Posts: 6
Joined: Mon Sep 16, 2019 7:46 am

Re: PWM with sync input

Postby markiv » Mon Sep 23, 2019 9:04 am

//#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"
#include "mcpwm1.h"
#include "string.h"
#include "freertos/queue.h"
#include "esp_attr.h"
#include "soc/rtc.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
//#include "mcpwm.h"
#define GPIO_PWM0A_OUT 17
#define GPIO_PWM0B_OUT 19
#define GPIO_PWM1A_OUT 21
#define GPIO_PWM1B_OUT 22
#define GPIO_PWM2A_OUT 23
#define GPIO_PWM2B_OUT 12
#define GPIO_SYNC0_IN 18 //Set GPIO 18 as SYNC0
//#define MCPWM_CLK_PRESCL 159 //MCPWM clock prescale------160/160=1mhz
//#define TIMER_CLK_PRESCALE 199 //MCPWM timer prescales----1mhz/(50hz*(39+1))--50hz
mcpwm_pin_config_t pin_config;
mcpwm_config_t pwm_config;
mcpwm_timer_t i;
// MCPWM_TIMER_0;
// MCPWM_TIMER_1;
// MCPWM_TIMER_2;
int k;
mcpwm_dev_t *MCPWM_U[2] = {&MCPWM0, &MCPWM1};
void setup() {
Serial.begin(115200);
pin_config.mcpwm0a_out_num = GPIO_PWM0A_OUT;
pin_config.mcpwm0b_out_num = GPIO_PWM0B_OUT;
pin_config.mcpwm1a_out_num = GPIO_PWM1A_OUT;
pin_config.mcpwm1b_out_num = GPIO_PWM1B_OUT;
pin_config.mcpwm2a_out_num = GPIO_PWM2A_OUT;
pin_config.mcpwm2b_out_num = GPIO_PWM2B_OUT;
pin_config.mcpwm_sync0_in_num = GPIO_SYNC0_IN;
gpio_pulldown_en(GPIO_NUM_18);
for(int num=0;num<3;num++){
if (num==0){ i=MCPWM_TIMER_0;}
else if (num==1){ i=MCPWM_TIMER_1;}
else if (num==2){ i=MCPWM_TIMER_2;}
pwm_config.frequency = 50;
pwm_config.cmpr_a = 0;
pwm_config.cmpr_b = 0;//duty cycle of PWMxb = 50.0%
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_1;
mcpwm_init(MCPWM_UNIT_0, i, &pwm_config);

mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);

// mcpwm_sync_enable(MCPWM_UNIT_0, i,MCPWM_SELECT_SYNC0, 1);
}
MCPWM_U[0]->clk_cfg.prescale=255;
MCPWM_U[0]->timer[0].period.period=300; //----10MS TIME
MCPWM_U[0]->timer[0].period.prescale=255;
MCPWM_U[0]->timer[0].period.upmethod = 0;
MCPWM_U[0]->channel[0].cmpr_value[0].cmpr_val = 150;
MCPWM_U[0]->channel[0].cmpr_cfg.a_upmethod = BIT(0);
MCPWM_U[0]->timer[0].sync.timer_phase=10;
MCPWM_U[0]->timer_synci_cfg.t0_in_sel = MCPWM_SELECT_SYNC0;
MCPWM_U[0]->channel[0].gen_cfg0.t0_sel= 3;
MCPWM_U[0]->channel[0].gen_cfg0.upmethod= 1;
MCPWM_U[0]->timer[0].sync.in_en = 1;
MCPWM_U[0]->timer_sel.operator0_sel=0;
MCPWM_U[0]->channel[0].generator[0].ut0=3;
// MCPWM_U[0]->channel[0].generator[0].utez=1;
//MCPWM_U[0]->channel[0].generator[0].utep=2;
}
void loop(){
if (Serial.available()>0)
{ k= Serial.parseInt();
MCPWM_U[0]->timer[0].sync.timer_phase=k;
Serial.println(k);
}
}

markiv
Posts: 6
Joined: Mon Sep 16, 2019 7:46 am

Re: PWM with sync input

Postby markiv » Mon Sep 23, 2019 9:06 am

tomi_isp wrote:
Wed Sep 18, 2019 1:05 pm
Hi,

after some experiments I was able to set PWM/SYNC with mcpwm in Arudino, seems this works fine but I also try to setup Capture mode, I follow example for mcpwm but I stuck on reading/setting registers, problem is with this lines:
  1. static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
it end with error: 'mcpwm_dev_t' does not name a type

and of course this lines is affected :
  1. mcpwm_intr_status = MCPWM[MCPWM_UNIT_0]->int_st.val;
  2. MCPWM[MCPWM_UNIT_0]->int_clr.val = mcpwm_intr_status;
  3. MCPWM[MCPWM_UNIT_0]->int_ena.val = CAP0_INT_EN;
have anyone idea what is wrong or how to fix this (in Arduino) ?

Thanks
/Tomi
bro have try making it static and just include headers properly

markiv
Posts: 6
Joined: Mon Sep 16, 2019 7:46 am

Re: PWM with sync input

Postby markiv » Wed Oct 09, 2019 7:49 am

Any updates could we use mcpwm for dimming without flickering?

Who is online

Users browsing this forum: No registered users and 56 guests