| 1 | /* mbed Microcontroller Library - PwmOut |
|---|
| 2 | * Copyright (c) 2007-2011 ARM Limited. All rights reserved. |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #ifndef MBED_PWMOUT_H |
|---|
| 6 | #define MBED_PWMOUT_H |
|---|
| 7 | |
|---|
| 8 | #include "device.h" |
|---|
| 9 | |
|---|
| 10 | #if DEVICE_PWMOUT |
|---|
| 11 | |
|---|
| 12 | #include "platform.h" |
|---|
| 13 | #include "PinNames.h" |
|---|
| 14 | #include "PeripheralNames.h" |
|---|
| 15 | #include "Base.h" |
|---|
| 16 | |
|---|
| 17 | namespace mbed { |
|---|
| 18 | |
|---|
| 19 | /* Class: PwmOut |
|---|
| 20 | * A pulse-width modulation digital output |
|---|
| 21 | * |
|---|
| 22 | * Example |
|---|
| 23 | * > // Fade a led on. |
|---|
| 24 | * > #include "mbed.h" |
|---|
| 25 | * > |
|---|
| 26 | * > PwmOut led(LED1); |
|---|
| 27 | * > |
|---|
| 28 | * > int main() { |
|---|
| 29 | * > while(1) { |
|---|
| 30 | * > led = led + 0.01; |
|---|
| 31 | * > wait(0.2); |
|---|
| 32 | * > if(led == 1.0) { |
|---|
| 33 | * > led = 0; |
|---|
| 34 | * > } |
|---|
| 35 | * > } |
|---|
| 36 | * > } |
|---|
| 37 | * |
|---|
| 38 | * Note that on the LPC1768 and LPC2368, the PWMs all share the same |
|---|
| 39 | * period - if you change the period for one, you change it for all. |
|---|
| 40 | * Although routines that change the period maintain the duty cycle |
|---|
| 41 | * for its PWM, all other PWMs will require their duty cycle to be |
|---|
| 42 | * refreshed. |
|---|
| 43 | */ |
|---|
| 44 | class PwmOut : public Base { |
|---|
| 45 | |
|---|
| 46 | public: |
|---|
| 47 | |
|---|
| 48 | /* Constructor: PwmOut |
|---|
| 49 | * Create a PwmOut connected to the specified pin |
|---|
| 50 | * |
|---|
| 51 | * Variables: |
|---|
| 52 | * pin - PwmOut pin to connect to |
|---|
| 53 | */ |
|---|
| 54 | PwmOut(PinName pin, const char *name = NULL); |
|---|
| 55 | |
|---|
| 56 | /* Function: write |
|---|
| 57 | * Set the ouput duty-cycle, specified as a percentage (float) |
|---|
| 58 | * |
|---|
| 59 | * Variables: |
|---|
| 60 | * value - A floating-point value representing the output duty-cycle, |
|---|
| 61 | * specified as a percentage. The value should lie between |
|---|
| 62 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
|---|
| 63 | * Values outside this range will be saturated to 0.0f or 1.0f. |
|---|
| 64 | */ |
|---|
| 65 | void write(float value); |
|---|
| 66 | |
|---|
| 67 | /* Function: read |
|---|
| 68 | * Return the current output duty-cycle setting, measured as a percentage (float) |
|---|
| 69 | * |
|---|
| 70 | * Variables: |
|---|
| 71 | * returns - A floating-point value representing the current duty-cycle being output on the pin, |
|---|
| 72 | * measured as a percentage. The returned value will lie between |
|---|
| 73 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
|---|
| 74 | * |
|---|
| 75 | * Note: |
|---|
| 76 | * This value may not match exactly the value set by a previous <write>. |
|---|
| 77 | */ |
|---|
| 78 | float read(); |
|---|
| 79 | |
|---|
| 80 | /* Function: period |
|---|
| 81 | * Set the PWM period, specified in seconds (float), keeping the |
|---|
| 82 | * duty cycle the same. |
|---|
| 83 | * |
|---|
| 84 | * Note: |
|---|
| 85 | * The resolution is currently in microseconds; periods smaller than this |
|---|
| 86 | * will be set to zero. |
|---|
| 87 | */ |
|---|
| 88 | void period(float seconds); |
|---|
| 89 | |
|---|
| 90 | /* Function: period_ms |
|---|
| 91 | * Set the PWM period, specified in milli-seconds (int), keeping the |
|---|
| 92 | * duty cycle the same. |
|---|
| 93 | */ |
|---|
| 94 | void period_ms(int ms); |
|---|
| 95 | |
|---|
| 96 | /* Function: period_us |
|---|
| 97 | * Set the PWM period, specified in micro-seconds (int), keeping the |
|---|
| 98 | * duty cycle the same. |
|---|
| 99 | */ |
|---|
| 100 | void period_us(int us); |
|---|
| 101 | |
|---|
| 102 | /* Function: pulsewidth |
|---|
| 103 | * Set the PWM pulsewidth, specified in seconds (float), keeping the |
|---|
| 104 | * period the same. |
|---|
| 105 | */ |
|---|
| 106 | void pulsewidth(float seconds); |
|---|
| 107 | |
|---|
| 108 | /* Function: pulsewidth_ms |
|---|
| 109 | * Set the PWM pulsewidth, specified in milli-seconds (int), keeping |
|---|
| 110 | * the period the same. |
|---|
| 111 | */ |
|---|
| 112 | void pulsewidth_ms(int ms); |
|---|
| 113 | |
|---|
| 114 | /* Function: pulsewidth_us |
|---|
| 115 | * Set the PWM pulsewidth, specified in micro-seconds (int), keeping |
|---|
| 116 | * the period the same. |
|---|
| 117 | */ |
|---|
| 118 | void pulsewidth_us(int us); |
|---|
| 119 | |
|---|
| 120 | #ifdef MBED_OPERATORS |
|---|
| 121 | /* Function: operator= |
|---|
| 122 | * A operator shorthand for <write()> |
|---|
| 123 | */ |
|---|
| 124 | PwmOut& operator= (float value); |
|---|
| 125 | PwmOut& operator= (PwmOut& rhs); |
|---|
| 126 | |
|---|
| 127 | /* Function: operator float() |
|---|
| 128 | * An operator shorthand for <read()> |
|---|
| 129 | */ |
|---|
| 130 | operator float(); |
|---|
| 131 | #endif |
|---|
| 132 | |
|---|
| 133 | #ifdef MBED_RPC |
|---|
| 134 | virtual const struct rpc_method *get_rpc_methods(); |
|---|
| 135 | static struct rpc_class *get_rpc_class(); |
|---|
| 136 | #endif |
|---|
| 137 | |
|---|
| 138 | protected: |
|---|
| 139 | |
|---|
| 140 | PWMName _pwm; |
|---|
| 141 | |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | } // namespace mbed |
|---|
| 145 | |
|---|
| 146 | #endif |
|---|
| 147 | |
|---|
| 148 | #endif |
|---|