PCA9635 16-bit I2C-bus LED driver

Dependents:   digitalThermometer Counter SimpleClock printNumber ... more

Committer:
d_worrall
Date:
Tue Jun 28 12:32:18 2011 +0000
Revision:
2:9ca6a4fbab5e
Parent:
0:d9cc568daeaf
Child:
4:056255549579
version2

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 #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 0:d9cc568daeaf 17 cmd[0] = 0x03;
d_worrall 0:d9cc568daeaf 18 m_i2c.write(m_addr, cmd, 1);
d_worrall 0:d9cc568daeaf 19 }
d_worrall 0:d9cc568daeaf 20
d_worrall 2:9ca6a4fbab5e 21 void PCA9635::init(int address){
d_worrall 2:9ca6a4fbab5e 22 m_addr = address;
d_worrall 2:9ca6a4fbab5e 23
d_worrall 0:d9cc568daeaf 24 reset();
d_worrall 0:d9cc568daeaf 25
d_worrall 0:d9cc568daeaf 26 //Mode-1 Register:
d_worrall 0:d9cc568daeaf 27 cmd[0] = 0x00;
d_worrall 0:d9cc568daeaf 28 cmd[1] = 0x00;
d_worrall 2:9ca6a4fbab5e 29 m_i2c.write(address, cmd, 2);
d_worrall 0:d9cc568daeaf 30
d_worrall 0:d9cc568daeaf 31 //Mode-2 Register:
d_worrall 0:d9cc568daeaf 32 cmd[0] = 0x01;
d_worrall 0:d9cc568daeaf 33 cmd[1] = 0x22;
d_worrall 2:9ca6a4fbab5e 34 m_i2c.write(address, cmd, 2);
d_worrall 0:d9cc568daeaf 35
d_worrall 0:d9cc568daeaf 36 //LED Registers into PWM Control
d_worrall 0:d9cc568daeaf 37 for(char i=0x14; i<0x18; i++)
d_worrall 0:d9cc568daeaf 38 {
d_worrall 0:d9cc568daeaf 39 cmd[0] = i;
d_worrall 0:d9cc568daeaf 40 cmd[1] = 0xAA;
d_worrall 2:9ca6a4fbab5e 41 m_i2c.write(address, cmd, 2);
d_worrall 0:d9cc568daeaf 42 }
d_worrall 0:d9cc568daeaf 43 }
d_worrall 0:d9cc568daeaf 44
d_worrall 0:d9cc568daeaf 45
d_worrall 0:d9cc568daeaf 46 //Single LED On
d_worrall 0:d9cc568daeaf 47 void PCA9635::on(char led)
d_worrall 0:d9cc568daeaf 48 {
d_worrall 0:d9cc568daeaf 49 cmd[0] = led + 2; //led position
d_worrall 0:d9cc568daeaf 50 cmd[1] = 0xFF; //brightness value
d_worrall 0:d9cc568daeaf 51 m_i2c.write(m_addr, cmd, 2);
d_worrall 0:d9cc568daeaf 52 }
d_worrall 0:d9cc568daeaf 53
d_worrall 0:d9cc568daeaf 54
d_worrall 0:d9cc568daeaf 55 //Single LED Off
d_worrall 0:d9cc568daeaf 56 void PCA9635::off(char led)
d_worrall 0:d9cc568daeaf 57 {
d_worrall 0:d9cc568daeaf 58 cmd[0] = led + 2; //led position
d_worrall 0:d9cc568daeaf 59 cmd[1] = 0x00; //brightness value
d_worrall 0:d9cc568daeaf 60 m_i2c.write(m_addr, cmd, 2);
d_worrall 0:d9cc568daeaf 61 }
d_worrall 0:d9cc568daeaf 62
d_worrall 0:d9cc568daeaf 63
d_worrall 0:d9cc568daeaf 64 void PCA9635::bus(short leds)
d_worrall 0:d9cc568daeaf 65 {
d_worrall 0:d9cc568daeaf 66 short temp = leds; //check each bit of leds
d_worrall 0:d9cc568daeaf 67 bool stat = false; //check LSB of temp
d_worrall 0:d9cc568daeaf 68 for(int j=0; j<16; j++) {
d_worrall 0:d9cc568daeaf 69 stat = (bool)(temp & 0x0001);
d_worrall 0:d9cc568daeaf 70 if(stat == true){ //set output accordingly
d_worrall 0:d9cc568daeaf 71 on(j);
d_worrall 0:d9cc568daeaf 72 }
d_worrall 0:d9cc568daeaf 73 else {
d_worrall 0:d9cc568daeaf 74 off(j);
d_worrall 0:d9cc568daeaf 75 }
d_worrall 0:d9cc568daeaf 76 temp = (temp >> 1); //bitwise shift right by 1 to scan next bit
d_worrall 0:d9cc568daeaf 77 }
d_worrall 0:d9cc568daeaf 78 }
d_worrall 0:d9cc568daeaf 79
d_worrall 0:d9cc568daeaf 80 //Brightness control for single or all LEDs
d_worrall 0:d9cc568daeaf 81 void PCA9635::brightness(char led, char value)
d_worrall 0:d9cc568daeaf 82 {
d_worrall 0:d9cc568daeaf 83 if(led == ALL)
d_worrall 0:d9cc568daeaf 84 {
d_worrall 0:d9cc568daeaf 85 for(char allLeds=0x02; allLeds<0x12; allLeds++)
d_worrall 0:d9cc568daeaf 86 {
d_worrall 0:d9cc568daeaf 87 cmd[0] = allLeds;
d_worrall 0:d9cc568daeaf 88 cmd[1] = value;
d_worrall 0:d9cc568daeaf 89 m_i2c.write(m_addr, cmd, 2);
d_worrall 0:d9cc568daeaf 90 }
d_worrall 0:d9cc568daeaf 91 }
d_worrall 0:d9cc568daeaf 92 else
d_worrall 0:d9cc568daeaf 93 {
d_worrall 0:d9cc568daeaf 94 cmd[0] = led + 2;
d_worrall 0:d9cc568daeaf 95 cmd[1] = value;
d_worrall 0:d9cc568daeaf 96 m_i2c.write(m_addr, cmd, 2);
d_worrall 0:d9cc568daeaf 97 }
d_worrall 0:d9cc568daeaf 98 }
d_worrall 0:d9cc568daeaf 99
d_worrall 0:d9cc568daeaf 100
d_worrall 0:d9cc568daeaf 101
d_worrall 0:d9cc568daeaf 102
d_worrall 0:d9cc568daeaf 103
d_worrall 0:d9cc568daeaf 104