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