dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/api/PwmOut.h@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 1:55a6170b404f
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* mbed Microcontroller Library |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2006-2013 ARM Limited |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 1:55a6170b404f | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 6 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 7 | * |
nexpaq | 1:55a6170b404f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 9 | * |
nexpaq | 1:55a6170b404f | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 1:55a6170b404f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 13 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 14 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 15 | */ |
nexpaq | 1:55a6170b404f | 16 | #ifndef MBED_PWMOUT_H |
nexpaq | 1:55a6170b404f | 17 | #define MBED_PWMOUT_H |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | #include "platform.h" |
nexpaq | 1:55a6170b404f | 20 | |
nexpaq | 1:55a6170b404f | 21 | #if DEVICE_PWMOUT |
nexpaq | 1:55a6170b404f | 22 | #include "pwmout_api.h" |
nexpaq | 1:55a6170b404f | 23 | #include "critical.h" |
nexpaq | 1:55a6170b404f | 24 | |
nexpaq | 1:55a6170b404f | 25 | namespace mbed { |
nexpaq | 1:55a6170b404f | 26 | |
nexpaq | 1:55a6170b404f | 27 | /** A pulse-width modulation digital output |
nexpaq | 1:55a6170b404f | 28 | * |
nexpaq | 1:55a6170b404f | 29 | * @Note Synchronization level: Interrupt safe |
nexpaq | 1:55a6170b404f | 30 | * |
nexpaq | 1:55a6170b404f | 31 | * Example |
nexpaq | 1:55a6170b404f | 32 | * @code |
nexpaq | 1:55a6170b404f | 33 | * // Fade a led on. |
nexpaq | 1:55a6170b404f | 34 | * #include "mbed.h" |
nexpaq | 1:55a6170b404f | 35 | * |
nexpaq | 1:55a6170b404f | 36 | * PwmOut led(LED1); |
nexpaq | 1:55a6170b404f | 37 | * |
nexpaq | 1:55a6170b404f | 38 | * int main() { |
nexpaq | 1:55a6170b404f | 39 | * while(1) { |
nexpaq | 1:55a6170b404f | 40 | * led = led + 0.01; |
nexpaq | 1:55a6170b404f | 41 | * wait(0.2); |
nexpaq | 1:55a6170b404f | 42 | * if(led == 1.0) { |
nexpaq | 1:55a6170b404f | 43 | * led = 0; |
nexpaq | 1:55a6170b404f | 44 | * } |
nexpaq | 1:55a6170b404f | 45 | * } |
nexpaq | 1:55a6170b404f | 46 | * } |
nexpaq | 1:55a6170b404f | 47 | * @endcode |
nexpaq | 1:55a6170b404f | 48 | * |
nexpaq | 1:55a6170b404f | 49 | * @note |
nexpaq | 1:55a6170b404f | 50 | * On the LPC1768 and LPC2368, the PWMs all share the same |
nexpaq | 1:55a6170b404f | 51 | * period - if you change the period for one, you change it for all. |
nexpaq | 1:55a6170b404f | 52 | * Although routines that change the period maintain the duty cycle |
nexpaq | 1:55a6170b404f | 53 | * for its PWM, all other PWMs will require their duty cycle to be |
nexpaq | 1:55a6170b404f | 54 | * refreshed. |
nexpaq | 1:55a6170b404f | 55 | */ |
nexpaq | 1:55a6170b404f | 56 | class PwmOut { |
nexpaq | 1:55a6170b404f | 57 | |
nexpaq | 1:55a6170b404f | 58 | public: |
nexpaq | 1:55a6170b404f | 59 | |
nexpaq | 1:55a6170b404f | 60 | /** Create a PwmOut connected to the specified pin |
nexpaq | 1:55a6170b404f | 61 | * |
nexpaq | 1:55a6170b404f | 62 | * @param pin PwmOut pin to connect to |
nexpaq | 1:55a6170b404f | 63 | */ |
nexpaq | 1:55a6170b404f | 64 | PwmOut(PinName pin) { |
nexpaq | 1:55a6170b404f | 65 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 66 | pwmout_init(&_pwm, pin); |
nexpaq | 1:55a6170b404f | 67 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 68 | } |
nexpaq | 1:55a6170b404f | 69 | |
nexpaq | 1:55a6170b404f | 70 | /** Set the ouput duty-cycle, specified as a percentage (float) |
nexpaq | 1:55a6170b404f | 71 | * |
nexpaq | 1:55a6170b404f | 72 | * @param value A floating-point value representing the output duty-cycle, |
nexpaq | 1:55a6170b404f | 73 | * specified as a percentage. The value should lie between |
nexpaq | 1:55a6170b404f | 74 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
nexpaq | 1:55a6170b404f | 75 | * Values outside this range will be saturated to 0.0f or 1.0f. |
nexpaq | 1:55a6170b404f | 76 | */ |
nexpaq | 1:55a6170b404f | 77 | void write(float value) { |
nexpaq | 1:55a6170b404f | 78 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 79 | pwmout_write(&_pwm, value); |
nexpaq | 1:55a6170b404f | 80 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 81 | } |
nexpaq | 1:55a6170b404f | 82 | |
nexpaq | 1:55a6170b404f | 83 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
nexpaq | 1:55a6170b404f | 84 | * |
nexpaq | 1:55a6170b404f | 85 | * @returns |
nexpaq | 1:55a6170b404f | 86 | * A floating-point value representing the current duty-cycle being output on the pin, |
nexpaq | 1:55a6170b404f | 87 | * measured as a percentage. The returned value will lie between |
nexpaq | 1:55a6170b404f | 88 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
nexpaq | 1:55a6170b404f | 89 | * |
nexpaq | 1:55a6170b404f | 90 | * @note |
nexpaq | 1:55a6170b404f | 91 | * This value may not match exactly the value set by a previous <write>. |
nexpaq | 1:55a6170b404f | 92 | */ |
nexpaq | 1:55a6170b404f | 93 | float read() { |
nexpaq | 1:55a6170b404f | 94 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 95 | float val = pwmout_read(&_pwm); |
nexpaq | 1:55a6170b404f | 96 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 97 | return val; |
nexpaq | 1:55a6170b404f | 98 | } |
nexpaq | 1:55a6170b404f | 99 | |
nexpaq | 1:55a6170b404f | 100 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
nexpaq | 1:55a6170b404f | 101 | * |
nexpaq | 1:55a6170b404f | 102 | * @note |
nexpaq | 1:55a6170b404f | 103 | * The resolution is currently in microseconds; periods smaller than this |
nexpaq | 1:55a6170b404f | 104 | * will be set to zero. |
nexpaq | 1:55a6170b404f | 105 | */ |
nexpaq | 1:55a6170b404f | 106 | void period(float seconds) { |
nexpaq | 1:55a6170b404f | 107 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 108 | pwmout_period(&_pwm, seconds); |
nexpaq | 1:55a6170b404f | 109 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 110 | } |
nexpaq | 1:55a6170b404f | 111 | |
nexpaq | 1:55a6170b404f | 112 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
nexpaq | 1:55a6170b404f | 113 | */ |
nexpaq | 1:55a6170b404f | 114 | void period_ms(int ms) { |
nexpaq | 1:55a6170b404f | 115 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 116 | pwmout_period_ms(&_pwm, ms); |
nexpaq | 1:55a6170b404f | 117 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 118 | } |
nexpaq | 1:55a6170b404f | 119 | |
nexpaq | 1:55a6170b404f | 120 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
nexpaq | 1:55a6170b404f | 121 | */ |
nexpaq | 1:55a6170b404f | 122 | void period_us(int us) { |
nexpaq | 1:55a6170b404f | 123 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 124 | pwmout_period_us(&_pwm, us); |
nexpaq | 1:55a6170b404f | 125 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 126 | } |
nexpaq | 1:55a6170b404f | 127 | |
nexpaq | 1:55a6170b404f | 128 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
nexpaq | 1:55a6170b404f | 129 | */ |
nexpaq | 1:55a6170b404f | 130 | void pulsewidth(float seconds) { |
nexpaq | 1:55a6170b404f | 131 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 132 | pwmout_pulsewidth(&_pwm, seconds); |
nexpaq | 1:55a6170b404f | 133 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 134 | } |
nexpaq | 1:55a6170b404f | 135 | |
nexpaq | 1:55a6170b404f | 136 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
nexpaq | 1:55a6170b404f | 137 | */ |
nexpaq | 1:55a6170b404f | 138 | void pulsewidth_ms(int ms) { |
nexpaq | 1:55a6170b404f | 139 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 140 | pwmout_pulsewidth_ms(&_pwm, ms); |
nexpaq | 1:55a6170b404f | 141 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 142 | } |
nexpaq | 1:55a6170b404f | 143 | |
nexpaq | 1:55a6170b404f | 144 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
nexpaq | 1:55a6170b404f | 145 | */ |
nexpaq | 1:55a6170b404f | 146 | void pulsewidth_us(int us) { |
nexpaq | 1:55a6170b404f | 147 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 148 | pwmout_pulsewidth_us(&_pwm, us); |
nexpaq | 1:55a6170b404f | 149 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 150 | } |
nexpaq | 1:55a6170b404f | 151 | |
nexpaq | 1:55a6170b404f | 152 | /** A operator shorthand for write() |
nexpaq | 1:55a6170b404f | 153 | */ |
nexpaq | 1:55a6170b404f | 154 | PwmOut& operator= (float value) { |
nexpaq | 1:55a6170b404f | 155 | // Underlying call is thread safe |
nexpaq | 1:55a6170b404f | 156 | write(value); |
nexpaq | 1:55a6170b404f | 157 | return *this; |
nexpaq | 1:55a6170b404f | 158 | } |
nexpaq | 1:55a6170b404f | 159 | |
nexpaq | 1:55a6170b404f | 160 | PwmOut& operator= (PwmOut& rhs) { |
nexpaq | 1:55a6170b404f | 161 | // Underlying call is thread safe |
nexpaq | 1:55a6170b404f | 162 | write(rhs.read()); |
nexpaq | 1:55a6170b404f | 163 | return *this; |
nexpaq | 1:55a6170b404f | 164 | } |
nexpaq | 1:55a6170b404f | 165 | |
nexpaq | 1:55a6170b404f | 166 | /** An operator shorthand for read() |
nexpaq | 1:55a6170b404f | 167 | */ |
nexpaq | 1:55a6170b404f | 168 | operator float() { |
nexpaq | 1:55a6170b404f | 169 | // Underlying call is thread safe |
nexpaq | 1:55a6170b404f | 170 | return read(); |
nexpaq | 1:55a6170b404f | 171 | } |
nexpaq | 1:55a6170b404f | 172 | |
nexpaq | 1:55a6170b404f | 173 | protected: |
nexpaq | 1:55a6170b404f | 174 | pwmout_t _pwm; |
nexpaq | 1:55a6170b404f | 175 | }; |
nexpaq | 1:55a6170b404f | 176 | |
nexpaq | 1:55a6170b404f | 177 | } // namespace mbed |
nexpaq | 1:55a6170b404f | 178 | |
nexpaq | 1:55a6170b404f | 179 | #endif |
nexpaq | 1:55a6170b404f | 180 | |
nexpaq | 1:55a6170b404f | 181 | #endif |