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