I am new to ESP-IDF and C, so please forgive my mistakes. I am trying to create a wifi dimmer using ESP32 wroom module. I have configured to use MCPWM APIs for the same. However I am seeing incorrect output on the connected load(bulb).
I understand that we will need to take zero crossing of incoming AC signal into account. We have created a full wave rectified for the same and the output of the rectifier have been connected to an optocouple to give out pulse. Following is the calculation :
Household freq of AC signal : 50 Hz
Output of the full wave rectified : 100 Hz ( 2 * incoming AC signal)
So according to above calculation I am getting a period of 10 ms(100 Hz) from my optocoupler output. So if my duty resolution is say 60%, then my MCPWM should fire the TRIAC on the zero crossing optocoupler interrupt for 6 ms (60% of 10 ms) and then close the triac gate for the remaining 4 ms till the next interrupt comes and this process is repeated forever.
I have given zero interrupt pin as sync input to the mcpwm module. Following is the cleaned up code
Code: Select all
#include <stdio.h>
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_attr.h"
#include "soc/rtc.h"
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#include "esp_err.h"
#include "esp_log.h"
#include "utils/general.h"
#include "utils/node_storage.h"
#include "node/pwm.h"
static const char *TAG = "sample.pwm";
#define MCPWM_EN_SYNC 1
#define GPIO_SYNC_IN 18
#define FREQ_HZ 100
static void mcpwm_example_gpio_initialize()
{
ESP_LOGI(TAG, "Initializing all pwm pins");
mcpwm_pin_config_t pin_config = {
.mcpwm0a_out_num = 16,
.mcpwm0b_out_num = 17,
.mcpwm1a_out_num = 21,
.mcpwm1b_out_num = 26,
.mcpwm_sync0_in_num = GPIO_SYNC_IN,
};
mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);
gpio_pulldown_en(GPIO_SYNC_IN); // needs to be pull down
}
/**
* @brief Configure whole MCPWM module
*/
void initialize_gpios()
{
//1. mcpwm gpio initialization
mcpwm_example_gpio_initialize();
//2. initialize mcpwm configuration
ESP_LOGD(TAG, "Configuring Initial Parameters of mcpwm");
mcpwm_config_t pwm_config;
pwm_config.frequency = FREQ_HZ;
pwm_config.cmpr_a = 0; //duty cycle of PWMxA in %
pwm_config.cmpr_b = 0; //duty cycle of PWMxb in %
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_1;
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Configure PWM0A & PWM0B with above settings
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config); //Configure PWM1A & PWM1B with above settings
#if MCPWM_EN_SYNC
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC0, 0);
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_SELECT_SYNC0, 0);
#endif
}
int change_status(int gpio, int duty) {
if (gpio == 16) {
ESP_ERROR_CHECK( mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, (float)value) );
}
else { //gpio 17
ESP_ERROR_CHECK( mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, (float)value) );
}
return ret;
}
void main() {
initialize_gpios();
int ret = change_status(16, 50);
}
In the attached video, BLUE line on the oscilloscope is for full wave rectifier(100 Hz) and yellow is for the 50% pwm duty cycle.
https://drive.google.com/open?id=1wtdYq ... Qt-NmgzNHO
Can you please help me out where I am going wrong?