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

Frequency detector possible?

Hi. Can anyone think of a way to use the nRF52832 (probably Timer) to detect approximately 125KHz-150KHz variable square wave down to about a 1Hz resolution? I'd like to detect frequency changes at about 100 times a second though. Ideally I'd like to do this without any external hardware (like a heterodyne mixer). Thoughts? Thanks!

(FINAL answer at very bottom. The answer is YES, the nRF52832 can be used even with a SoftDevice to accurately detect a few hundred KHz signal down to a few Hz resolution)

  • Hi Kevin,

    Happy to know that your code works :)  With softdevice, sd_clock_hfclk_request() call should help.

    Also, is there a way to tell the SoftDevice NOT to panic when I am single stepping through code?

    It's annoying, but it seems there's no way to single-step while connection is active.

  • Thanks Dmitry. Any thoughts about my first question about the SoftDevice seemingly changing internal oscillators or something like that...?

  • Please ignore the above. I missed your first reply... Investigating now...

  • You need J-Link Monitor which allows you to single step debug while the SoftDevice runs happily in the background without dropping the BLE connection. Try this:

    #if defined(FEATURE_JLINK_MONITOR)
       // This allows all interrupts higher than _PRIO_SD_LOW (ie 4, BLE SD stuff) to continue execution even during a break
       // note Two J-Link commands are required in command file:
       // SetMonModeDebug = 1
       // SetMonModeVTableAddr = ADDR
       //  where ADDR is the application's vector table located in FLASH, ie the start address for the
       // application, probably either 0x1B000 or 0x1C000 or 0x26000 depending on SoftDevice
       // See https://github.com/NordicPlayground/j-link-monitoring-mode-debugging
       NVIC_SetPriority(DebugMonitor_IRQn, _PRIO_SD_LOW);
    #endif

    Enable FEATURE_JLINK_MONITOR in the project preprocessor (so it's easy to turn feature on and off). Add the commands noted above in Debug->J-Link->Command Options for SES; other IDEs are similar, I use both SES and IAR.

    Get code from monitor-mode-debugging

  • Thank you again Dmitry! Your idea worked. Again. Slight smile  This code is needed to maintain accurate frequency results when using a SoftDevice. If not using a SoftDevice, use other means to enable the external 32MHz oscillator.

    static void ensureExternalClkRunning(void)
    {
    	uint32_t isRunning = 0;
    	int cnt = 0;
    	sd_clock_hfclk_request();
    	do {
    		sd_clock_hfclk_is_running(&isRunning);
    		cnt++;
    		if (cnt > 1000000) {} // Handle non transition. Cnt is usually on the order of 80-ish
    	} while (isRunning == 0);
    }
    

Related