SPKT

Dependents:   WAV

Committer:
phungductung
Date:
Tue Jun 04 21:51:46 2019 +0000
Revision:
0:e87aa4c49e95
libray

Who changed what in which revision?

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