mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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