Kojo / SoftwarePWM

Dependents:   Seeed_Motor_Shield_HelloWorld Official_MedusaIcon Seeed_Motor_Shield adrobo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SoftwarePWM.h Source File

SoftwarePWM.h

00001 /* mbed Software PWM Library without using PWM pins
00002  * Copyright (c) 2012 Takashi Kojo
00003  * based and modified, Copyright (c) 2010 Jasper Denkers
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022 */
00023 
00024 #ifndef MBED_SOFTWAREPWM_H
00025 #define MBED_SOFTWAREPWM_H
00026 
00027 #include "mbed.h"
00028 
00029 /** Class to control a servo on any pin, without using pwm
00030  *
00031  * Example:
00032  * @code
00033  * // Keep sweeping servo from left to right
00034  * #include "mbed.h"
00035  * #include "Servo.h"
00036  *
00037  * Servo Servo1(p20);
00038  *
00039  * Servo1.Enable(1500,20000);
00040  *
00041  * while(1) {
00042  *     for (int pos = 1000; pos < 2000; pos += 25) {
00043  *         Servo1.SetPosition(pos);
00044  *         wait_ms(20);
00045  *     }
00046  *     for (int pos = 2000; pos > 1000; pos -= 25) {
00047  *         Servo1.SetPosition(pos);
00048  *         wait_ms(20);
00049  *     }
00050  * }
00051  * @endcode
00052  */
00053 
00054 
00055 class SoftwarePWM {
00056 
00057 public:
00058     /** Create a new SoftwarePWM object on any mbed pin
00059       *
00060       * @param Pin Pin on mbed to connect PWM device to
00061      */
00062     SoftwarePWM(PinName Pin);
00063 
00064     /** Change the position of the PWM. Position in us
00065       *
00066       * @param NewPos The new value of the PWM position (us)
00067      */
00068     void SetPosition(int NewPos);
00069 
00070     /** Enable the PWM. Without enabling the PWM won't be running. Startposition and period both in us.
00071       *
00072       * @param StartPos The position of the PWM to start (us)
00073       * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us)
00074      */
00075     void Enable(int StartPos, int Period);
00076 
00077     /** Disable the PWM. After disabling the PWM won't get any signal anymore
00078       *
00079       */
00080     void Disable();
00081 
00082 private:
00083     void StartPulse();
00084     void EndPulse();
00085 
00086     int Position;
00087     DigitalOut SoftwarePWMPin;
00088     Ticker Pulse;
00089     Timeout PulseStop;
00090 };
00091 
00092 
00093 extern SoftwarePWM Intensity[] ;
00094 #define INTENSITY_FACTOR 10 
00095 
00096 #endif