24-channel Fm+ I2C-bus 57 mA/20 V constant current LED driver
Dependencies: mbed
PCA9956A.cpp@0:2ae25badaf6a, 2015-02-04 (annotated)
- Committer:
- wataaki
- Date:
- Wed Feb 04 06:09:44 2015 +0000
- Revision:
- 0:2ae25badaf6a
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wataaki | 0:2ae25badaf6a | 1 | #include "mbed.h" |
wataaki | 0:2ae25badaf6a | 2 | #include "PCA9956A.h" |
wataaki | 0:2ae25badaf6a | 3 | |
wataaki | 0:2ae25badaf6a | 4 | I2C i2c(p28,p27); // sda, scl |
wataaki | 0:2ae25badaf6a | 5 | Serial pc(USBTX, USBRX); // tx, rx |
wataaki | 0:2ae25badaf6a | 6 | char cmd[32]; |
wataaki | 0:2ae25badaf6a | 7 | int i, j; |
wataaki | 0:2ae25badaf6a | 8 | |
wataaki | 0:2ae25badaf6a | 9 | void set_ch(char sel) |
wataaki | 0:2ae25badaf6a | 10 | { // PCA9541のサンプル |
wataaki | 0:2ae25badaf6a | 11 | // MST_0側の自分にスレーブ側の制御権を得る場合 |
wataaki | 0:2ae25badaf6a | 12 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:2ae25badaf6a | 13 | i2c.write( 0xe2, cmd, 1); // Cont Regを指定 |
wataaki | 0:2ae25badaf6a | 14 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:2ae25badaf6a | 15 | wait(0.1); // 0.1s待つ |
wataaki | 0:2ae25badaf6a | 16 | switch(cmd[0] & 0xf) |
wataaki | 0:2ae25badaf6a | 17 | { |
wataaki | 0:2ae25badaf6a | 18 | case 0: // bus off, has control |
wataaki | 0:2ae25badaf6a | 19 | case 1: // bus off, no control |
wataaki | 0:2ae25badaf6a | 20 | case 5: // bus on, no control |
wataaki | 0:2ae25badaf6a | 21 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:2ae25badaf6a | 22 | cmd[1] = 4; // bus on, has control |
wataaki | 0:2ae25badaf6a | 23 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 24 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:2ae25badaf6a | 25 | break; |
wataaki | 0:2ae25badaf6a | 26 | case 2: // bus off, no control |
wataaki | 0:2ae25badaf6a | 27 | case 3: // bus off, has control |
wataaki | 0:2ae25badaf6a | 28 | case 6: // bus on, no control |
wataaki | 0:2ae25badaf6a | 29 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:2ae25badaf6a | 30 | cmd[1] = 5; // bus on, has control |
wataaki | 0:2ae25badaf6a | 31 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 32 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:2ae25badaf6a | 33 | break; |
wataaki | 0:2ae25badaf6a | 34 | case 9: // bus on, no control |
wataaki | 0:2ae25badaf6a | 35 | case 0xc: // bus on, no control |
wataaki | 0:2ae25badaf6a | 36 | case 0xd: // bus off, no control |
wataaki | 0:2ae25badaf6a | 37 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:2ae25badaf6a | 38 | cmd[1] = 0; // bus on, has control |
wataaki | 0:2ae25badaf6a | 39 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 40 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:2ae25badaf6a | 41 | break; |
wataaki | 0:2ae25badaf6a | 42 | case 0xa: // bus on, no control |
wataaki | 0:2ae25badaf6a | 43 | case 0xe: // bus off, no control |
wataaki | 0:2ae25badaf6a | 44 | case 0xf: // bus on, has control |
wataaki | 0:2ae25badaf6a | 45 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:2ae25badaf6a | 46 | cmd[1] = 1; // bus on, has control |
wataaki | 0:2ae25badaf6a | 47 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 48 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:2ae25badaf6a | 49 | break; |
wataaki | 0:2ae25badaf6a | 50 | default: |
wataaki | 0:2ae25badaf6a | 51 | break; |
wataaki | 0:2ae25badaf6a | 52 | } |
wataaki | 0:2ae25badaf6a | 53 | |
wataaki | 0:2ae25badaf6a | 54 | cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled |
wataaki | 0:2ae25badaf6a | 55 | i2c.write( 0xe8, cmd, 1); // Send command string |
wataaki | 0:2ae25badaf6a | 56 | } |
wataaki | 0:2ae25badaf6a | 57 | |
wataaki | 0:2ae25badaf6a | 58 | int main () |
wataaki | 0:2ae25badaf6a | 59 | { |
wataaki | 0:2ae25badaf6a | 60 | i2c.frequency(100000); |
wataaki | 0:2ae25badaf6a | 61 | pc.printf("PCA9956A Sample Program\r\n"); |
wataaki | 0:2ae25badaf6a | 62 | |
wataaki | 0:2ae25badaf6a | 63 | set_ch(2); // PCA9956Aはch1に接続 |
wataaki | 0:2ae25badaf6a | 64 | |
wataaki | 0:2ae25badaf6a | 65 | // PCA9956A |
wataaki | 0:2ae25badaf6a | 66 | cmd[0] = MODE1; |
wataaki | 0:2ae25badaf6a | 67 | cmd[1] = 0x0; // SLEEP = 0 |
wataaki | 0:2ae25badaf6a | 68 | i2c.write(PCA9956A_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 69 | |
wataaki | 0:2ae25badaf6a | 70 | cmd[0] = LEDOUT0 + 0x80; // LEDOUT0, Auto incriment |
wataaki | 0:2ae25badaf6a | 71 | cmd[1] = 0xaa; // LED3,2,1,0 10= PWM |
wataaki | 0:2ae25badaf6a | 72 | cmd[2] = 0xaa; // LED7,6,5,4 10= PWM |
wataaki | 0:2ae25badaf6a | 73 | cmd[3] = 0xaa; // LED11,10,9,8 10= PWM |
wataaki | 0:2ae25badaf6a | 74 | cmd[4] = 0xaa; // LED15,14,13,12 10= PWM |
wataaki | 0:2ae25badaf6a | 75 | cmd[5] = 0xaa; // LED19,18,17,16 10= PWM |
wataaki | 0:2ae25badaf6a | 76 | cmd[6] = 0xaa; // LED23,22,21,20 10= PWM |
wataaki | 0:2ae25badaf6a | 77 | i2c.write(PCA9956A_ADDR, cmd, 7); // cmd[0]Regにcmd[1-6]を書込み |
wataaki | 0:2ae25badaf6a | 78 | |
wataaki | 0:2ae25badaf6a | 79 | cmd[0] = IREFALL; |
wataaki | 0:2ae25badaf6a | 80 | cmd[1] = 0x80; // Idrv = 40uA * 128 = 5.12mA |
wataaki | 0:2ae25badaf6a | 81 | i2c.write(PCA9956A_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 82 | |
wataaki | 0:2ae25badaf6a | 83 | while(1) |
wataaki | 0:2ae25badaf6a | 84 | { |
wataaki | 0:2ae25badaf6a | 85 | if (i>23) i = 0; |
wataaki | 0:2ae25badaf6a | 86 | cmd[0] = PWMALL; |
wataaki | 0:2ae25badaf6a | 87 | cmd[1] = 0x0; // PWM = 0% |
wataaki | 0:2ae25badaf6a | 88 | i2c.write(PCA9956A_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 89 | |
wataaki | 0:2ae25badaf6a | 90 | cmd[0] = PWM0 + i; // PWMx = PWM0 + i |
wataaki | 0:2ae25badaf6a | 91 | cmd[1] = 0xf0; // PWM = 99.6% |
wataaki | 0:2ae25badaf6a | 92 | i2c.write(PCA9956A_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み |
wataaki | 0:2ae25badaf6a | 93 | wait(0.5); |
wataaki | 0:2ae25badaf6a | 94 | i++; |
wataaki | 0:2ae25badaf6a | 95 | } |
wataaki | 0:2ae25badaf6a | 96 | } |
wataaki | 0:2ae25badaf6a | 97 |