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