PCA9635 16-bit I2C-bus LED driver
Dependents: digitalThermometer Counter SimpleClock printNumber ... more
PCA9635.cpp@9:48985b738e5a, 2011-06-29 (annotated)
- Committer:
- d_worrall
- Date:
- Wed Jun 29 13:39:28 2011 +0000
- Revision:
- 9:48985b738e5a
- Parent:
- 7:5c10a0ccc663
- Child:
- 10:b807470a8b09
version8
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 | 0:d9cc568daeaf | 6 | #include "mbed.h" |
d_worrall | 0:d9cc568daeaf | 7 | #include "PCA9635.h" |
d_worrall | 0:d9cc568daeaf | 8 | |
d_worrall | 2:9ca6a4fbab5e | 9 | PCA9635::PCA9635(PinName sda, PinName scl) : m_i2c(sda, scl) |
d_worrall | 0:d9cc568daeaf | 10 | { |
d_worrall | 2:9ca6a4fbab5e | 11 | init(0x02); |
d_worrall | 0:d9cc568daeaf | 12 | } |
d_worrall | 0:d9cc568daeaf | 13 | |
d_worrall | 0:d9cc568daeaf | 14 | |
d_worrall | 0:d9cc568daeaf | 15 | //Driver Software Reset |
d_worrall | 0:d9cc568daeaf | 16 | void PCA9635::reset(void){ |
d_worrall | 4:056255549579 | 17 | cmd[0] = 0x06; |
d_worrall | 4:056255549579 | 18 | int ack = m_i2c.write(m_addr, cmd, 1); |
d_worrall | 4:056255549579 | 19 | if(!ack); |
d_worrall | 4:056255549579 | 20 | else return; |
d_worrall | 4:056255549579 | 21 | cmd[0] = 0xA5; |
d_worrall | 4:056255549579 | 22 | cmd[1] = 0x5A; |
d_worrall | 4:056255549579 | 23 | m_i2c.write(m_addr, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 24 | } |
d_worrall | 0:d9cc568daeaf | 25 | |
d_worrall | 2:9ca6a4fbab5e | 26 | void PCA9635::init(int address){ |
d_worrall | 2:9ca6a4fbab5e | 27 | m_addr = address; |
d_worrall | 2:9ca6a4fbab5e | 28 | |
d_worrall | 0:d9cc568daeaf | 29 | reset(); |
d_worrall | 0:d9cc568daeaf | 30 | |
d_worrall | 0:d9cc568daeaf | 31 | //Mode-1 Register: |
d_worrall | 0:d9cc568daeaf | 32 | cmd[0] = 0x00; |
d_worrall | 0:d9cc568daeaf | 33 | cmd[1] = 0x00; |
d_worrall | 2:9ca6a4fbab5e | 34 | m_i2c.write(address, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 35 | |
d_worrall | 0:d9cc568daeaf | 36 | //Mode-2 Register: |
d_worrall | 0:d9cc568daeaf | 37 | cmd[0] = 0x01; |
d_worrall | 0:d9cc568daeaf | 38 | cmd[1] = 0x22; |
d_worrall | 2:9ca6a4fbab5e | 39 | m_i2c.write(address, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 40 | |
d_worrall | 0:d9cc568daeaf | 41 | //LED Registers into PWM Control |
d_worrall | 0:d9cc568daeaf | 42 | for(char i=0x14; i<0x18; i++) |
d_worrall | 0:d9cc568daeaf | 43 | { |
d_worrall | 0:d9cc568daeaf | 44 | cmd[0] = i; |
d_worrall | 0:d9cc568daeaf | 45 | cmd[1] = 0xAA; |
d_worrall | 2:9ca6a4fbab5e | 46 | m_i2c.write(address, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 47 | } |
d_worrall | 0:d9cc568daeaf | 48 | } |
d_worrall | 0:d9cc568daeaf | 49 | |
d_worrall | 7:5c10a0ccc663 | 50 | void PCA9635::setAddress(int address){ |
d_worrall | 7:5c10a0ccc663 | 51 | m_addr = address; |
d_worrall | 7:5c10a0ccc663 | 52 | } |
d_worrall | 0:d9cc568daeaf | 53 | |
d_worrall | 0:d9cc568daeaf | 54 | //Single LED On |
d_worrall | 0:d9cc568daeaf | 55 | void PCA9635::on(char led) |
d_worrall | 0:d9cc568daeaf | 56 | { |
d_worrall | 0:d9cc568daeaf | 57 | cmd[0] = led + 2; //led position |
d_worrall | 0:d9cc568daeaf | 58 | cmd[1] = 0xFF; //brightness value |
d_worrall | 0:d9cc568daeaf | 59 | m_i2c.write(m_addr, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 60 | } |
d_worrall | 0:d9cc568daeaf | 61 | |
d_worrall | 0:d9cc568daeaf | 62 | |
d_worrall | 0:d9cc568daeaf | 63 | //Single LED Off |
d_worrall | 0:d9cc568daeaf | 64 | void PCA9635::off(char led) |
d_worrall | 0:d9cc568daeaf | 65 | { |
d_worrall | 0:d9cc568daeaf | 66 | cmd[0] = led + 2; //led position |
d_worrall | 0:d9cc568daeaf | 67 | cmd[1] = 0x00; //brightness value |
d_worrall | 0:d9cc568daeaf | 68 | m_i2c.write(m_addr, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 69 | } |
d_worrall | 0:d9cc568daeaf | 70 | |
d_worrall | 0:d9cc568daeaf | 71 | |
d_worrall | 0:d9cc568daeaf | 72 | void PCA9635::bus(short leds) |
d_worrall | 0:d9cc568daeaf | 73 | { |
d_worrall | 0:d9cc568daeaf | 74 | short temp = leds; //check each bit of leds |
d_worrall | 0:d9cc568daeaf | 75 | for(int j=0; j<16; j++) { |
d_worrall | 6:3f35bdfec837 | 76 | if(temp & 1){ //set output according to LSB |
d_worrall | 0:d9cc568daeaf | 77 | on(j); |
d_worrall | 0:d9cc568daeaf | 78 | } |
d_worrall | 0:d9cc568daeaf | 79 | else { |
d_worrall | 0:d9cc568daeaf | 80 | off(j); |
d_worrall | 0:d9cc568daeaf | 81 | } |
d_worrall | 0:d9cc568daeaf | 82 | temp = (temp >> 1); //bitwise shift right by 1 to scan next bit |
d_worrall | 0:d9cc568daeaf | 83 | } |
d_worrall | 0:d9cc568daeaf | 84 | } |
d_worrall | 9:48985b738e5a | 85 | void PCA9635::bus(short leds, int address) |
d_worrall | 9:48985b738e5a | 86 | { |
d_worrall | 9:48985b738e5a | 87 | setAddress(address); |
d_worrall | 9:48985b738e5a | 88 | |
d_worrall | 9:48985b738e5a | 89 | short temp = leds; //check each bit of leds |
d_worrall | 9:48985b738e5a | 90 | for(int j=0; j<16; j++) { |
d_worrall | 9:48985b738e5a | 91 | if(temp & 1){ //set output according to LSB |
d_worrall | 9:48985b738e5a | 92 | on(j); |
d_worrall | 9:48985b738e5a | 93 | } |
d_worrall | 9:48985b738e5a | 94 | else { |
d_worrall | 9:48985b738e5a | 95 | off(j); |
d_worrall | 9:48985b738e5a | 96 | } |
d_worrall | 9:48985b738e5a | 97 | temp = (temp >> 1); //bitwise shift right by 1 to scan next bit |
d_worrall | 9:48985b738e5a | 98 | } |
d_worrall | 9:48985b738e5a | 99 | } |
d_worrall | 0:d9cc568daeaf | 100 | |
d_worrall | 0:d9cc568daeaf | 101 | //Brightness control for single or all LEDs |
d_worrall | 0:d9cc568daeaf | 102 | void PCA9635::brightness(char led, char value) |
d_worrall | 0:d9cc568daeaf | 103 | { |
d_worrall | 0:d9cc568daeaf | 104 | if(led == ALL) |
d_worrall | 0:d9cc568daeaf | 105 | { |
d_worrall | 0:d9cc568daeaf | 106 | for(char allLeds=0x02; allLeds<0x12; allLeds++) |
d_worrall | 0:d9cc568daeaf | 107 | { |
d_worrall | 0:d9cc568daeaf | 108 | cmd[0] = allLeds; |
d_worrall | 0:d9cc568daeaf | 109 | cmd[1] = value; |
d_worrall | 0:d9cc568daeaf | 110 | m_i2c.write(m_addr, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 111 | } |
d_worrall | 0:d9cc568daeaf | 112 | } |
d_worrall | 0:d9cc568daeaf | 113 | else |
d_worrall | 0:d9cc568daeaf | 114 | { |
d_worrall | 0:d9cc568daeaf | 115 | cmd[0] = led + 2; |
d_worrall | 0:d9cc568daeaf | 116 | cmd[1] = value; |
d_worrall | 0:d9cc568daeaf | 117 | m_i2c.write(m_addr, cmd, 2); |
d_worrall | 0:d9cc568daeaf | 118 | } |
d_worrall | 0:d9cc568daeaf | 119 | } |
d_worrall | 0:d9cc568daeaf | 120 | |
d_worrall | 0:d9cc568daeaf | 121 | |
d_worrall | 0:d9cc568daeaf | 122 | |
d_worrall | 0:d9cc568daeaf | 123 | |
d_worrall | 0:d9cc568daeaf | 124 | |
d_worrall | 0:d9cc568daeaf | 125 |