softwarePWM by ticker, timer
Dependents: Seeed_Motor_Shield_HelloWorld Official_MedusaIcon Seeed_Motor_Shield adrobo
SoftwarePWM.h@0:1e86036581bd, 2012-04-05 (annotated)
- Committer:
- takashikojo
- Date:
- Thu Apr 05 07:09:39 2012 +0000
- Revision:
- 0:1e86036581bd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:1e86036581bd | 1 | /* mbed Software PWM Library without using PWM pins |
takashikojo | 0:1e86036581bd | 2 | * Copyright (c) 2012 Takashi Kojo |
takashikojo | 0:1e86036581bd | 3 | * based and modified, Copyright (c) 2010 Jasper Denkers |
takashikojo | 0:1e86036581bd | 4 | * |
takashikojo | 0:1e86036581bd | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
takashikojo | 0:1e86036581bd | 6 | * of this software and associated documentation files (the "Software"), to deal |
takashikojo | 0:1e86036581bd | 7 | * in the Software without restriction, including without limitation the rights |
takashikojo | 0:1e86036581bd | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
takashikojo | 0:1e86036581bd | 9 | * copies of the Software, and to permit persons to whom the Software is |
takashikojo | 0:1e86036581bd | 10 | * furnished to do so, subject to the following conditions: |
takashikojo | 0:1e86036581bd | 11 | * |
takashikojo | 0:1e86036581bd | 12 | * The above copyright notice and this permission notice shall be included in |
takashikojo | 0:1e86036581bd | 13 | * all copies or substantial portions of the Software. |
takashikojo | 0:1e86036581bd | 14 | * |
takashikojo | 0:1e86036581bd | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
takashikojo | 0:1e86036581bd | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
takashikojo | 0:1e86036581bd | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
takashikojo | 0:1e86036581bd | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
takashikojo | 0:1e86036581bd | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
takashikojo | 0:1e86036581bd | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
takashikojo | 0:1e86036581bd | 21 | * THE SOFTWARE. |
takashikojo | 0:1e86036581bd | 22 | */ |
takashikojo | 0:1e86036581bd | 23 | |
takashikojo | 0:1e86036581bd | 24 | #ifndef MBED_SOFTWAREPWM_H |
takashikojo | 0:1e86036581bd | 25 | #define MBED_SOFTWAREPWM_H |
takashikojo | 0:1e86036581bd | 26 | |
takashikojo | 0:1e86036581bd | 27 | #include "mbed.h" |
takashikojo | 0:1e86036581bd | 28 | |
takashikojo | 0:1e86036581bd | 29 | /** Class to control a servo on any pin, without using pwm |
takashikojo | 0:1e86036581bd | 30 | * |
takashikojo | 0:1e86036581bd | 31 | * Example: |
takashikojo | 0:1e86036581bd | 32 | * @code |
takashikojo | 0:1e86036581bd | 33 | * // Keep sweeping servo from left to right |
takashikojo | 0:1e86036581bd | 34 | * #include "mbed.h" |
takashikojo | 0:1e86036581bd | 35 | * #include "Servo.h" |
takashikojo | 0:1e86036581bd | 36 | * |
takashikojo | 0:1e86036581bd | 37 | * Servo Servo1(p20); |
takashikojo | 0:1e86036581bd | 38 | * |
takashikojo | 0:1e86036581bd | 39 | * Servo1.Enable(1500,20000); |
takashikojo | 0:1e86036581bd | 40 | * |
takashikojo | 0:1e86036581bd | 41 | * while(1) { |
takashikojo | 0:1e86036581bd | 42 | * for (int pos = 1000; pos < 2000; pos += 25) { |
takashikojo | 0:1e86036581bd | 43 | * Servo1.SetPosition(pos); |
takashikojo | 0:1e86036581bd | 44 | * wait_ms(20); |
takashikojo | 0:1e86036581bd | 45 | * } |
takashikojo | 0:1e86036581bd | 46 | * for (int pos = 2000; pos > 1000; pos -= 25) { |
takashikojo | 0:1e86036581bd | 47 | * Servo1.SetPosition(pos); |
takashikojo | 0:1e86036581bd | 48 | * wait_ms(20); |
takashikojo | 0:1e86036581bd | 49 | * } |
takashikojo | 0:1e86036581bd | 50 | * } |
takashikojo | 0:1e86036581bd | 51 | * @endcode |
takashikojo | 0:1e86036581bd | 52 | */ |
takashikojo | 0:1e86036581bd | 53 | |
takashikojo | 0:1e86036581bd | 54 | |
takashikojo | 0:1e86036581bd | 55 | class SoftwarePWM { |
takashikojo | 0:1e86036581bd | 56 | |
takashikojo | 0:1e86036581bd | 57 | public: |
takashikojo | 0:1e86036581bd | 58 | /** Create a new SoftwarePWM object on any mbed pin |
takashikojo | 0:1e86036581bd | 59 | * |
takashikojo | 0:1e86036581bd | 60 | * @param Pin Pin on mbed to connect PWM device to |
takashikojo | 0:1e86036581bd | 61 | */ |
takashikojo | 0:1e86036581bd | 62 | SoftwarePWM(PinName Pin); |
takashikojo | 0:1e86036581bd | 63 | |
takashikojo | 0:1e86036581bd | 64 | /** Change the position of the PWM. Position in us |
takashikojo | 0:1e86036581bd | 65 | * |
takashikojo | 0:1e86036581bd | 66 | * @param NewPos The new value of the PWM position (us) |
takashikojo | 0:1e86036581bd | 67 | */ |
takashikojo | 0:1e86036581bd | 68 | void SetPosition(int NewPos); |
takashikojo | 0:1e86036581bd | 69 | |
takashikojo | 0:1e86036581bd | 70 | /** Enable the PWM. Without enabling the PWM won't be running. Startposition and period both in us. |
takashikojo | 0:1e86036581bd | 71 | * |
takashikojo | 0:1e86036581bd | 72 | * @param StartPos The position of the PWM to start (us) |
takashikojo | 0:1e86036581bd | 73 | * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us) |
takashikojo | 0:1e86036581bd | 74 | */ |
takashikojo | 0:1e86036581bd | 75 | void Enable(int StartPos, int Period); |
takashikojo | 0:1e86036581bd | 76 | |
takashikojo | 0:1e86036581bd | 77 | /** Disable the PWM. After disabling the PWM won't get any signal anymore |
takashikojo | 0:1e86036581bd | 78 | * |
takashikojo | 0:1e86036581bd | 79 | */ |
takashikojo | 0:1e86036581bd | 80 | void Disable(); |
takashikojo | 0:1e86036581bd | 81 | |
takashikojo | 0:1e86036581bd | 82 | private: |
takashikojo | 0:1e86036581bd | 83 | void StartPulse(); |
takashikojo | 0:1e86036581bd | 84 | void EndPulse(); |
takashikojo | 0:1e86036581bd | 85 | |
takashikojo | 0:1e86036581bd | 86 | int Position; |
takashikojo | 0:1e86036581bd | 87 | DigitalOut SoftwarePWMPin; |
takashikojo | 0:1e86036581bd | 88 | Ticker Pulse; |
takashikojo | 0:1e86036581bd | 89 | Timeout PulseStop; |
takashikojo | 0:1e86036581bd | 90 | }; |
takashikojo | 0:1e86036581bd | 91 | |
takashikojo | 0:1e86036581bd | 92 | |
takashikojo | 0:1e86036581bd | 93 | extern SoftwarePWM Intensity[] ; |
takashikojo | 0:1e86036581bd | 94 | #define INTENSITY_FACTOR 10 |
takashikojo | 0:1e86036581bd | 95 | |
takashikojo | 0:1e86036581bd | 96 | #endif |