8 years, 9 months ago.

Clock Frequency

Hi, I first worked with microcontrollers, previously i worked with FPGA, then i have some stupid question: I have board "NUCLEO-F103RB", when I write the following code:

simple code

#include "mbed.h"

DigitalOut out1(PA_0);

int main() {
    while(1) 
    {
        out_A0 = 1;
        out_A0 = 0;
    }
}

I measure the output signal at A0 pin with an oscilloscope, and this signal have frequency 490kHz. But I thought that the frequency will be 4MHz, becouse I change the signal every clock cycle. How to write the code to know exactly what the output frequency?

1 Answer

8 years, 9 months ago.

First of all, the F103 has alot higher clock frequency, I guess something like 48MHz: A PLL increases the 8MHz crystal frequency.

Then, the compiler is a bit retarded, they all are. This makes it faster to instead use out_A0.write(1). Still, toggling it by software takes multiple clock cycles. And the while loop also has overhead.

I see FastIO (https://developer.mbed.org/users/Sissors/code/FastIO/) doesn't support your target (don't have it myself). That is a more optimal code for using GPIO. If you want to set a specific output frequency: Use PWM. Then a hardware block does it for you.

Thank You for your answer. Sorry, that mislead you, i don't said that i disable PLL. I think PWM is good idea, at what frequency range can I expect if enable PLL and use 72MHz?

posted by Anton Pimenov 03 Jul 2015

Regular mbed PWM can go up to 1MHz. If you import FastPWM lib you can go to 36MHz. It is just an integer division of the system clock.

posted by Erik - 04 Jul 2015