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, 3 months ago.
can anyone explain the code of FastPWM.cpp to me?
I don't quite understand this code. Could you please put comment beside the code? Thank you very much!
include "FastPWM.h"
FastPWM::FastPWM(PinName pin) : PWMObject(pin){ Set clock source LPC_SC->PCLKSEL0|=1<<12;
_duty=0; _period=0.02; if (pin==p26||pin==LED1) { PWMUnit=1; MR=&LPC_PWM1->MR1; } else if (pin==p25||pin==LED2){ PWMUnit=2; MR=&LPC_PWM1->MR2; } else if (pin==p24||pin==LED3){ PWMUnit=3; MR=&LPC_PWM1->MR3; } else if (pin==p23||pin==LED4){ PWMUnit=4; MR=&LPC_PWM1->MR4; } else if (pin==p22){ PWMUnit=5; MR=&LPC_PWM1->MR5; } else if (pin==p21){ PWMUnit=6; MR=&LPC_PWM1->MR6; } else error("No hardware PWM pin\n\r");
period(_period); }
void FastPWM::period(double seconds) { LPC_PWM1->MR0 = (unsigned int) (seconds * (double)F_CLK); LPC_PWM1->LER |= 1; _period = seconds; pulsewidth(_duty*_period); }
void FastPWM::period_ms(int ms) { period((double)ms/1000.0); }
void FastPWM::period_us(int us) { period((double)us/1000000.0); }
void FastPWM::period_us(double us) { period(us/1000000.0); }
void FastPWM::pulsewidth(double seconds) {
- MR=(unsigned int) (seconds * (double)F_CLK); LPC_PWM1->LER |= 1<<PWMUnit; _duty=seconds/_period; }
void FastPWM::pulsewidth_ms(int ms) { pulsewidth((double)ms/1000.0); }
void FastPWM::pulsewidth_us(int us) { pulsewidth((double)us/1000000.0); }
void FastPWM::pulsewidth_us(double us) { pulsewidth(us/1000000.0); }
void FastPWM::write(double duty) { _duty=duty; pulsewidth(duty*_period); }
double FastPWM::read( void ) { return _duty; }
FastPWM & FastPWM::operator= (double value) { write(value); return(*this); }
FastPWM::operator double() { return _duty; }
1 Answer
11 years, 3 months ago.
Use <<code>> and <</code>>
for readable code.
Or just link the code: http://mbed.org/users/Sissors/code/FastPWM/docs/3094d3806cfc/FastPWM_8cpp_source.html
There are no comments in the cpp file since the .h file has all functions documented, and with only 2-3 lines of code per function I didn't consider it really necesary to comment them. But what exactly is your question? In your other question you asked why it can be 48MHz compared to 0.5MHz of the standard library.
The standard library uses a minimum tick time of 1us. So one tick high, one low, which is the minimum period, is 2us -> 500kHz period. The hardware runs at 96MHz. FastPWM uses the maximum the hardware can offer, where one tick takes 1/96th of a microsecond. The result then is a maximum frequency of 96/2 = 48MHz.
The code itself simply calculates the required number of ticks at maximum operating speed required for a certain period, and sets that. And it does the same for the pulsewidth. Besides that there is some code for compatibility with mbed libraries, for example that if you chance the frequency that the duty cycle stays equal.
Thanks for your patience. I see what you mean, but I wanna know which lines of the code mean FastPWM's resolution is 1/96th of a microsecond. Beacause I can't tell the difference between PwnOut.h and FastPWM.h, so I guess something in the FastPWM.cpp changes the minimum of the tick time.
The other one is how to generate a 48MHz frequency? I make the code like this:
CLK.period_us(0.020834); But I know it is less than 48MHz a bit. How can I make it 48MHz even.
Thank you very much.
posted by 06 Aug 2013Thanks for your patience. I see what you mean, but I wanna know which lines of the code mean FastPWM's resolution is 1/96th of a microsecond. Beacause I can't tell the difference between PwnOut.h and FastPWM.h, so I guess something in the FastPWM.cpp changes the minimum of the tick time.
The other one is how to generate a 48MHz frequency? I make the code like this:
CLK.period_us(0.020834); But I know it is less than 48MHz a bit. How can I make it 48MHz even.
Thank you very much.
posted by 06 Aug 2013LPC_SC->PCLKSEL0|=1<<12;
This line sets the PWM's clock at the system clock, so 96MHz. Besides that there is no rounding and the usage of doubles instead of floats for more precission. The standard mbed library has a PWM clock of 1/4th the system clock. But more important, they use floats and round the values they set on microseconds.
But when using CLK.period_us(0.020834), or maybe easier, CLK.period_us(1/48) should work fine to make 48MHz clock (and of course also set the duty cycle at 50%). Otherwise something goes wrong with rounding.
posted by 07 Aug 2013