How do you verify the speed of your MCU

Thread Starter

skyr6546

Joined Mar 22, 2019
73
If your clock settings are wrong it makes life very difficult! I have a general question, can the speed of the PIC microcontroller be verified that it is working at the right speed?
 

MrChips

Joined Oct 2, 2009
24,237
I toggle a GPIO port and measure the frequency on an oscilloscope.
Then I look up how many CPU cycles are required for each instruction in the sw loop.
 

AlbertHall

Joined Jun 4, 2014
11,506
Create what should be a half second delay at the desired clock speed and connect an LED to an output pin. Put the delay and a toggle LED line in a loop and you can see whther you have the right speed.
 

click_here

Joined Sep 22, 2020
407
I also use the toggle pin method

Put an LED (and resistor) on the output, Set high, delay 500ms, low, delay 500ms, repeat

I usually then just compare it to the seconds hand on my clock, nothing fancy
 

Young2

Joined Dec 7, 2020
90
MCU internal registers can read the main frequency of the clock frequency, write the code to read it out, and you can print it out through the log, or show it on the display if you have one.
 

peterdeco

Joined Oct 8, 2019
393
One time I set the internal osc to 8MHZ. Something wasn't right. As in post #6, I made an output high for 1 second. It went high for 2 seconds. I made a mistake setting the osc and it defaulted to 4MHZ.
 

nsaspook

Joined Aug 27, 2009
8,688
I don't know of a MCU that can read its clock frequency - what would it use as a reference clock?
If you have a MCU with a fixed 31 kHz (LFINTOSC) I'm sure there is a way to measure the period of the current FOSC to a few percentage points using a timer with the LFINTOSC as the reference.
 

AlbertHall

Joined Jun 4, 2014
11,506
Except that an external crystal is going to more accurate then LFINTOSC, so I suppose you could do it the other way round and measure LFINTOSC against the crystal with some software, but it isn't built in to the micro.
 

nsaspook

Joined Aug 27, 2009
8,688
Except that an external crystal is going to more accurate then LFINTOSC, so I suppose you could do it the other way round and measure LFINTOSC against the crystal with some software, but it isn't built in to the micro.
It's not going to be accurate but a LFINTOSC timer reference to a instruction speed timing loop should be able to tell if the MCU speed is half (PLL/clock setting error) what it should be easily.
 

click_here

Joined Sep 22, 2020
407
>One time I set the internal osc to 8MHZ. Something wasn't right. As in post #6, I made an output high for 1 second. It went high for 2 seconds. I made a mistake setting the osc and it defaulted to 4MHZ.

I also find that if my osc setup is wrong, it's wrong by a factor of 2^n - Toggling a pin with a delay picks that up really quickly and requires minimal effort.

If you need a more accurate reading there is an option on PICs to set a pin to output the clock and use an Oscilloscope or frequency counter, but I've never needed that sort of accuracy. Lots of effort for little gain in my case.
 

nsaspook

Joined Aug 27, 2009
8,688
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.
https://microchipdeveloper.com/8bit:timer1
Timer1 Gate
The Timer1 Gate feature allows the PIC® MCU device to easily time external events using T1G pin as the input or analog events using an internal Comparator. This can be useful for timing the high pulse time of a waveform. The Timer1 Gate can control when the Timer1 increments. It is essentially an on/off switch for the Timer1 counter. There are several individual bits in the Timer1 Gate Control register (T1GCON register) that control the operation of the Timer1 Gate. Each setting is covered below.
Clear and use the timer1 interrupt flag (no timer 0 and 1 interrupt are not enabled) as the gate complete event. Enable the gate signal. When the timer1 interrupt flag is set (polled) the timer1 16-bit counter will have the clock frequency in 100us counts.

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());
The external clock chip for this Q43 board is 16MHz with a 4XPLL for 64MHz FOSC. The internal High-Precision Internal Oscillator Block (HFINTOSC) is at the default of 4MHz.

PXL_20210501_082939677.jpgPXL_20210501_082946290.jpgPXL_20210501_083027493.jpgPXL_20210501_083032726.jpgPXL_20210501_083049688.jpg
 
Last edited:
Top