help :(

Dependencies:   FFT

Committer:
dimitryjl23
Date:
Fri Dec 04 18:01:22 2020 +0000
Revision:
11:951bbb0385aa
Parent:
0:d6c9b09b4042
Final :)

Who changed what in which revision?

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