test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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