Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:818904abef3d, committed 2012-11-14
- Comitter:
- xiaxia686
- Date:
- Wed Nov 14 18:44:03 2012 +0000
- Parent:
- 0:5ebe36076172
- Commit message:
- Working PWM;
Changed in this revision
--- a/PwmIn/PwmIn.cpp Wed Nov 14 16:53:28 2012 +0000
+++ b/PwmIn/PwmIn.cpp Wed Nov 14 18:44:03 2012 +0000
@@ -1,52 +1,76 @@
-/* mbed PwmIn Library
- * Copyright (c) 2008-2010, sford
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "PwmIn.h"
-
-PwmIn::PwmIn(PinName p) : _p(p) {
- _p.rise(this, &PwmIn::rise);
- _p.fall(this, &PwmIn::fall);
- _period = 0.0;
- _pulsewidth = 0.0;
- _t.start();
-}
-
-float PwmIn::period() {
- return _period;
-}
-
-float PwmIn::pulsewidth() {
- return _pulsewidth;
-}
-
-float PwmIn::dutycycle() {
- return _pulsewidth / _period;
-}
-
-void PwmIn::rise() {
- _period = _t.read();
- _t.reset();
-}
-
-void PwmIn::fall() {
- _pulsewidth = _t.read();
-}
+/* mbed PwmIn Library
+ * Copyright (c) 2008-2010, sford
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "PwmIn.h"
+#include "globals.h"
+
+PwmIn::PwmIn(PinName p) : _p(p) {
+ #ifdef PWM_INVERT
+ _p.rise(this, &PwmIn::fall);
+ _p.fall(this, &PwmIn::rise);
+ #else
+ _p.rise(this, &PwmIn::rise);
+ _p.fall(this, &PwmIn::fall);
+ #endif
+
+ _period = 0.0;
+ _pulsewidth = 0.0;
+ _t.start();
+
+ //init callabck function
+ callbackfunc = NULL;
+ callbackobj = NULL;
+ mcallbackfunc = NULL;
+
+}
+
+float PwmIn::period() {
+ return _period;
+}
+
+float PwmIn::pulsewidth() {
+ return _pulsewidth;
+}
+
+float PwmIn::dutycycle() {
+ return _pulsewidth / _period;
+}
+
+void PwmIn::rise() {
+ _period = _t.read();
+ _t.reset();
+
+
+
+
+}
+
+void PwmIn::fall() {
+ _pulsewidth = _t.read();
+
+ if (callbackfunc)
+ (*callbackfunc)(_pulsewidth / _period);
+
+ if (callbackobj && mcallbackfunc)
+ (callbackobj->*mcallbackfunc)(_pulsewidth / _period);
+
+}
--- a/PwmIn/PwmIn.h Wed Nov 14 16:53:28 2012 +0000
+++ b/PwmIn/PwmIn.h Wed Nov 14 18:44:03 2012 +0000
@@ -1,70 +1,78 @@
-/* mbed PwmIn Library
- * Copyright (c) 2008-2010, sford
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef MBED_PWMIN_H
-#define MBED_PWMIN_H
-
-#include "mbed.h"
-
-/** PwmIn class to read PWM inputs
- *
- * Uses InterruptIn to measure the changes on the input
- * and record the time they occur
- *
- * @note uses InterruptIn, so not available on p19/p20
- */
-class PwmIn {
-public:
- /** Create a PwmIn
- *
- * @param p The pwm input pin (must support InterruptIn)
- */
- PwmIn(PinName p);
-
- /** Read the current period
- *
- * @returns the period in seconds
- */
- float period();
-
- /** Read the current pulsewidth
- *
- * @returns the pulsewidth in seconds
- */
- float pulsewidth();
-
- /** Read the current dutycycle
- *
- * @returns the dutycycle as a percentage, represented between 0.0-1.0
- */
- float dutycycle();
-
-protected:
- void rise();
- void fall();
-
- InterruptIn _p;
- Timer _t;
- float _pulsewidth, _period;
-};
-
-#endif
+/* mbed PwmIn Library
+ * Copyright (c) 2008-2010, sford
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_PWMIN_H
+#define MBED_PWMIN_H
+
+#include "mbed.h"
+
+/** PwmIn class to read PWM inputs
+ *
+ * Uses InterruptIn to measure the changes on the input
+ * and record the time they occur
+ *
+ * @note uses InterruptIn, so not available on p19/p20
+ */
+class DummyCT;
+
+class PwmIn {
+public:
+ /** Create a PwmIn
+ *
+ * @param p The pwm input pin (must support InterruptIn)
+ */
+ PwmIn(PinName p);
+
+ /** Read the current period
+ *
+ * @returns the period in seconds
+ */
+ float period();
+
+ /** Read the current pulsewidth
+ *
+ * @returns the pulsewidth in seconds
+ */
+ float pulsewidth();
+
+ /** Read the current dutycycle
+ *
+ * @returns the dutycycle as a percentage, represented between 0.0-1.0
+ */
+ float dutycycle();
+
+ /** A assigns a callback function when a new reading is available **/
+ void (*callbackfunc)(float dutycycle);
+ DummyCT* callbackobj;
+ void (DummyCT::*mcallbackfunc)(float dutycycle);
+
+
+protected:
+ void rise();
+ void fall();
+
+ InterruptIn _p;
+ Timer _t;
+ float _pulsewidth, _period;
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globals.h Wed Nov 14 18:44:03 2012 +0000 @@ -0,0 +1,9 @@ +#ifndef GLOBALS_H +#define GLOBALS_H + +#include "mbed.h" +#define PI 3.14159265 + +#define PWM_INVERT + +#endif \ No newline at end of file
--- a/main.cpp Wed Nov 14 16:53:28 2012 +0000
+++ b/main.cpp Wed Nov 14 18:44:03 2012 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "PwmIn.h"
+#include "globals.h"
+#include "rtos.h"
+
+#define PWM_INVERT
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+Serial pc(USBTX, USBRX); // tx, rx
+
+PwmIn My_PWM(p30);
+float my_dutycycle;
+Semaphore serial_sema(1);
+
+void PWM_Callback(float dutycycle) {
+ led1 = !led1;
+ my_dutycycle = dutycycle;
+ serial_sema.release();
+
+}
+
+
+void serial_thread(void const *argument) {
+ while (true) {
+ serial_sema.wait();
+ printf("dutycycle: %0.4f \n\r", my_dutycycle);
+ }
+}
+
+int main() {
+ pc.baud(19200);
+ pc.printf("Hello from mbed\n");
+
+ My_PWM.callbackfunc = PWM_Callback;
+
+ Thread thread(serial_thread);
+
+ while (true) {
+ Thread::wait(osWaitForever);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Nov 14 18:44:03 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Nov 14 18:44:03 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06 \ No newline at end of file