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