My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.

Dependents:   FastPWM-DISCO-test

Fork of FastPWM by Erik -

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Thu Jul 12 11:20:09 2012 +0000
Child:
1:1aed61747ed6
Commit message:
v1.0, seems completely functional

Changed in this revision

FastPWM.cpp Show annotated file Show diff for this revision Revisions of this file
FastPWM.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FastPWM.cpp	Thu Jul 12 11:20:09 2012 +0000
@@ -0,0 +1,75 @@
+#include "FastPWM.h"
+
+FastPWM::FastPWM(PinName pin) : PWMObject(pin){
+    _duty=0;
+    _period=0.02;
+    if (pin==p26||pin==LED1)
+        MR=&LPC_PWM1->MR1;
+    else if (pin==p25||pin==LED2)
+        MR=&LPC_PWM1->MR2;
+    else if (pin==p24||pin==LED3)
+        MR=&LPC_PWM1->MR3;
+    else if (pin==p23||pin==LED4)
+        MR=&LPC_PWM1->MR4;
+    else if (pin==p22)
+        MR=&LPC_PWM1->MR5;
+    else if (pin==p21)
+        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);
+    pulsewidth(_duty*seconds);
+    _period = seconds;
+}
+
+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);
+}
+
+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;
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FastPWM.h	Thu Jul 12 11:20:09 2012 +0000
@@ -0,0 +1,105 @@
+#include "mbed.h"
+
+
+#ifndef FASTPWM_H
+#define FASTPWM_H
+
+#ifndef F_CLK
+#define F_CLK   96000000
+#endif
+
+/** Library that allows faster and/or higher resolution PWM output
+  *
+  * Library can directly replace standard mbed PWM library. Only limitation is that the maximum PWM period is four times shorter
+  * The maximum achievable period is roughly 40 seconds, I dont think that should be a problem.
+  * Do take into account all PWM objects will run four times faster than default.
+  *
+  * Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed,
+  * but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision.
+  *
+  * In your program you can define F_CLK if you use a different clock frequency than the default one. 
+  *
+  * Only works on LPC1768 for now. If you want support for the other one, send a PM and I will have a look, but I cannot even compile for it.
+  */
+class FastPWM {
+public:
+    /**
+    * Create a FastPWM object connected to the specified pin
+    *
+    * @param pin - PWM pin to connect to
+    */
+    FastPWM(PinName pin);
+    
+    /**
+    * Set the PWM period, specified in seconds (double), keeping the duty cycle the same.
+    */
+    void period(double seconds);
+    
+    /**
+    * Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
+    */
+    void period_ms(int ms);
+    
+    /**
+    * Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
+    */
+    void period_us(int us);
+    
+    /**
+    * Set the PWM period, specified in micro-seconds (double), keeping the duty cycle the same.
+    */
+    void period_us(double us);
+    
+    /**
+    * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same.
+    */
+    void pulsewidth(double seconds);
+    
+    /**
+    * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
+    */
+    void pulsewidth_ms(int ms);
+    
+    /**
+    * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
+    */
+    void pulsewidth_us(int us);
+    
+    /**
+    * Set the PWM pulsewidth, specified in micro-seconds (double), keeping the period the same.
+    */
+    void pulsewidth_us(double us);
+    
+    /**
+    * Set the ouput duty-cycle, specified as a percentage (double)
+    *
+    * @param duty - A double value representing the output duty-cycle, specified as a percentage.  The value should lie between 0.0 (representing on 0%) and 1.0 (representing on 100%).
+    */
+    void write(double duty);
+    
+    /**
+    * Return the ouput duty-cycle, specified as a percentage (double)
+    *
+    * @param return - A double value representing the output duty-cycle, specified as a percentage.
+    */
+    double read( void );
+    
+    /**
+    * An operator shorthand for write()
+    */
+    FastPWM& operator= (double value);
+    
+    /**
+    * An operator shorthand for read()
+    */
+    operator double();
+    
+private:
+    PwmOut PWMObject;
+    double _duty;
+    double _period;
+    
+    __IO uint32_t *MR;
+
+};
+#endif
\ No newline at end of file