8-bit Fm+ I2C-bus 100 mA 40 V LED driver

Dependencies:   mbed

Revision:
0:5b422753a79e
diff -r 000000000000 -r 5b422753a79e PCA9624.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9624.cpp	Wed Feb 04 06:05:59 2015 +0000
@@ -0,0 +1,87 @@
+#include "mbed.h"
+#include "PCA9624.h"
+ 
+I2C i2c(p28,p27);        // sda, scl
+Serial pc(USBTX, USBRX); // tx, rx
+char cmd[32];
+int i, j;
+ 
+void set_ch(char sel)
+{    // PCA9541のサンプル
+        // MST_0側の自分にスレーブ側の制御権を得る場合
+    cmd[0] = 1;                     // PCA9541 コマンドコード Cont Reg
+    i2c.write( 0xe2, cmd, 1);       // Cont Regを指定
+    i2c.read( 0xe2, cmd, 1);        // Cont Regを読込み
+    wait(0.1);                      // 0.1s待つ
+    switch(cmd[0] & 0xf)
+    {
+    case 0:                         // bus off, has control
+    case 1:                         // bus off, no control
+    case 5:                         // bus on, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 4;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 2:                         // bus off, no control
+    case 3:                         // bus off, has control
+    case 6:                         // bus on, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 5;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 9:                         // bus on, no control
+    case 0xc:                       // bus on, no control
+    case 0xd:                       // bus off, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 0;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 0xa:                       // bus on, no control
+    case 0xe:                       // bus off, no control
+    case 0xf:                       // bus on, has control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 1;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    default:
+        break;
+    }
+
+    cmd[0] = sel;                   // PCA9546 Cont Reg sel channel enabled
+    i2c.write( 0xe8, cmd, 1);       // Send command string
+}
+
+int main ()
+{
+   i2c.frequency(100000);
+    pc.printf("PCA9624 Sample Program\r\n");
+     
+    set_ch(2);              // PCA9624はch1に接続
+
+  // PCA9624
+  cmd[0] = MODE1;
+  cmd[1] = 0x0;           // SLEEP = 0
+  i2c.write(PCA9624_ADDR, cmd, 2);   // cmd[0]Regにcmd[1]を書込み
+
+  cmd[0] = LEDOUT0 + 0x80;   // LEDOUT0, Auto incriment
+  cmd[1] = 0xaa;          // LED3,2,1,0 10= PWM
+  cmd[2] = 0xaa;          // LED7,6,5,4 10= PWM
+  i2c.write(PCA9624_ADDR, cmd, 3);   // cmd[0]Regにcmd[1-2]を書込み
+
+  while(1)
+  {
+    if (i>7) i = 0;                 // 0-7を循環
+    cmd[0] = PWM0 + 0x80;           // PWM0, Auto incriment
+    for(j=0; j<8; j++)  cmd[j+1] = 0x0;   // PWM = 0%
+    cmd[i+1] = 0xff;   // PWM = 99.6%
+    i2c.write(PCA9624_ADDR, cmd, 9);   // cmd[0]Regにcmd[1-16]を書込み
+    wait(1.0);
+    i++;
+  }
+}
+
+