test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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