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