This fork captures the mbed lib v125 for ease of integration into older projects.
Fork of mbed-dev by
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 #include "critical.h" 00024 00025 namespace mbed { 00026 00027 /** A pulse-width modulation digital output 00028 * 00029 * @Note Synchronization level: Interrupt safe 00030 * 00031 * Example 00032 * @code 00033 * // Fade a led on. 00034 * #include "mbed.h" 00035 * 00036 * PwmOut led(LED1); 00037 * 00038 * int main() { 00039 * while(1) { 00040 * led = led + 0.01; 00041 * wait(0.2); 00042 * if(led == 1.0) { 00043 * led = 0; 00044 * } 00045 * } 00046 * } 00047 * @endcode 00048 * 00049 * @note 00050 * On the LPC1768 and LPC2368, the PWMs all share the same 00051 * period - if you change the period for one, you change it for all. 00052 * Although routines that change the period maintain the duty cycle 00053 * for its PWM, all other PWMs will require their duty cycle to be 00054 * refreshed. 00055 */ 00056 class PwmOut { 00057 00058 public: 00059 00060 /** Create a PwmOut connected to the specified pin 00061 * 00062 * @param pin PwmOut pin to connect to 00063 */ 00064 PwmOut(PinName pin) { 00065 core_util_critical_section_enter(); 00066 pwmout_init(&_pwm, pin); 00067 core_util_critical_section_exit(); 00068 } 00069 00070 /** Set the ouput duty-cycle, specified as a percentage (float) 00071 * 00072 * @param value A floating-point value representing the output duty-cycle, 00073 * specified as a percentage. The value should lie between 00074 * 0.0f (representing on 0%) and 1.0f (representing on 100%). 00075 * Values outside this range will be saturated to 0.0f or 1.0f. 00076 */ 00077 void write(float value) { 00078 core_util_critical_section_enter(); 00079 pwmout_write(&_pwm, value); 00080 core_util_critical_section_exit(); 00081 } 00082 00083 /** Return the current output duty-cycle setting, measured as a percentage (float) 00084 * 00085 * @returns 00086 * A floating-point value representing the current duty-cycle being output on the pin, 00087 * measured as a percentage. The returned value will lie between 00088 * 0.0f (representing on 0%) and 1.0f (representing on 100%). 00089 * 00090 * @note 00091 * This value may not match exactly the value set by a previous <write>. 00092 */ 00093 float read() { 00094 core_util_critical_section_enter(); 00095 float val = pwmout_read(&_pwm); 00096 core_util_critical_section_exit(); 00097 return val; 00098 } 00099 00100 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. 00101 * 00102 * @note 00103 * The resolution is currently in microseconds; periods smaller than this 00104 * will be set to zero. 00105 */ 00106 void period(float seconds) { 00107 core_util_critical_section_enter(); 00108 pwmout_period(&_pwm, seconds); 00109 core_util_critical_section_exit(); 00110 } 00111 00112 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. 00113 */ 00114 void period_ms(int ms) { 00115 core_util_critical_section_enter(); 00116 pwmout_period_ms(&_pwm, ms); 00117 core_util_critical_section_exit(); 00118 } 00119 00120 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. 00121 */ 00122 void period_us(int us) { 00123 core_util_critical_section_enter(); 00124 pwmout_period_us(&_pwm, us); 00125 core_util_critical_section_exit(); 00126 } 00127 00128 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. 00129 */ 00130 void pulsewidth(float seconds) { 00131 core_util_critical_section_enter(); 00132 pwmout_pulsewidth(&_pwm, seconds); 00133 core_util_critical_section_exit(); 00134 } 00135 00136 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. 00137 */ 00138 void pulsewidth_ms(int ms) { 00139 core_util_critical_section_enter(); 00140 pwmout_pulsewidth_ms(&_pwm, ms); 00141 core_util_critical_section_exit(); 00142 } 00143 00144 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. 00145 */ 00146 void pulsewidth_us(int us) { 00147 core_util_critical_section_enter(); 00148 pwmout_pulsewidth_us(&_pwm, us); 00149 core_util_critical_section_exit(); 00150 } 00151 00152 /** A operator shorthand for write() 00153 */ 00154 PwmOut& operator= (float value) { 00155 // Underlying call is thread safe 00156 write(value); 00157 return *this; 00158 } 00159 00160 PwmOut& operator= (PwmOut& rhs) { 00161 // Underlying call is thread safe 00162 write(rhs.read()); 00163 return *this; 00164 } 00165 00166 /** An operator shorthand for read() 00167 */ 00168 operator float() { 00169 // Underlying call is thread safe 00170 return read(); 00171 } 00172 00173 protected: 00174 pwmout_t _pwm; 00175 }; 00176 00177 } // namespace mbed 00178 00179 #endif 00180 00181 #endif
Generated on Tue Jul 12 2022 19:06:16 by 1.7.2