Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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