![](/media/cache/group/i2c_logo.png.50x50_q85.png)
Low-voltage 16-bit I2C-bus GPIO with Agile I/O, interrupt and weak pull-up
Dependencies: mbed
PCAL9555A.cpp@0:e714fc19ba54, 2015-02-04 (annotated)
- Committer:
- wataaki
- Date:
- Wed Feb 04 06:11:10 2015 +0000
- Revision:
- 0:e714fc19ba54
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wataaki | 0:e714fc19ba54 | 1 | #include "mbed.h" |
wataaki | 0:e714fc19ba54 | 2 | #include "PCAL9555A.h" |
wataaki | 0:e714fc19ba54 | 3 | |
wataaki | 0:e714fc19ba54 | 4 | I2C i2c(p28,p27); // sda, scl |
wataaki | 0:e714fc19ba54 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
wataaki | 0:e714fc19ba54 | 6 | char cmd[32]; |
wataaki | 0:e714fc19ba54 | 7 | char buf[4]; |
wataaki | 0:e714fc19ba54 | 8 | int i, j, k; |
wataaki | 0:e714fc19ba54 | 9 | int sw; |
wataaki | 0:e714fc19ba54 | 10 | |
wataaki | 0:e714fc19ba54 | 11 | void set_ch(char sel) |
wataaki | 0:e714fc19ba54 | 12 | { // PCA9541のサンプル |
wataaki | 0:e714fc19ba54 | 13 | // MST_0側の自分にスレーブ側の制御権を得る場合 |
wataaki | 0:e714fc19ba54 | 14 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:e714fc19ba54 | 15 | i2c.write( 0xe2, cmd, 1); // Cont Regを指定 |
wataaki | 0:e714fc19ba54 | 16 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:e714fc19ba54 | 17 | wait(0.1); // 0.1s待つ |
wataaki | 0:e714fc19ba54 | 18 | switch(cmd[0] & 0xf) |
wataaki | 0:e714fc19ba54 | 19 | { |
wataaki | 0:e714fc19ba54 | 20 | case 0: // bus off, has control |
wataaki | 0:e714fc19ba54 | 21 | case 1: // bus off, no control |
wataaki | 0:e714fc19ba54 | 22 | case 5: // bus on, no control |
wataaki | 0:e714fc19ba54 | 23 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:e714fc19ba54 | 24 | cmd[1] = 4; // bus on, has control |
wataaki | 0:e714fc19ba54 | 25 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:e714fc19ba54 | 26 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:e714fc19ba54 | 27 | break; |
wataaki | 0:e714fc19ba54 | 28 | case 2: // bus off, no control |
wataaki | 0:e714fc19ba54 | 29 | case 3: // bus off, has control |
wataaki | 0:e714fc19ba54 | 30 | case 6: // bus on, no control |
wataaki | 0:e714fc19ba54 | 31 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:e714fc19ba54 | 32 | cmd[1] = 5; // bus on, has control |
wataaki | 0:e714fc19ba54 | 33 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:e714fc19ba54 | 34 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:e714fc19ba54 | 35 | break; |
wataaki | 0:e714fc19ba54 | 36 | case 9: // bus on, no control |
wataaki | 0:e714fc19ba54 | 37 | case 0xc: // bus on, no control |
wataaki | 0:e714fc19ba54 | 38 | case 0xd: // bus off, no control |
wataaki | 0:e714fc19ba54 | 39 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:e714fc19ba54 | 40 | cmd[1] = 0; // bus on, has control |
wataaki | 0:e714fc19ba54 | 41 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:e714fc19ba54 | 42 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:e714fc19ba54 | 43 | break; |
wataaki | 0:e714fc19ba54 | 44 | case 0xa: // bus on, no control |
wataaki | 0:e714fc19ba54 | 45 | case 0xe: // bus off, no control |
wataaki | 0:e714fc19ba54 | 46 | case 0xf: // bus on, has control |
wataaki | 0:e714fc19ba54 | 47 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:e714fc19ba54 | 48 | cmd[1] = 1; // bus on, has control |
wataaki | 0:e714fc19ba54 | 49 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:e714fc19ba54 | 50 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:e714fc19ba54 | 51 | break; |
wataaki | 0:e714fc19ba54 | 52 | default: |
wataaki | 0:e714fc19ba54 | 53 | break; |
wataaki | 0:e714fc19ba54 | 54 | } |
wataaki | 0:e714fc19ba54 | 55 | |
wataaki | 0:e714fc19ba54 | 56 | cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled |
wataaki | 0:e714fc19ba54 | 57 | i2c.write( 0xe8, cmd, 1); // Send command string |
wataaki | 0:e714fc19ba54 | 58 | } |
wataaki | 0:e714fc19ba54 | 59 | |
wataaki | 0:e714fc19ba54 | 60 | int main () |
wataaki | 0:e714fc19ba54 | 61 | { |
wataaki | 0:e714fc19ba54 | 62 | i2c.frequency(100000); |
wataaki | 0:e714fc19ba54 | 63 | pc.printf("PCAL9555A Sample Program\r\n"); |
wataaki | 0:e714fc19ba54 | 64 | set_ch(1); // PCAL9555Aはch0に接続 |
wataaki | 0:e714fc19ba54 | 65 | |
wataaki | 0:e714fc19ba54 | 66 | // PCAL9555A |
wataaki | 0:e714fc19ba54 | 67 | while(1) |
wataaki | 0:e714fc19ba54 | 68 | { |
wataaki | 0:e714fc19ba54 | 69 | pc.printf("入力(PU) … 1, 出力 … 2, Interrupt_status … 3 ? \r\n"); |
wataaki | 0:e714fc19ba54 | 70 | pc.scanf("%d", &sw); |
wataaki | 0:e714fc19ba54 | 71 | pc.printf("%d\r\n", sw); |
wataaki | 0:e714fc19ba54 | 72 | switch (sw) |
wataaki | 0:e714fc19ba54 | 73 | { |
wataaki | 0:e714fc19ba54 | 74 | case 1: |
wataaki | 0:e714fc19ba54 | 75 | pc.printf("入力(PU) Sample Start\r\n"); |
wataaki | 0:e714fc19ba54 | 76 | |
wataaki | 0:e714fc19ba54 | 77 | cmd[0] = PUPD_enable0; |
wataaki | 0:e714fc19ba54 | 78 | cmd[1] = 0xff; // all P0 PUPD enable |
wataaki | 0:e714fc19ba54 | 79 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 80 | cmd[0] = PUPD_selection0; |
wataaki | 0:e714fc19ba54 | 81 | cmd[1] = 0xff; // all P0 PU |
wataaki | 0:e714fc19ba54 | 82 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 83 | cmd[0] = PUPD_enable1; |
wataaki | 0:e714fc19ba54 | 84 | cmd[1] = 0xff; // all P1 PUPD enable |
wataaki | 0:e714fc19ba54 | 85 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 86 | cmd[0] = PUPD_selection1; |
wataaki | 0:e714fc19ba54 | 87 | cmd[1] = 0xff; // all P1 PU |
wataaki | 0:e714fc19ba54 | 88 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 89 | |
wataaki | 0:e714fc19ba54 | 90 | for(i=0; i<20; i++) |
wataaki | 0:e714fc19ba54 | 91 | { |
wataaki | 0:e714fc19ba54 | 92 | cmd[0] = Input_port0; |
wataaki | 0:e714fc19ba54 | 93 | i2c.write(PCAL9555A_ADDR, cmd, 1); |
wataaki | 0:e714fc19ba54 | 94 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input port0 Regを読込み |
wataaki | 0:e714fc19ba54 | 95 | buf[0] = cmd[0]; |
wataaki | 0:e714fc19ba54 | 96 | cmd[0] = Input_port1; |
wataaki | 0:e714fc19ba54 | 97 | i2c.write(PCAL9555A_ADDR, cmd, 1); |
wataaki | 0:e714fc19ba54 | 98 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input port1 Regを読込み |
wataaki | 0:e714fc19ba54 | 99 | pc.printf("Input P0=%02x P1=%02x\r\n", buf[0], cmd[0]); // Input port Regを表示 |
wataaki | 0:e714fc19ba54 | 100 | wait(0.5); |
wataaki | 0:e714fc19ba54 | 101 | } |
wataaki | 0:e714fc19ba54 | 102 | pc.printf("入力(PU) Sample End\r\n"); |
wataaki | 0:e714fc19ba54 | 103 | break; |
wataaki | 0:e714fc19ba54 | 104 | |
wataaki | 0:e714fc19ba54 | 105 | case 2: |
wataaki | 0:e714fc19ba54 | 106 | pc.printf("出力 Sample Start\r\n"); |
wataaki | 0:e714fc19ba54 | 107 | |
wataaki | 0:e714fc19ba54 | 108 | cmd[0] = Configuration0; |
wataaki | 0:e714fc19ba54 | 109 | cmd[1] = 0x0; // all P1 output |
wataaki | 0:e714fc19ba54 | 110 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 111 | cmd[0] = Configuration1; |
wataaki | 0:e714fc19ba54 | 112 | cmd[1] = 0x0; // all P2 output |
wataaki | 0:e714fc19ba54 | 113 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 114 | for(i=0; i<256; i++) |
wataaki | 0:e714fc19ba54 | 115 | { |
wataaki | 0:e714fc19ba54 | 116 | cmd[0] = Output_port0; |
wataaki | 0:e714fc19ba54 | 117 | cmd[1] = i; // P1 = i |
wataaki | 0:e714fc19ba54 | 118 | buf[0] = cmd[1]; // data buffer |
wataaki | 0:e714fc19ba54 | 119 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 120 | cmd[0] = Output_port1; |
wataaki | 0:e714fc19ba54 | 121 | cmd[1] = i; // P2 = 255-i |
wataaki | 0:e714fc19ba54 | 122 | cmd[1] = 255-i; // P2 = 255-i |
wataaki | 0:e714fc19ba54 | 123 | buf[1] = cmd[1]; // data buffer |
wataaki | 0:e714fc19ba54 | 124 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 125 | |
wataaki | 0:e714fc19ba54 | 126 | cmd[0] = Input_port0; |
wataaki | 0:e714fc19ba54 | 127 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 128 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input port1 Regを読込み |
wataaki | 0:e714fc19ba54 | 129 | buf[2] = cmd[0]; // data buffer |
wataaki | 0:e714fc19ba54 | 130 | cmd[0] = Input_port1; |
wataaki | 0:e714fc19ba54 | 131 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 132 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input port1 Regを読込み |
wataaki | 0:e714fc19ba54 | 133 | buf[3] = cmd[0]; // data buffer |
wataaki | 0:e714fc19ba54 | 134 | |
wataaki | 0:e714fc19ba54 | 135 | pc.printf("Output P0=%02x P1=%02x Input P0=%02x P1=%02x\r\n", buf[0], buf[1], buf[2], buf[3]); |
wataaki | 0:e714fc19ba54 | 136 | wait(0.1); |
wataaki | 0:e714fc19ba54 | 137 | } |
wataaki | 0:e714fc19ba54 | 138 | pc.printf("出力 Sample End\r\n"); |
wataaki | 0:e714fc19ba54 | 139 | |
wataaki | 0:e714fc19ba54 | 140 | cmd[0] = Configuration0; |
wataaki | 0:e714fc19ba54 | 141 | cmd[1] = 0xff; // all input |
wataaki | 0:e714fc19ba54 | 142 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 143 | break; |
wataaki | 0:e714fc19ba54 | 144 | |
wataaki | 0:e714fc19ba54 | 145 | case 3: |
wataaki | 0:e714fc19ba54 | 146 | pc.printf("Interrupt_status Sample Start\r\n"); |
wataaki | 0:e714fc19ba54 | 147 | |
wataaki | 0:e714fc19ba54 | 148 | cmd[0] = Configuration0; |
wataaki | 0:e714fc19ba54 | 149 | cmd[1] = 0xff; // all P0 input |
wataaki | 0:e714fc19ba54 | 150 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 151 | |
wataaki | 0:e714fc19ba54 | 152 | cmd[0] = Configuration1; |
wataaki | 0:e714fc19ba54 | 153 | cmd[1] = 0xff; // all P1 input |
wataaki | 0:e714fc19ba54 | 154 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 155 | |
wataaki | 0:e714fc19ba54 | 156 | cmd[0] = Interrupt_mask0; |
wataaki | 0:e714fc19ba54 | 157 | cmd[1] = 0x00; // all P0 割込み可 |
wataaki | 0:e714fc19ba54 | 158 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 159 | |
wataaki | 0:e714fc19ba54 | 160 | cmd[0] = Interrupt_mask1; |
wataaki | 0:e714fc19ba54 | 161 | cmd[1] = 0x00; // all P1 割込み可 |
wataaki | 0:e714fc19ba54 | 162 | i2c.write(PCAL9555A_ADDR, cmd, 2); |
wataaki | 0:e714fc19ba54 | 163 | |
wataaki | 0:e714fc19ba54 | 164 | cmd[0] = Input_port0; |
wataaki | 0:e714fc19ba54 | 165 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 166 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input_port0からオリジナル値の取得 |
wataaki | 0:e714fc19ba54 | 167 | |
wataaki | 0:e714fc19ba54 | 168 | cmd[0] = Input_port1; |
wataaki | 0:e714fc19ba54 | 169 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 170 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Input_port1からオリジナル値の取得 |
wataaki | 0:e714fc19ba54 | 171 | |
wataaki | 0:e714fc19ba54 | 172 | for(i=0; i<100; i++) |
wataaki | 0:e714fc19ba54 | 173 | { |
wataaki | 0:e714fc19ba54 | 174 | cmd[0] = Interrupt_status0; |
wataaki | 0:e714fc19ba54 | 175 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 176 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Interrupt_status0 Regを読込み |
wataaki | 0:e714fc19ba54 | 177 | buf[0] = cmd[0]; // data buffer |
wataaki | 0:e714fc19ba54 | 178 | |
wataaki | 0:e714fc19ba54 | 179 | cmd[0] = Interrupt_status1; |
wataaki | 0:e714fc19ba54 | 180 | i2c.write(PCAL9555A_ADDR, cmd, 1, true); |
wataaki | 0:e714fc19ba54 | 181 | i2c.read(PCAL9555A_ADDR, cmd, 1); // Interrupt_status1 Regを読込み |
wataaki | 0:e714fc19ba54 | 182 | pc.printf("Interrupt_status P0=%02x P1=%02x\r\n", buf[0], cmd[0]); // 割込状況を表示 |
wataaki | 0:e714fc19ba54 | 183 | wait(0.2); |
wataaki | 0:e714fc19ba54 | 184 | } |
wataaki | 0:e714fc19ba54 | 185 | pc.printf("Interrupt_status Sample End\r\n"); |
wataaki | 0:e714fc19ba54 | 186 | break; |
wataaki | 0:e714fc19ba54 | 187 | default: |
wataaki | 0:e714fc19ba54 | 188 | break; |
wataaki | 0:e714fc19ba54 | 189 | } |
wataaki | 0:e714fc19ba54 | 190 | } |
wataaki | 0:e714fc19ba54 | 191 | } |