PCA9635 16-bit I2C-bus LED driver

Dependents:   digitalThermometer Counter SimpleClock printNumber ... more

Committer:
d_worrall
Date:
Wed Jun 29 08:45:40 2011 +0000
Revision:
8:47e2fc119445
Parent:
7:5c10a0ccc663
Child:
9:48985b738e5a
version8

Who changed what in which revision?

UserRevisionLine numberNew 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 3:0f4d95a27e26 7 #define MBED_PCA9635_H
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 2:9ca6a4fbab5e 21 * PCA9635 my_driver(p28, p27);
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 2:9ca6a4fbab5e 26 * my_driver.init(0x02);
d_worrall 2:9ca6a4fbab5e 27 * //N.B. you MUST declare init(int address), before calling any PCA9635 functions
d_worrall 0:d9cc568daeaf 28 * //turns each pin on and then off
d_worrall 0:d9cc568daeaf 29 *
d_worrall 0:d9cc568daeaf 30 * short op = 1;
d_worrall 0:d9cc568daeaf 31 * for(char j = 0; j < 16; j++){
d_worrall 0:d9cc568daeaf 32 * my_driver.bus(op);
d_worrall 0:d9cc568daeaf 33 * wait(0.7);
d_worrall 0:d9cc568daeaf 34 * op = (op << 1);
d_worrall 0:d9cc568daeaf 35 * }
d_worrall 0:d9cc568daeaf 36 *
d_worrall 0:d9cc568daeaf 37 * //ramp brightness up three times faster than down, repeatedly
d_worrall 0:d9cc568daeaf 38 *
d_worrall 0:d9cc568daeaf 39 * while(1)
d_worrall 0:d9cc568daeaf 40 * {
d_worrall 0:d9cc568daeaf 41 * for(char valueUp=0; valueUp<0xFF; (valueUp = valueUp + 3))
d_worrall 0:d9cc568daeaf 42 * {
d_worrall 0:d9cc568daeaf 43 * my_driver.brightness(ALL, valueUp);
d_worrall 0:d9cc568daeaf 44 * }
d_worrall 0:d9cc568daeaf 45 *
d_worrall 0:d9cc568daeaf 46 * for(char valueD=0; valueD<0xFF; valueD++)
d_worrall 0:d9cc568daeaf 47 * {
d_worrall 0:d9cc568daeaf 48 * my_driver.brightness(ALL, (0xFF - valueD));
d_worrall 0:d9cc568daeaf 49 * wait(0.007);
d_worrall 0:d9cc568daeaf 50 * }
d_worrall 0:d9cc568daeaf 51 * }
d_worrall 0:d9cc568daeaf 52 * }
d_worrall 0:d9cc568daeaf 53 * @endcode
d_worrall 0:d9cc568daeaf 54 */
d_worrall 0:d9cc568daeaf 55
d_worrall 0:d9cc568daeaf 56 class PCA9635 {
d_worrall 0:d9cc568daeaf 57
d_worrall 0:d9cc568daeaf 58 public:
d_worrall 0:d9cc568daeaf 59 /** Create a PCA9635 object, connected to the specified I2C pins
d_worrall 0:d9cc568daeaf 60 *
d_worrall 0:d9cc568daeaf 61 * @param sda Defines serial data line
d_worrall 0:d9cc568daeaf 62 * @param scl Defines serial clock line
d_worrall 0:d9cc568daeaf 63 */
d_worrall 2:9ca6a4fbab5e 64 PCA9635(PinName sda, PinName scl);
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 /** Reset chip
d_worrall 7:5c10a0ccc663 92 *
d_worrall 0:d9cc568daeaf 93 */
d_worrall 0:d9cc568daeaf 94 void reset(void);
d_worrall 7:5c10a0ccc663 95 /** Set write address
d_worrall 7:5c10a0ccc663 96 *
d_worrall 7:5c10a0ccc663 97 * &param address write address
d_worrall 7:5c10a0ccc663 98 */
d_worrall 8:47e2fc119445 99 void setAddress(int address);
d_worrall 2:9ca6a4fbab5e 100 /** Initialise chip - MUST call this before calling any other functions
d_worrall 2:9ca6a4fbab5e 101 *
d_worrall 2:9ca6a4fbab5e 102 * @param address hardware address
d_worrall 0:d9cc568daeaf 103 */
d_worrall 2:9ca6a4fbab5e 104 void init(int address);
d_worrall 0:d9cc568daeaf 105
d_worrall 0:d9cc568daeaf 106
d_worrall 0:d9cc568daeaf 107 private:
d_worrall 0:d9cc568daeaf 108 I2C m_i2c;
d_worrall 0:d9cc568daeaf 109 int m_addr;
d_worrall 0:d9cc568daeaf 110
d_worrall 0:d9cc568daeaf 111 #define ALL 0xFF
d_worrall 1:0a254eb888f1 112 };
d_worrall 1:0a254eb888f1 113
d_worrall 1:0a254eb888f1 114 #endif