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

How to add clock function to ble_app_uart? (SDK12 with nRF51DK)

I need to add a clock function to the ble_app_uart example (SDK12). Where can I find a hint on how to do it? Do I need an external 32768Hz quartz? I wanted to derive from the 16 MHz quartz instead to save cost.

  • What sort of clock function are you looking for?

    You don't need an external 32768Hz XTAL. You can use the RC Oscillator.

    Please be aware that using the RC Oscillator is less accurate, and draws a bit more power than having an external LFXTAL. 

    To change to the RC Oscillatorin SDK12, look for the define NRF_CLOCK_LFCLKSRC (in pca10028.h, or your custom board file if you have a custom PCB).

    To see what the different parameters mean, check out nrf_sdm.h line 204 - 235. To get started using the RC Oscillator you can set NRF_CLOCK_LFCLKSRC to:

    // Low frequency clock source to be used by the SoftDevice
    #define NRF_CLOCK_LFCLKSRC      {.source        = NRF_CLOCK_LF_SRC_RC,            \
                                     .rc_ctiv       = 16,                                \
                                     .rc_temp_ctiv  = 2,                                \
                                     .xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_500_PPM}

    Best regards,

    Edvin

  • Thanks Edvin.

    I need a Date and Clock with some accuracy for my application because I have to run a schedule according to the user needs. This schedule is set via Bluetooth and it may take several months until the user sets a different schedule again, so the clock has to be close to exact.

    I wanted to use the synthesized clock so I think all I need to do is set the source to NRF_CLOCK_SRC_SYNTH.

    The next question is where can I find information about the clock function? I need to know how I can count the time and date, including daylight saving and so on.

    Sorry but I always made the complete program in all my applications so I am not used to using code already made by someone else.

  • To make it a bit more clear, I am using the ble_app_uart example from SDK12 to start building my application. According to what I read, RTC0 and RTC1 are already used and nRF51DK doesn't have RTC2. This means I have to implement my watch (clock and date) on a Timer. Is this right?

  • Sergio T said:
    I wanted to use the synthesized clock so I think all I need to do is set the source to NRF_CLOCK_SRC_SYNTH.

     Yes. You can do this, but be aware that this draws a bit more current, but it is also more accurate than the RC oscillator.

    Sergio T said:
    The next question is where can I find information about the clock function? I need to know how I can count the time and date, including daylight saving and so on.

     Well. We don't have a library for this. The app_timer is probably the way you want to go here. You can absolutely base your application on the ble_app_uart example. Just look at e.g. the ble_app_hrs example on how to set up the app_timer for your application. This example uses it to update the simulated battery level.

    I believe that to keep track of time, you would need a timer that times out every second, 10 seconds, minute, hour, or whatever time resolution you need. Then use this timer to increment your "current time" variable. 

    To get the current time, you can look into the "current time service" which is used in the ble_app_cts_c example (description here). This service will give you the current time from the device that is connected, e.g. a phone, in order to synchronize the current time and date.

    EDIT:

    The link to the description of the ble_app_cts_c example as nRF51 is not supported in SDK15. Use this link instead:

    https://www.nordicsemi.com/DocLib/Content/SDK_Doc/nRF5_SDK/v12-3-0/ble_sdk_app_cts_c

    Best regards,

    Edvin

  • Also, I tried putting TIMER1 to work with the code below but I don't succeed to get any interrupt. What is wrong?

    I am using ble_app_uart example from SDK12.

      NRF_TIMER1->TASKS_STOP = 1;
      NRF_TIMER1->TASKS_CLEAR = 1;
      NRF_TIMER1->PRESCALER = 4; 
      NRF_TIMER1->CC[0] = 1000000;                           //  1 second
      NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; 
      NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit; 
      NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
      NRF_TIMER1->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);		
      NRF_TIMER1->TASKS_START = 1;*/
    
     

Related