Implementing PWM control makes it impossible to read the voltage

Hello.

I am currently working on implementing a PWM control function using nrf52840. I used the function "es_battery_voltage_get()" to read the battery voltage before and succeeded in reading the voltage. In that case, when I wrote a function for PWM control and a LOG to read the voltage at the same time in the for statement of the "main" function as shown in the second code attached below, the voltage value did not change. I thought about the reason for this, but could not figure it out, so I asked this question.
Thank you very much.

LemonCake

static void PWM_control(float luminance,int index)
{
    float period=0.002;//500[Hz]
    float time_p;
    float duty;
    float duty_ratio;

    switch(index)
    {
        case 0:
        {
            duty_ratio=luminance;
            if(duty_ratio<1.0)
            {
                duty=duty_ratio;
            }
            else
            {
                duty=1.0;
            }
            time_p=duty*period;

            hal_led_pin_set(ONOFF_SERVER_R_LED, 0);
            hal_led_pin_set(ONOFF_SERVER_G_LED, 1);
            hal_led_pin_set(ONOFF_SERVER_B_LED, 1);
            nrf_delay_ms(time_p*1000);
            hal_led_pin_set(ONOFF_SERVER_R_LED, 1);
            nrf_delay_ms((period-time_p)*1000);
            break;
        }

        case 1:
        {...The following describes the conditional branches for various colors
int main(void)
{
    initialize();
    start();

    nrf_gpio_pin_clear(POWER);
    nrf_gpio_pin_clear(ONOFF_SERVER_R_LED);
    nrf_gpio_pin_clear(ONOFF_SERVER_G_LED);
    nrf_gpio_pin_clear(ONOFF_SERVER_B_LED);
    nrf_gpio_pin_clear(BUZZER);

    nrf_gpio_cfg_output(POWER);
    nrf_gpio_cfg_output(ONOFF_SERVER_R_LED);
    nrf_gpio_cfg_output(ONOFF_SERVER_G_LED);
    nrf_gpio_cfg_output(ONOFF_SERVER_B_LED);
    nrf_gpio_cfg_output(BUZZER);

    uint16_t vbatt;
    uint16_t battVolt;
    uint16_t battVolt_1;
    es_battery_voltage_init();
    
    int color_index;
    float luminance=0.4;
    int counter;
    

    for (;;)
    {
        color_index=0;
        counter=0;
        (void)sd_app_evt_wait();
        es_battery_voltage_get(&vbatt);
        battVolt=(uint16_t)vbatt;
        battVolt_1=battVolt;
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "battery_voltage=%d\n",battVolt_1);
        while(counter<3000)
        {
            PWM_control(luminance,color_index);
            counter+=1;
        }
        
    }
}

Parents
  • Hi 

    Could you please let me know which SDK version you are using?

    I wouldn't recommend using the nrf_delay_ms() function to implement PWM, as this is only a simple busy wait that will keep the CPU running in a loop for the entirety of the delay. 

    Either you can use the PWM peripheral, which allows you to set up PWM signals much more easily, or you can use an app_timer callback to trigger the GPIO's at different points in time, in case you don't need the pins to change very often. 

    Also, I notice that you run sd_app_evt_wait() in the main loop. Do you have some kind of interrupt that will wake up the system from this sleep mode?

    Best regards
    Torbjørn

  • Hello.
    First of all, thank you very much for your very clear answer.
    As for your question, I am currently using v16.0 SDK.
    Also, regarding sd_app_evt_wait(), I don't have such an interrupt implemented at the moment.
    I apologize for the inconvenience, and thank you in advance for your patience.


    LemonCake

Reply Children
Related