How to Verify MCU Operating Frequency Without External Tools?

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I work with a PIC18F45K80 microcontroller and need to verify its operating frequency without using external equipment like a logic analyzer or oscilloscope. Buying new equipment is not an option for me right now.

To check the frequency, I used an LED toggle method. My MCU is running at the default internal frequency of 8MHz, so I toggled an LED every 5 seconds and verified the timing with a stopwatch. The LED blinked as expected, confirming the assumed frequency.

Code:
while (1) {
        LATAbits.LA5 ^= 1; // Toggle LED
        __delay_ms(5000);   // 5 seconds delay
    }
Now, my question is:

If I change my MCU's clock to 20MHz but keep the same toggle time as before, will the LED still toggle every 5 seconds? Or will the timing change due to the clock speed difference?
 

MrChips

Joined Oct 2, 2009
34,628
It depends on how the __delay_ms( ) function is implemented.
As a library function, it would accept the installed clock frequency as a parameter and hence yes, it would still give 5-second delay.
 

nsaspook

Joined Aug 27, 2009
16,250
I've done this before to check for actual processor clock speed without external timing equipment. Build a simple internal frequency counter function with the controller using the separate low speed internal clock as a timed gate for another timer tallying the FOSC clock as a input for that gate time duration. You don't need to display the FOSC variable, it can be used internally for speed checks or any other function. It's only needs to run once at boot, the two timers can then be reused for other needed functionality.
C:
/*
* File:   speed.h
* Author: root
*
* Created on April 30, 2021, 11:14 PM
*/

#ifndef SPEED_H
#define    SPEED_H

#ifdef    __cplusplus
extern "C" {
#endif

#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

#include "mcc_generated_files/tmr0.h"
#include "mcc_generated_files/tmr1.h"

    uint16_t get_fosc(void);
    void init_get_fosc(void);

#ifdef    __cplusplus
}
#endif

#endif    /* SPEED_H */



#include "speed.h"

/*
* return controller clock speed
*/
uint16_t get_fosc(void)
{
    while (!TMR1GIF); // wait unit the count gate is done.  init_get_fosc must be executed first
    return TMR1_ReadTimer(); // return the number of counts for the period set by timer0
}

/*
* configure timer 0 and 1 for processor timing duty
*/
void init_get_fosc(void)
{
    TMR1GIF = 0; // clear gate interrupt flag
    TMR1_WriteTimer(0); // zero counter date
    /*
     * trigger the count gate using the output of timer0 for one cycle, one shot
     */
    TMR1_StartSinglePulseAcquisition(); // Starts the single pulse acquisition in TMR1 gate operation.
}

// init in the main startup section
init_get_fosc();

// in main for the display text buffer

sprintf(buffer, "%u0000Hz FOSC", get_fosc());
https://forum.allaboutcircuits.com/...ify-the-speed-of-your-mcu.178567/post-1625337
A simple internal frequency counter for FOSC or other clock sources using the MCC for timer hardware configuration and software interfaces.

Clock timer0 in 8-bit mode using one of the internal LFINTOSC (31 kHz) or MFINTOSC (31.25 kHz) Oscillator Blocks and adjust the timer period for 100us (the LFINTOSC will be about 97us at best)
Clock timer1 in gate control mode from FOSC (or the desired frequency source) and enable the clock gate for toggle single-pulse using timer0 overflow output as the gate signal for a ~100us gate reference.
The internal High-Precision Internal Oscillator Block (HFINTOSC) is at the default of 4MHz.
PXL_20210501_082939677.jpg


The external clock chip for this Q43 board is 16MHz with a 4XPLL for 64MHz FOSC
PXL_20210501_083032726.jpg
The last 4 digits are faked for display but it's pretty close.
 
Last edited:

AlbertHall

Joined Jun 4, 2014
12,619
It depends on how the __delay_ms( ) function is implemented.
As a library function, it would accept the installed clock frequency as a parameter and hence yes, it would still give 5-second delay.
The delay function uses the clock value defined as _XTAL_FREQ to base the delay on. If this value is changed to match the actual clock frequency the delay will remain the same.
 
Top