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