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