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

Use lower frequency(32 KHz) to measure higher frequency(8 MHz) ticks/counts for 1 second

Hi all,

I want to measure higher frequency (8 MHz) count/ticks by using the lower frequency (32 KHz) is there is any method to do it or any examples.

I want to measure the count as shown in the figure.

Thanks in advance..

Parents Reply Children
  • My bad. I wanted to measure a lower frequency using higher frequency. The intention is to calibrate the low frequency clock (32kHz LFCLK) using high frequency clock (64MHz HFCLK). I understand there could be 2 ways to do it

    1) Count the ticks of HFCLK in a single LFCLK period. Hence had asked this question

    2) Additionally, i also figured out that there is a calibration timer for this purpose (which uses CTSTART,CTSTOP,CTTO). I believe this would be a more straightforward way of achieving what i intend to do.

    Is there any sample code that i could look into for this calibration, especially using approach 2.

    Thanks,

  • Please have a look at my answer in this thread.

    You can calibrate the low frequency RC oscillator by triggering the task TASKS_CAL, and calibration can be done at a given interval by setting CTIV, and triggering CT_START task.

  • Hi Jorgen,

    Is this the way to calibrate RC oscillator .

    	NRF_CLOCK->TASKS_HFCLKSTART;
    	//NRF_CLOCK->TASKS_LFCLKSTART;
    	NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
    	NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    	NRF_CLOCK->TASKS_LFCLKSTART    = 1;
    	
    	NRF_CLOCK->TASKS_CAL=1;
    	printf("task cal\n \r");
    	printf("ctiv\n\r");
    	NRF_CLOCK->TASKS_CTSTART;
    	NRF_CLOCK->CTIV=10;
    //	NRF_CLOCK->EVENTS_DONE;
    //	NRF_CLOCK->EVENTS_CTTO;
    	NRF_CLOCK->TASKS_CTSTOP;
    
    
    	//nrf_delay_ms(1000);
    	while(NRF_CLOCK->CTIV==0)
    	{
    		nrf_delay_ms(100);
    		printf("Calibrating done\n\r");
    	}
    	printf("Calibration\n\r");

    or i am going in the wrong direction. By using this i am not getting any output. If i am going wrong please tell me where i am going wrong.

    Thank..,

  • No, this is not correct. If you trigger the calibration task (NRF_CLOCK->TASKS_CAL=1;), a NRF_CLOCK->EVENTS_DONE event is generated. This is what you should wait for in a while loop. You can run the calibration timer to get an event when you should calibrate the clock (EVENTS_CTTO), you then need to trigger the calibration task and start the timer again. Also note that you need to write 1 to the registers to trigger a task.

  • Thanks for your suggestion. I have edited the code as follows. The code runs in a while(1) loop.

    void clock_init(void)
    { NRF_CLOCK->EVENTS_CTTO=0;
      NRF_CLOCK->EVENTS_DONE=0;
    	NRF_CLOCK->CTIV=100;
    	
    	NRF_CLOCK->TASKS_CAL=1;
    	printf("task cal %d\n \r", NRF_CLOCK->EVENTS_DONE);
    	nrf_delay_ms(100); 
    	printf("task cal %d\n \r", NRF_CLOCK->EVENTS_DONE);
    //	while(NRF_CLOCK->EVENTS_DONE==0)
    //	{
    //		nrf_delay_ms(1);
    //	}
    	NRF_CLOCK->TASKS_CTSTART=1;
    	int i=0;
    	while(!(NRF_CLOCK->EVENTS_CTTO))
    	{
    
    		i++;
    
    	}
    	NRF_CLOCK->TASKS_CTSTOP;
    	printf("Count for CTTO %d \n\r",i);
    
    	
    	
    }

    Wanted to cofirm if this is what you have suggested. Further i have a few questions:

    1) I have given a delay of 100ms for the NRF_CLOCK-> EVENTS_DONE to become one after triggering TASKS_CAL. The code prints the value as 0 before the delay and 1 after the delay. However instead of giving the fixed delay, if i try to implement the same in a while loop (the commented lines in the code), the program stays in the loop forever, giving an impression that the EVENTS_DONE is never becoming 1. Anything wrong with this implementation.

    2) I wanted a better clarity about EVENTS_DONE and EVENTS_CTTO. The time required for EVENTS_DONE=1 is the time required for calibration. What exactly the EVENTS_CTTO indicate (is it the period across which the calibration is triggered ??. Does it mean that calibration happens after every CTTO interval, however the actual time for calibration is indicated by EVENTS_DONE flag?).

    3) How is the clock for EVENTS_CTTO getting counted. Is it using the low frequency clock (32kHz) or the high frequency clock (16MHz)?

    Thanks,

Related