PCA9635 16-bit I2C-bus LED driver

Dependents:   digitalThermometer Counter SimpleClock printNumber ... more

Committer:
d_worrall
Date:
Tue Jun 28 10:03:49 2011 +0000
Revision:
0:d9cc568daeaf
Child:
1:0a254eb888f1
version1

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