temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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