8-bit Fm+ I2C-bus 100 mA 40 V LED driver
Dependencies: mbed
PCA9624.cpp
00001 #include "mbed.h" 00002 #include "PCA9624.h" 00003 00004 I2C i2c(p28,p27); // sda, scl 00005 Serial pc(USBTX, USBRX); // tx, rx 00006 char cmd[32]; 00007 int i, j; 00008 00009 void set_ch(char sel) 00010 { // PCA9541のサンプル 00011 // MST_0側の自分にスレーブ側の制御権を得る場合 00012 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00013 i2c.write( 0xe2, cmd, 1); // Cont Regを指定 00014 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00015 wait(0.1); // 0.1s待つ 00016 switch(cmd[0] & 0xf) 00017 { 00018 case 0: // bus off, has control 00019 case 1: // bus off, no control 00020 case 5: // bus on, no control 00021 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00022 cmd[1] = 4; // bus on, has control 00023 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00024 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00025 break; 00026 case 2: // bus off, no control 00027 case 3: // bus off, has control 00028 case 6: // bus on, no control 00029 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00030 cmd[1] = 5; // bus on, has control 00031 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00032 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00033 break; 00034 case 9: // bus on, no control 00035 case 0xc: // bus on, no control 00036 case 0xd: // bus off, no control 00037 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00038 cmd[1] = 0; // bus on, has control 00039 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00040 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00041 break; 00042 case 0xa: // bus on, no control 00043 case 0xe: // bus off, no control 00044 case 0xf: // bus on, has control 00045 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00046 cmd[1] = 1; // bus on, has control 00047 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00048 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00049 break; 00050 default: 00051 break; 00052 } 00053 00054 cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled 00055 i2c.write( 0xe8, cmd, 1); // Send command string 00056 } 00057 00058 int main () 00059 { 00060 i2c.frequency(100000); 00061 pc.printf("PCA9624 Sample Program\r\n"); 00062 00063 set_ch(2); // PCA9624はch1に接続 00064 00065 // PCA9624 00066 cmd[0] = MODE1; 00067 cmd[1] = 0x0; // SLEEP = 0 00068 i2c.write(PCA9624_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み 00069 00070 cmd[0] = LEDOUT0 + 0x80; // LEDOUT0, Auto incriment 00071 cmd[1] = 0xaa; // LED3,2,1,0 10= PWM 00072 cmd[2] = 0xaa; // LED7,6,5,4 10= PWM 00073 i2c.write(PCA9624_ADDR, cmd, 3); // cmd[0]Regにcmd[1-2]を書込み 00074 00075 while(1) 00076 { 00077 if (i>7) i = 0; // 0-7を循環 00078 cmd[0] = PWM0 + 0x80; // PWM0, Auto incriment 00079 for(j=0; j<8; j++) cmd[j+1] = 0x0; // PWM = 0% 00080 cmd[i+1] = 0xff; // PWM = 99.6% 00081 i2c.write(PCA9624_ADDR, cmd, 9); // cmd[0]Regにcmd[1-16]を書込み 00082 wait(1.0); 00083 i++; 00084 } 00085 } 00086 00087
Generated on Tue Jul 12 2022 21:26:58 by 1.7.2