Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 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
9 years, 8 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.