News:

Let's find out together what makes a PIC Tick!

Main Menu

MCU effective clock frequency check

Started by Wimax, Aug 15, 2025, 05:07 PM

Previous topic - Next topic

Wimax

Hello Everybody  ;)

Perhaps this is a trivial question, but what method do you consider the simplest and, possibly, sufficiently accurate to understand whether the microcontroller you are using is actually  operating at the clock frequency you declare at the beginning of your programme (within known tolerances ), after setting a significant number of registers more or less protected by unlocking sequences, start-up switching modes and so on ?

trastikata

Toggling a LED with 1s period is the simplest way that I am using. If you meant that you need to check the actual clock offset then you'd need an oscilloscope.

top204

I always setup USART1 for a particular Baud rate, and transmit "Hello World" using HRsoutLn, in a loop, with a small delay between transmissions.

If it is received correctly, the frequency that the compiler has been told to compile for, is the speed the device is actually running at.

Wimax

Yes, my question is more of a survey. I often use the timed toggling of a pin, but actually the UART is demanding in terms of frequency tolerance, usually +- 2.5% per side, and in practice it narrows as the frequency increases.
Probably a test with a fairly high baud rate (say 115.2 kbaud or more) leaves no room for doubt.

trastikata

Quote from: Wimax on Aug 15, 2025, 07:20 PMusually +- 2.5% per side, and in practice it narrows as the frequency increases.

The relative accuracy remains the same 2.5% is always 2.5% :)

Using the internal oscillator or an external crystal, if you have main frequency correct i.e. 4 or 8 or 12 ... MHz verified by a blinking LED, then the minor frequency offset should be within the desired tolerances for 99% of the applications. For the remaining 1% you'd need an oscilloscope anyway.


Wimax

Yes, the theoretical relative error is independent of the baud rate, but the higher the frequency, the more precise the clock must be for that percentage to remain valid.

trastikata

#6
You are right.

kcsl

I always use the flashing LED indicator, especially when using a PIC I'm not familiar with. My thinking is that just by looking at the LED flash rate, you can get an immediate indication if the PIC is actually running, and depending on the actual flash rate compared with what you're expecting, gives you a clue if you've got configuration issues around the clock source, dividers, PLL etc.

Joe
There's no room for optimism in software or hardware engineering.