SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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