mbed library for NZ32-SC151

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmOut.h Source File

PwmOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_PWMOUT_H
00017 #define MBED_PWMOUT_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_PWMOUT
00022 #include "pwmout_api.h"
00023 
00024 namespace mbed {
00025 
00026 /** A pulse-width modulation digital output
00027  *
00028  * Example
00029  * @code
00030  * // Fade a led on.
00031  * #include "mbed.h"
00032  *
00033  * PwmOut led(LED1);
00034  *
00035  * int main() {
00036  *     while(1) {
00037  *         led = led + 0.01;
00038  *         wait(0.2);
00039  *         if(led == 1.0) {
00040  *             led = 0;
00041  *         }
00042  *     }
00043  * }
00044  * @endcode
00045  *
00046  * @note
00047  *  On the LPC1768 and LPC2368, the PWMs all share the same
00048  *  period - if you change the period for one, you change it for all.
00049  *  Although routines that change the period maintain the duty cycle
00050  *  for its PWM, all other PWMs will require their duty cycle to be
00051  *  refreshed.
00052  */
00053 class PwmOut {
00054 
00055 public:
00056 
00057     /** Create a PwmOut connected to the specified pin
00058      *
00059      *  @param pin PwmOut pin to connect to
00060      */
00061     PwmOut(PinName pin) {
00062         pwmout_init(&_pwm, pin);
00063     }
00064 
00065     /** Set the ouput duty-cycle, specified as a percentage (float)
00066      *
00067      *  @param value A floating-point value representing the output duty-cycle,
00068      *    specified as a percentage. The value should lie between
00069      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00070      *    Values outside this range will be saturated to 0.0f or 1.0f.
00071      */
00072     void write(float value) {
00073         pwmout_write(&_pwm, value);
00074     }
00075 
00076     /** Return the current output duty-cycle setting, measured as a percentage (float)
00077      *
00078      *  @returns
00079      *    A floating-point value representing the current duty-cycle being output on the pin,
00080      *    measured as a percentage. The returned value will lie between
00081      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00082      *
00083      *  @note
00084      *  This value may not match exactly the value set by a previous <write>.
00085      */
00086     float read() {
00087         return pwmout_read(&_pwm);
00088     }
00089 
00090     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
00091      *
00092      *  @note
00093      *   The resolution is currently in microseconds; periods smaller than this
00094      *   will be set to zero.
00095      */
00096     void period(float seconds) {
00097         pwmout_period(&_pwm, seconds);
00098     }
00099 
00100     /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
00101      */
00102     void period_ms(int ms) {
00103         pwmout_period_ms(&_pwm, ms);
00104     }
00105 
00106     /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
00107      */
00108     void period_us(int us) {
00109         pwmout_period_us(&_pwm, us);
00110     }
00111 
00112     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
00113      */
00114     void pulsewidth(float seconds) {
00115         pwmout_pulsewidth(&_pwm, seconds);
00116     }
00117 
00118     /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
00119      */
00120     void pulsewidth_ms(int ms) {
00121         pwmout_pulsewidth_ms(&_pwm, ms);
00122     }
00123 
00124     /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
00125      */
00126     void pulsewidth_us(int us) {
00127         pwmout_pulsewidth_us(&_pwm, us);
00128     }
00129 
00130 #ifdef MBED_OPERATORS
00131     /** A operator shorthand for write()
00132      */
00133     PwmOut& operator= (float value) {
00134         write(value);
00135         return *this;
00136     }
00137 
00138     PwmOut& operator= (PwmOut& rhs) {
00139         write(rhs.read());
00140         return *this;
00141     }
00142 
00143     /** An operator shorthand for read()
00144      */
00145     operator float() {
00146         return read();
00147     }
00148 #endif
00149 
00150 protected:
00151     pwmout_t _pwm;
00152 };
00153 
00154 } // namespace mbed
00155 
00156 #endif
00157 
00158 #endif