mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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