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.
PwmOut.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_PWMOUT_H 00023 #define MBED_PWMOUT_H 00024 00025 #include "platform.h" 00026 00027 #if DEVICE_PWMOUT 00028 #include "pwmout_api.h" 00029 00030 namespace mbed { 00031 00032 /** A pulse-width modulation digital output 00033 * 00034 * Example 00035 * @code 00036 * // Fade a led on. 00037 * #include "mbed.h" 00038 * 00039 * PwmOut led(LED1); 00040 * 00041 * int main() { 00042 * while(1) { 00043 * led = led + 0.01; 00044 * wait(0.2); 00045 * if(led == 1.0) { 00046 * led = 0; 00047 * } 00048 * } 00049 * } 00050 * @endcode 00051 * 00052 * @note 00053 * On the LPC1768 and LPC2368, the PWMs all share the same 00054 * period - if you change the period for one, you change it for all. 00055 * Although routines that change the period maintain the duty cycle 00056 * for its PWM, all other PWMs will require their duty cycle to be 00057 * refreshed. 00058 */ 00059 class PwmOut { 00060 00061 public: 00062 00063 /** Create a PwmOut connected to the specified pin 00064 * 00065 * @param pin PwmOut pin to connect to 00066 */ 00067 PwmOut(PinName pin) { 00068 pwmout_init(&_pwm, pin); 00069 } 00070 00071 /** Set the ouput duty-cycle, specified as a percentage (float) 00072 * 00073 * @param value A floating-point value representing the output duty-cycle, 00074 * specified as a percentage. The value should lie between 00075 * 0.0f (representing on 0%) and 1.0f (representing on 100%). 00076 * Values outside this range will be saturated to 0.0f or 1.0f. 00077 */ 00078 void write(float value) { 00079 pwmout_write(&_pwm, value); 00080 } 00081 00082 /** Return the current output duty-cycle setting, measured as a percentage (float) 00083 * 00084 * @returns 00085 * A floating-point value representing the current duty-cycle being output on the pin, 00086 * measured as a percentage. The returned value will lie between 00087 * 0.0f (representing on 0%) and 1.0f (representing on 100%). 00088 * 00089 * @note 00090 * This value may not match exactly the value set by a previous <write>. 00091 */ 00092 float read() { 00093 return pwmout_read(&_pwm); 00094 } 00095 00096 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. 00097 * 00098 * @note 00099 * The resolution is currently in microseconds; periods smaller than this 00100 * will be set to zero. 00101 */ 00102 void period(float seconds) { 00103 pwmout_period(&_pwm, seconds); 00104 } 00105 00106 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. 00107 */ 00108 void period_ms(int ms) { 00109 pwmout_period_ms(&_pwm, ms); 00110 } 00111 00112 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. 00113 */ 00114 void period_us(int us) { 00115 pwmout_period_us(&_pwm, us); 00116 } 00117 00118 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. 00119 */ 00120 void pulsewidth(float seconds) { 00121 pwmout_pulsewidth(&_pwm, seconds); 00122 } 00123 00124 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. 00125 */ 00126 void pulsewidth_ms(int ms) { 00127 pwmout_pulsewidth_ms(&_pwm, ms); 00128 } 00129 00130 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. 00131 */ 00132 void pulsewidth_us(int us) { 00133 pwmout_pulsewidth_us(&_pwm, us); 00134 } 00135 00136 #ifdef MBED_OPERATORS 00137 /** A operator shorthand for write() 00138 */ 00139 PwmOut& operator= (float value) { 00140 write(value); 00141 return *this; 00142 } 00143 00144 PwmOut& operator= (PwmOut& rhs) { 00145 write(rhs.read()); 00146 return *this; 00147 } 00148 00149 /** An operator shorthand for read() 00150 */ 00151 operator float() { 00152 return read(); 00153 } 00154 #endif 00155 00156 protected: 00157 pwmout_t _pwm; 00158 }; 00159 00160 } // namespace mbed 00161 00162 #endif 00163 00164 #endif 00165
Generated on Wed Jul 13 2022 02:59:12 by
