This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

what is the principle of converting Ticks in to seconds

Hello

I am trying to change the ticks value of Battery measurement interval (ble_app-prox example) but i am not able to understand how the ticks period is measured based on what i understood from the example 1s is equal to 1000ms so totally we are setting it to120s then the ticks period is equal to 120000 if am wrong please correct me and explain me the correct method my aim to see the battery level changing based on the battery -measure_interval any help would be greatly appreciated

thankyou

Parents
  • Assuming you are using nRF51 and using APP_TIMER (RTC1).

    You initialize the APP_TIMER using the prescaler. lets say that the prescaler is 0 then your timer generates 32768 ticks per second.

    The macro given in app_timer.h converts the milliseconds to number of ticks.

    #define APP_TIMER_TICKS(MS, PRESCALER)\
                ((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000))
    

    Above you see that if MS = 120000 and prescaler 0, which is 120 seconds then you will get Ticks = ((120000*32768)/(0+1)*1000) rounded up to integer = 3932160 ticks. This is telling you that the timer will put 3932160 (0X3c0000) in RTC compare register and when RTC is started and is running for this many ticks, then it will be 120 seconds.

    Does this answer your question?

Reply
  • Assuming you are using nRF51 and using APP_TIMER (RTC1).

    You initialize the APP_TIMER using the prescaler. lets say that the prescaler is 0 then your timer generates 32768 ticks per second.

    The macro given in app_timer.h converts the milliseconds to number of ticks.

    #define APP_TIMER_TICKS(MS, PRESCALER)\
                ((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000))
    

    Above you see that if MS = 120000 and prescaler 0, which is 120 seconds then you will get Ticks = ((120000*32768)/(0+1)*1000) rounded up to integer = 3932160 ticks. This is telling you that the timer will put 3932160 (0X3c0000) in RTC compare register and when RTC is started and is running for this many ticks, then it will be 120 seconds.

    Does this answer your question?

Children
  • Thanks aryan for your brief explanation the reason i would to change the presclar value is to measure the battery level and send the notifications to user if the battery level drops to 30 percent and so for doing that what is the best possible ways to do that?

  • Thanks aryan for your brief explanation the reason i would to change the presclar value is to measure the battery level and send the notifications to user if the battery level drops to 30 percent and so for doing that what is the best possible ways to do that? karan (12 mins ago)editconvert to answer

  • prescaler is only needed to be changed if your timeout is more than 512 seconds. So here you you can use 0 as prescaler.

    This below code can be used to see if ADC sample value drops to 30% and send notification.

        uint32_t err_code;
        uint16_t adc_value;
        uint8_t  percentage_batt_lvl;
        uint16_t batt_lvl_in_milli_volts;
        nrf_adc_conversion_event_clean();
        nrf_adc_stop();
        
        adc_value = nrf_adc_result_get();
        
        
        batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_value);
        
        percentage_batt_lvl     = battery_level_in_percent(batt_lvl_in_milli_volts);
    
        if(percentage_batt_lvl <= 30) {
        err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl);
        if (
            (err_code != NRF_SUCCESS)
            &&
            (err_code != NRF_ERROR_INVALID_STATE)
            &&
            (err_code != BLE_ERROR_NO_TX_PACKETS)
            &&
            (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
           )
        {
            APP_ERROR_HANDLER(err_code);
        }
       }
    
  • thanks for your kind reply more importantly onething which i didnot understand is if i power the development board via USB it is showing intially 100 percent and then later it is going down to 57percent and in opposite case if i provide the power supply to the circuit via Battery CR2032 it is showing initially 100percent and after certain interval it is dropped down to 15percent i dont understand why basically VDD pin is indicating2.65 volt (Battery supply ) and 2.88volt for USB power supply but the result of the battery percentage values shown in the MCP is strange can you tell me some informations about this two points? and diode voltage drop plays a vital role in this two case?

  • At first the application is started with 100% battery because the actual ADC sample is started in battery_level_meas_timeout_handler after 120 seconds from enabling this notification. After this value the actual pin is sampled and then in the ADC_IRQHandler you use battery_level_in_percentto convert the sampled value.

    For your CR2032, the 15% might be correct value, but it is displayed after 120 seconds because that is when the first sample is taken. If you want this to be updated faster, you need to change BATTERY_LEVEL_MEAS_INTERVAL.

    For your USB, you cannot use battery_level_in_percent in ADDC_IRQHandler as this is for CR2032. You need to write your own interpreting function in case of USB.

Related