PCA9635 16-bit I2C-bus LED driver
Dependents: digitalThermometer Counter SimpleClock printNumber ... more
PCA9635.h@1:0a254eb888f1, 2011-06-28 (annotated)
- Committer:
- d_worrall
- Date:
- Tue Jun 28 10:19:23 2011 +0000
- Revision:
- 1:0a254eb888f1
- Parent:
- 0:d9cc568daeaf
- Child:
- 2:9ca6a4fbab5e
version1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
d_worrall | 0:d9cc568daeaf | 1 | //NXP PCA9635 library |
d_worrall | 0:d9cc568daeaf | 2 | //mbed Team - 1st June 2011 |
d_worrall | 0:d9cc568daeaf | 3 | //Ioannis Kedros |
d_worrall | 0:d9cc568daeaf | 4 | //updated by Daniel Worrall - 28th June 2011 |
d_worrall | 0:d9cc568daeaf | 5 | |
d_worrall | 1:0a254eb888f1 | 6 | #ifndef MBED_PCA9635_H |
d_worrall | 1:0a254eb888f1 | 7 | #define MBED_PCA9635 |
d_worrall | 1:0a254eb888f1 | 8 | |
d_worrall | 0:d9cc568daeaf | 9 | #include "mbed.h" |
d_worrall | 0:d9cc568daeaf | 10 | |
d_worrall | 0:d9cc568daeaf | 11 | /** PCA9635 class defined on an I2C master bus |
d_worrall | 0:d9cc568daeaf | 12 | * |
d_worrall | 0:d9cc568daeaf | 13 | * Example: |
d_worrall | 0:d9cc568daeaf | 14 | * @code |
d_worrall | 0:d9cc568daeaf | 15 | * //Turn each output on and then off in order p0 >> p15 |
d_worrall | 0:d9cc568daeaf | 16 | * //then ramp brightenss up and down at different speeds. |
d_worrall | 0:d9cc568daeaf | 17 | * #include "mbed.h" |
d_worrall | 0:d9cc568daeaf | 18 | * #include "PCA9635.h" |
d_worrall | 0:d9cc568daeaf | 19 | * |
d_worrall | 0:d9cc568daeaf | 20 | * DigitalOut enable(p26); |
d_worrall | 0:d9cc568daeaf | 21 | * PCA9635 my_driver(p28, p27, 0x02); |
d_worrall | 0:d9cc568daeaf | 22 | * |
d_worrall | 0:d9cc568daeaf | 23 | * int main() |
d_worrall | 0:d9cc568daeaf | 24 | * { |
d_worrall | 0:d9cc568daeaf | 25 | * enable = 0; |
d_worrall | 0:d9cc568daeaf | 26 | * |
d_worrall | 0:d9cc568daeaf | 27 | * //turns each pin on and then off |
d_worrall | 0:d9cc568daeaf | 28 | * |
d_worrall | 0:d9cc568daeaf | 29 | * short op = 1; |
d_worrall | 0:d9cc568daeaf | 30 | * for(char j = 0; j < 16; j++){ |
d_worrall | 0:d9cc568daeaf | 31 | * my_driver.bus(op); |
d_worrall | 0:d9cc568daeaf | 32 | * wait(0.7); |
d_worrall | 0:d9cc568daeaf | 33 | * op = (op << 1); |
d_worrall | 0:d9cc568daeaf | 34 | * } |
d_worrall | 0:d9cc568daeaf | 35 | * |
d_worrall | 0:d9cc568daeaf | 36 | * //ramp brightness up three times faster than down, repeatedly |
d_worrall | 0:d9cc568daeaf | 37 | * |
d_worrall | 0:d9cc568daeaf | 38 | * while(1) |
d_worrall | 0:d9cc568daeaf | 39 | * { |
d_worrall | 0:d9cc568daeaf | 40 | * for(char valueUp=0; valueUp<0xFF; (valueUp = valueUp + 3)) |
d_worrall | 0:d9cc568daeaf | 41 | * { |
d_worrall | 0:d9cc568daeaf | 42 | * my_driver.brightness(ALL, valueUp); |
d_worrall | 0:d9cc568daeaf | 43 | * } |
d_worrall | 0:d9cc568daeaf | 44 | * |
d_worrall | 0:d9cc568daeaf | 45 | * for(char valueD=0; valueD<0xFF; valueD++) |
d_worrall | 0:d9cc568daeaf | 46 | * { |
d_worrall | 0:d9cc568daeaf | 47 | * my_driver.brightness(ALL, (0xFF - valueD)); |
d_worrall | 0:d9cc568daeaf | 48 | * wait(0.007); |
d_worrall | 0:d9cc568daeaf | 49 | * } |
d_worrall | 0:d9cc568daeaf | 50 | * } |
d_worrall | 0:d9cc568daeaf | 51 | * } |
d_worrall | 0:d9cc568daeaf | 52 | * @endcode |
d_worrall | 0:d9cc568daeaf | 53 | */ |
d_worrall | 0:d9cc568daeaf | 54 | |
d_worrall | 0:d9cc568daeaf | 55 | class PCA9635 { |
d_worrall | 0:d9cc568daeaf | 56 | |
d_worrall | 0:d9cc568daeaf | 57 | public: |
d_worrall | 0:d9cc568daeaf | 58 | /** Create a PCA9635 object, connected to the specified I2C pins |
d_worrall | 0:d9cc568daeaf | 59 | * |
d_worrall | 0:d9cc568daeaf | 60 | * @param sda Defines serial data line |
d_worrall | 0:d9cc568daeaf | 61 | * @param scl Defines serial clock line |
d_worrall | 0:d9cc568daeaf | 62 | * @param addr chip address |
d_worrall | 0:d9cc568daeaf | 63 | */ |
d_worrall | 0:d9cc568daeaf | 64 | PCA9635(PinName sda, PinName scl, int addr); |
d_worrall | 0:d9cc568daeaf | 65 | |
d_worrall | 0:d9cc568daeaf | 66 | char cmd[2]; |
d_worrall | 0:d9cc568daeaf | 67 | |
d_worrall | 0:d9cc568daeaf | 68 | //Output control |
d_worrall | 0:d9cc568daeaf | 69 | |
d_worrall | 0:d9cc568daeaf | 70 | /** Set a particular output on PCA9635 high |
d_worrall | 0:d9cc568daeaf | 71 | * |
d_worrall | 0:d9cc568daeaf | 72 | * @param led LED pin coordinates |
d_worrall | 0:d9cc568daeaf | 73 | */ |
d_worrall | 0:d9cc568daeaf | 74 | void on(char led); |
d_worrall | 0:d9cc568daeaf | 75 | /** Reset a particular output on PCA9635 low |
d_worrall | 0:d9cc568daeaf | 76 | * |
d_worrall | 0:d9cc568daeaf | 77 | * @param led LED pin coordinates |
d_worrall | 0:d9cc568daeaf | 78 | */ |
d_worrall | 0:d9cc568daeaf | 79 | void off(char led); |
d_worrall | 0:d9cc568daeaf | 80 | /**Set all pins passed as argument high or low |
d_worrall | 0:d9cc568daeaf | 81 | * |
d_worrall | 0:d9cc568daeaf | 82 | * @param leds Set output according to parameter value - e.g. 0x0003 >> p0 & p1 high, rest low |
d_worrall | 0:d9cc568daeaf | 83 | */ |
d_worrall | 0:d9cc568daeaf | 84 | void bus(short leds); |
d_worrall | 0:d9cc568daeaf | 85 | /** Define LED brightness |
d_worrall | 0:d9cc568daeaf | 86 | * |
d_worrall | 0:d9cc568daeaf | 87 | * @param led LED pin coordinates |
d_worrall | 0:d9cc568daeaf | 88 | * @param value Brightness of LED, 0x00 < value < 0xFF |
d_worrall | 0:d9cc568daeaf | 89 | */ |
d_worrall | 0:d9cc568daeaf | 90 | void brightness(char led, char value); |
d_worrall | 0:d9cc568daeaf | 91 | |
d_worrall | 0:d9cc568daeaf | 92 | //Reset |
d_worrall | 0:d9cc568daeaf | 93 | |
d_worrall | 0:d9cc568daeaf | 94 | /** Reset chip |
d_worrall | 0:d9cc568daeaf | 95 | */ |
d_worrall | 0:d9cc568daeaf | 96 | void reset(void); |
d_worrall | 0:d9cc568daeaf | 97 | /** Initialise chip |
d_worrall | 0:d9cc568daeaf | 98 | */ |
d_worrall | 0:d9cc568daeaf | 99 | void init(void); |
d_worrall | 0:d9cc568daeaf | 100 | |
d_worrall | 0:d9cc568daeaf | 101 | |
d_worrall | 0:d9cc568daeaf | 102 | private: |
d_worrall | 0:d9cc568daeaf | 103 | I2C m_i2c; |
d_worrall | 0:d9cc568daeaf | 104 | int m_addr; |
d_worrall | 0:d9cc568daeaf | 105 | |
d_worrall | 0:d9cc568daeaf | 106 | #define ALL 0xFF |
d_worrall | 1:0a254eb888f1 | 107 | }; |
d_worrall | 1:0a254eb888f1 | 108 | |
d_worrall | 1:0a254eb888f1 | 109 | #endif |