I2C-bus Fm+, 1 degree C accuracy, digital temperature sensor and thermal watchdog

Dependencies:   mbed

Committer:
wataaki
Date:
Wed Feb 04 06:14:12 2015 +0000
Revision:
0:f0b188f7be09
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wataaki 0:f0b188f7be09 1 #include "mbed.h"
wataaki 0:f0b188f7be09 2 #include "PCT2075.h"
wataaki 0:f0b188f7be09 3
wataaki 0:f0b188f7be09 4 I2C i2c(p28,p27); // sda, scl
wataaki 0:f0b188f7be09 5 Serial pc(USBTX, USBRX); // tx, rx
wataaki 0:f0b188f7be09 6 //LM75B tmp(p28,p27);
wataaki 0:f0b188f7be09 7 char cmd[32];
wataaki 0:f0b188f7be09 8 int i;
wataaki 0:f0b188f7be09 9 short T;
wataaki 0:f0b188f7be09 10
wataaki 0:f0b188f7be09 11 void set_ch(char sel)
wataaki 0:f0b188f7be09 12 { // PCA9541のサンプル
wataaki 0:f0b188f7be09 13 // MST_0側の自分にスレーブ側の制御権を得る場合
wataaki 0:f0b188f7be09 14 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:f0b188f7be09 15 i2c.write( 0xe2, cmd, 1); // Cont Regを指定
wataaki 0:f0b188f7be09 16 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:f0b188f7be09 17 wait(0.1); // 0.1s待つ
wataaki 0:f0b188f7be09 18 switch(cmd[0] & 0xf)
wataaki 0:f0b188f7be09 19 {
wataaki 0:f0b188f7be09 20 case 0: // bus off, has control
wataaki 0:f0b188f7be09 21 case 1: // bus off, no control
wataaki 0:f0b188f7be09 22 case 5: // bus on, no control
wataaki 0:f0b188f7be09 23 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:f0b188f7be09 24 cmd[1] = 4; // bus on, has control
wataaki 0:f0b188f7be09 25 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:f0b188f7be09 26 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:f0b188f7be09 27 break;
wataaki 0:f0b188f7be09 28 case 2: // bus off, no control
wataaki 0:f0b188f7be09 29 case 3: // bus off, has control
wataaki 0:f0b188f7be09 30 case 6: // bus on, no control
wataaki 0:f0b188f7be09 31 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:f0b188f7be09 32 cmd[1] = 5; // bus on, has control
wataaki 0:f0b188f7be09 33 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:f0b188f7be09 34 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:f0b188f7be09 35 break;
wataaki 0:f0b188f7be09 36 case 9: // bus on, no control
wataaki 0:f0b188f7be09 37 case 0xc: // bus on, no control
wataaki 0:f0b188f7be09 38 case 0xd: // bus off, no control
wataaki 0:f0b188f7be09 39 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:f0b188f7be09 40 cmd[1] = 0; // bus on, has control
wataaki 0:f0b188f7be09 41 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:f0b188f7be09 42 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:f0b188f7be09 43 break;
wataaki 0:f0b188f7be09 44 case 0xa: // bus on, no control
wataaki 0:f0b188f7be09 45 case 0xe: // bus off, no control
wataaki 0:f0b188f7be09 46 case 0xf: // bus on, has control
wataaki 0:f0b188f7be09 47 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:f0b188f7be09 48 cmd[1] = 1; // bus on, has control
wataaki 0:f0b188f7be09 49 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:f0b188f7be09 50 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:f0b188f7be09 51 break;
wataaki 0:f0b188f7be09 52 default:
wataaki 0:f0b188f7be09 53 break;
wataaki 0:f0b188f7be09 54 }
wataaki 0:f0b188f7be09 55
wataaki 0:f0b188f7be09 56 cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled
wataaki 0:f0b188f7be09 57 i2c.write( 0xe8, cmd, 1); // Send command string
wataaki 0:f0b188f7be09 58 }
wataaki 0:f0b188f7be09 59
wataaki 0:f0b188f7be09 60 int main ()
wataaki 0:f0b188f7be09 61 {
wataaki 0:f0b188f7be09 62 i2c.frequency(100000);
wataaki 0:f0b188f7be09 63 pc.printf("PCT2075 Sample Program\r\n");
wataaki 0:f0b188f7be09 64 set_ch(1); // PCT2075はch0に接続
wataaki 0:f0b188f7be09 65
wataaki 0:f0b188f7be09 66 // PCT2075
wataaki 0:f0b188f7be09 67 cmd[0] = Tidle;
wataaki 0:f0b188f7be09 68 cmd[1] = 0x1f; // 3.1s
wataaki 0:f0b188f7be09 69 // cmd[1] = 0x0; // 0.1s
wataaki 0:f0b188f7be09 70 i2c.write(PCT2075_ADDR, cmd, 2); // Tidle = 0x1f = 3.1s
wataaki 0:f0b188f7be09 71 pc.printf("Tidle = %2d \r\n", cmd[1]);
wataaki 0:f0b188f7be09 72
wataaki 0:f0b188f7be09 73 while(1)
wataaki 0:f0b188f7be09 74 {
wataaki 0:f0b188f7be09 75 cmd[0] = Temp;
wataaki 0:f0b188f7be09 76 i2c.write(PCT2075_ADDR, cmd, 1, true); // pointer = Temp
wataaki 0:f0b188f7be09 77 i2c.read(PCT2075_ADDR, cmd, 2); // Read Temp register
wataaki 0:f0b188f7be09 78 T = (cmd[0]<<8) | cmd[1]; // calculate temperature
wataaki 0:f0b188f7be09 79 pc.printf("%.2f ℃\r\n", T / 256.0);
wataaki 0:f0b188f7be09 80 wait(1.0);
wataaki 0:f0b188f7be09 81 }
wataaki 0:f0b188f7be09 82 }
wataaki 0:f0b188f7be09 83