![](/media/cache/group/i2c_logo.png.50x50_q85.png)
8-bit A/D and D/A converter
Dependencies: mbed
PCF8591.cpp@0:74b27674f0d8, 2015-02-04 (annotated)
- Committer:
- wataaki
- Date:
- Wed Feb 04 06:12:52 2015 +0000
- Revision:
- 0:74b27674f0d8
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wataaki | 0:74b27674f0d8 | 1 | #include "mbed.h" |
wataaki | 0:74b27674f0d8 | 2 | #include "PCF8591.h" |
wataaki | 0:74b27674f0d8 | 3 | |
wataaki | 0:74b27674f0d8 | 4 | I2C i2c(p28,p27); // sda, scl |
wataaki | 0:74b27674f0d8 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
wataaki | 0:74b27674f0d8 | 6 | char cmd[32]; |
wataaki | 0:74b27674f0d8 | 7 | char i; |
wataaki | 0:74b27674f0d8 | 8 | |
wataaki | 0:74b27674f0d8 | 9 | void set_ch(char sel) |
wataaki | 0:74b27674f0d8 | 10 | { // PCA9541のサンプル |
wataaki | 0:74b27674f0d8 | 11 | // MST_0側の自分にスレーブ側の制御権を得る場合 |
wataaki | 0:74b27674f0d8 | 12 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:74b27674f0d8 | 13 | i2c.write( 0xe2, cmd, 1); // Cont Regを指定 |
wataaki | 0:74b27674f0d8 | 14 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:74b27674f0d8 | 15 | wait(0.1); // 0.1s待つ |
wataaki | 0:74b27674f0d8 | 16 | switch(cmd[0] & 0xf) |
wataaki | 0:74b27674f0d8 | 17 | { |
wataaki | 0:74b27674f0d8 | 18 | case 0: // bus off, has control |
wataaki | 0:74b27674f0d8 | 19 | case 1: // bus off, no control |
wataaki | 0:74b27674f0d8 | 20 | case 5: // bus on, no control |
wataaki | 0:74b27674f0d8 | 21 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:74b27674f0d8 | 22 | cmd[1] = 4; // bus on, has control |
wataaki | 0:74b27674f0d8 | 23 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:74b27674f0d8 | 24 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:74b27674f0d8 | 25 | break; |
wataaki | 0:74b27674f0d8 | 26 | case 2: // bus off, no control |
wataaki | 0:74b27674f0d8 | 27 | case 3: // bus off, has control |
wataaki | 0:74b27674f0d8 | 28 | case 6: // bus on, no control |
wataaki | 0:74b27674f0d8 | 29 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:74b27674f0d8 | 30 | cmd[1] = 5; // bus on, has control |
wataaki | 0:74b27674f0d8 | 31 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:74b27674f0d8 | 32 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:74b27674f0d8 | 33 | break; |
wataaki | 0:74b27674f0d8 | 34 | case 9: // bus on, no control |
wataaki | 0:74b27674f0d8 | 35 | case 0xc: // bus on, no control |
wataaki | 0:74b27674f0d8 | 36 | case 0xd: // bus off, no control |
wataaki | 0:74b27674f0d8 | 37 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:74b27674f0d8 | 38 | cmd[1] = 0; // bus on, has control |
wataaki | 0:74b27674f0d8 | 39 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:74b27674f0d8 | 40 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:74b27674f0d8 | 41 | break; |
wataaki | 0:74b27674f0d8 | 42 | case 0xa: // bus on, no control |
wataaki | 0:74b27674f0d8 | 43 | case 0xe: // bus off, no control |
wataaki | 0:74b27674f0d8 | 44 | case 0xf: // bus on, has control |
wataaki | 0:74b27674f0d8 | 45 | cmd[0] = 1; // PCA9541 コマンドコード Cont Reg |
wataaki | 0:74b27674f0d8 | 46 | cmd[1] = 1; // bus on, has control |
wataaki | 0:74b27674f0d8 | 47 | i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み |
wataaki | 0:74b27674f0d8 | 48 | i2c.read( 0xe2, cmd, 1); // Cont Regを読込み |
wataaki | 0:74b27674f0d8 | 49 | break; |
wataaki | 0:74b27674f0d8 | 50 | default: |
wataaki | 0:74b27674f0d8 | 51 | break; |
wataaki | 0:74b27674f0d8 | 52 | } |
wataaki | 0:74b27674f0d8 | 53 | |
wataaki | 0:74b27674f0d8 | 54 | cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled |
wataaki | 0:74b27674f0d8 | 55 | i2c.write( 0xe8, cmd, 1); // Send command string |
wataaki | 0:74b27674f0d8 | 56 | } |
wataaki | 0:74b27674f0d8 | 57 | |
wataaki | 0:74b27674f0d8 | 58 | int main () |
wataaki | 0:74b27674f0d8 | 59 | { |
wataaki | 0:74b27674f0d8 | 60 | i2c.frequency(100000); |
wataaki | 0:74b27674f0d8 | 61 | pc.printf("PCF8591T Sample Program\r\n"); |
wataaki | 0:74b27674f0d8 | 62 | |
wataaki | 0:74b27674f0d8 | 63 | set_ch(2); // PCF8591Tはch1に接続 |
wataaki | 0:74b27674f0d8 | 64 | |
wataaki | 0:74b27674f0d8 | 65 | // PCF8591T |
wataaki | 0:74b27674f0d8 | 66 | |
wataaki | 0:74b27674f0d8 | 67 | cmd[0] = 0x44; |
wataaki | 0:74b27674f0d8 | 68 | cmd[1] = 0x80; // SLEEP = 0 |
wataaki | 0:74b27674f0d8 | 69 | i2c.write(PCF8591_ADDR, cmd, 2); // cmd[0]Regにcmd[1]を書込み |
wataaki | 0:74b27674f0d8 | 70 | pc.printf("AOUT=%2x AIN0=%2x AIN1=%2x AIN2=%2x AIN3=%2x\r\n", i++, cmd[1], cmd[2], cmd[3], cmd[4]); // Cont Regを表示 |
wataaki | 0:74b27674f0d8 | 71 | |
wataaki | 0:74b27674f0d8 | 72 | while(1) |
wataaki | 0:74b27674f0d8 | 73 | { |
wataaki | 0:74b27674f0d8 | 74 | cmd[0] = 0x44 + 0x0; // DAC有効、オートインクリメント |
wataaki | 0:74b27674f0d8 | 75 | cmd[1] = i; // DAC値設定 0~0xFF |
wataaki | 0:74b27674f0d8 | 76 | i2c.write(PCF8591_ADDR, cmd, 2); // DAC値更新 |
wataaki | 0:74b27674f0d8 | 77 | |
wataaki | 0:74b27674f0d8 | 78 | i2c.read(PCF8591_ADDR, cmd, 5); // AD変換値を5バイト分取得 |
wataaki | 0:74b27674f0d8 | 79 | // AD変換値はCH0の前回データ、CH0~CH3の変換データ |
wataaki | 0:74b27674f0d8 | 80 | pc.printf("AOUT=%2x PREV=%2x AIN0=%2x AIN1=%2x AIN2=%2x AIN3=%2x\r\n", i++, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); // AD変換値を表示 |
wataaki | 0:74b27674f0d8 | 81 | wait(1.0); |
wataaki | 0:74b27674f0d8 | 82 | } |
wataaki | 0:74b27674f0d8 | 83 | } |