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;
        }
        
    }
}

Related