LM75B I2C temperature sensor
Dependencies: mbed
LM75B.cpp
00001 #include "mbed.h" 00002 #include "stdint.h" 00003 #include "LM75B.h" 00004 00005 I2C i2c(p28,p27); // sda, scl 00006 Serial pc(USBTX, USBRX); // tx, rx 00007 char cmd[32]; 00008 int i; 00009 short T; 00010 00011 void set_ch(char sel) 00012 { // PCA9541のサンプル 00013 // MST_0側の自分にスレーブ側の制御権を得る場合 00014 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00015 i2c.write( 0xe2, cmd, 1); // Cont Regを指定 00016 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00017 wait(0.1); // 0.1s待つ 00018 switch(cmd[0] & 0xf) 00019 { 00020 case 0: // bus off, has control 00021 case 1: // bus off, no control 00022 case 5: // bus on, no control 00023 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00024 cmd[1] = 4; // bus on, has control 00025 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00026 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00027 break; 00028 case 2: // bus off, no control 00029 case 3: // bus off, has control 00030 case 6: // bus on, no control 00031 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00032 cmd[1] = 5; // bus on, has control 00033 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00034 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00035 break; 00036 case 9: // bus on, no control 00037 case 0xc: // bus on, no control 00038 case 0xd: // bus off, no control 00039 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00040 cmd[1] = 0; // bus on, has control 00041 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00042 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00043 break; 00044 case 0xa: // bus on, no control 00045 case 0xe: // bus off, no control 00046 case 0xf: // bus on, has control 00047 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg 00048 cmd[1] = 1; // bus on, has control 00049 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み 00050 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み 00051 break; 00052 default: 00053 break; 00054 } 00055 00056 cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled 00057 i2c.write( 0xe8, cmd, 1); // Send command string 00058 } 00059 00060 int main () 00061 { 00062 i2c.frequency(100000); 00063 pc.printf("LM75B Sample Program\r\n"); 00064 set_ch(2); // LM75Bはch1に接続 00065 00066 // LM75B 00067 while(1) 00068 { 00069 cmd[0] = Temp; 00070 i2c.write(LM75_ADDR, cmd, 1, true); // pointer = Temp 00071 i2c.read(LM75_ADDR, cmd, 2); // Read Temp register 00072 T = (cmd[0]<<8) | cmd[1]; // calculate temperature 00073 pc.printf("%.2f ℃\r\n", T / 256.0); 00074 wait(2.0); 00075 } 00076 } 00077 00078
Generated on Mon Jul 18 2022 07:29:08 by 1.7.2