HI team,
I am a beginner of Nordic nrf52840 DK, I would like to control the AC ceiling fan using the nrf52840 board with pwm library. As, I can see we can use either single channel or multi- channel for setting up the frequency of the pwm pin. In order to control the ac fan i am using zero crossing detector (H11AA1) so, that for every zero crossing the Triac(BT136) is triggered and pwm is set to the given duty cycle. But, what i am getting is only PWM sampling output, and the ZCD is always showing '0'. I am taking the ZCD as input assigned to gpio pin 30 and for every zero crossing i should get the given duty cycle output on GPIO pin 31 which is connected to load.
Kindly, give me suggestions or some kind of related code for getting the output.
Here, below is the code i am trying to execute.
#include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "app_error.h" #include "bsp.h" #include "nrf_delay.h" #include "app_pwm.h" #include "nrf_gpio.h" #include "nrf_gpiote.h" #define data 31 #define zerocrossing 30 //nrf_gpio_pin_dir_t zerocrossing= NRF_GPIO_PIN_DIR_INPUT; APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1. static volatile bool ready_flag; // A flag indicating PWM status. void pwm_ready_callback(uint32_t pwm_id) // PWM callback function { ready_flag = true; } int main() { nrf_gpio_cfg_input(zerocrossing,NRF_GPIO_PIN_NOPULL); ret_code_t err_code; //app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(20000L,data); app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(20000L,data,zerocrossing); /* Switch the polarity of the second channel. */ pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH; /* Initialize and enable PWM. */ err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); app_pwm_enable(&PWM1); while (true) { if(nrf_gpio_pin_read(zerocrossing)==0) { printf("entered loop\n"); printf("%d \t",nrf_gpio_pin_read(zerocrossing)); printf("%d \t",nrf_gpio_pin_out_read(data)); /* Set the duty cycle - keep trying until PWM is ready... */ while (app_pwm_channel_duty_set(&PWM1,0,40) == NRF_ERROR_BUSY); /* ... or wait for callback. */ while (!ready_flag); APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1,1,40)); nrf_delay_ms(100); nrf_gpiote_task_disable; } else{ printf("exited loop \n"); app_pwm_disable(&PWM1); nrf_gpio_pin_clear(data); } } }