mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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