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.
11 years, 4 months ago.
What is F_CLK?
I searched a library which allows faster or higher resolution PWM out. I put part of the code below:
- include "mbed.h"
- ifndef FASTPWM_H
- define FASTPWM_H
- ifndef F_CLK
- define F_CLK 96000000
so, what is the F_CLK? why should make it 96000000? what is the original F_CLK?
If you need more information to answer my question, please have a look at "FastPWM". I'm a very new biginner, thanks for your help.
1 Answer
11 years, 4 months ago.
I assume you're talking about this version of the fastpwm library. I'm basing my answer off of it. http://mbed.org/users/jocis/code/HighPWM/
F_CLK is a macro that the library uses to store the clock speed of the mbed LPC1768 (96 MHz, or 96000000 Hz).
(Not sure what you mean by "original fclk" - did you mean the #ifndef? That's an inlude guard which prevents F_CLK from being defined twice in different files or by including a header twice: http://en.wikipedia.org/wiki/Include_guard)
I didn't look to closely, but it appears that the library uses it to convert things from seconds to mbed clock cycles. If you look below the #define F_CLK line, F_CLK is converted into 1/us, 1/ms, and 1/s.
HighPWM.h snippet
#define us_2_F_CLK (F_CLK/1000000) #define ms_2_F_CLK (F_CLK/1000) #define s_2_F_CLK (F_CLK)
And in HighPWM.cpp, the relevant F_CLKs are multiplied by a time to set the period.
HighPWM.cpp snippet
void HighPWM::period_us(unsigned int us) { period_ticks(us*us_2_F_CLK); }
Hope this helps,
Kevin
F_CLK is also used in the original library. But yeah it is just used to calculate how many clock periods go in a certain amount of time. Since in those libraries the PWM runs at the full speed of the mbed clock, it is needed to calculate the required clock ticks to get a certain period/pulse width.
posted by 08 Jul 2013Thanks for your reply. But actually I am using this library http://mbed.org/users/Sissors/code/FastPWM/ This allows faster frequency. I just don't know how this library work to make the frequency higher. And I don't know if I can put it this way. Because the mbed LPC1768 is 96MHz, the F_CLK will remain 96MHz, which will never change.
posted by 08 Jul 2013Thats what I meant with the original library in my reply. The mbed PWM library for imo not really required reasons has microsecond resolution for PWM. Since the LPC1768's clock runs at 96MHz, you can get 1/96th microsecond resolution from the hardware, FastPWM allows you to unlock those options.
If you don't do anything with the mbed's PLLs (which is the case for 99.9% of the use cases), you don't have to do anything with F_CLK, and it will indeed always be 96MHz. However it is possible to manually change the clock to another value. And since I had to define it somewhere anyway, I made it so you can easily change it to another value.
posted by 08 Jul 2013