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

Dependencies:   mbed

PCT2075.cpp

Committer:
wataaki
Date:
2015-02-04
Revision:
0:f0b188f7be09

File content as of revision 0:f0b188f7be09:

#include "mbed.h"
#include "PCT2075.h"
 
I2C i2c(p28,p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx
//LM75B tmp(p28,p27);
    char cmd[32];
int i;
short T;

void set_ch(char sel)
{    // PCA9541のサンプル
        // MST_0側の自分にスレーブ側の制御権を得る場合
    cmd[0] = 1;                     // PCA9541 コマンドコード Cont Reg
    i2c.write( 0xe2, cmd, 1);       // Cont Regを指定
    i2c.read( 0xe2, cmd, 1);        // Cont Regを読込み
    wait(0.1);                      // 0.1s待つ
    switch(cmd[0] & 0xf)
    {
    case 0:                         // bus off, has control
    case 1:                         // bus off, no control
    case 5:                         // bus on, no control
        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
        cmd[1] = 4;                 // bus on, has control
        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
        break;
    case 2:                         // bus off, no control
    case 3:                         // bus off, has control
    case 6:                         // bus on, no control
        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
        cmd[1] = 5;                 // bus on, has control
        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
        break;
    case 9:                         // bus on, no control
    case 0xc:                       // bus on, no control
    case 0xd:                       // bus off, no control
        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
        cmd[1] = 0;                 // bus on, has control
        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
        break;
    case 0xa:                       // bus on, no control
    case 0xe:                       // bus off, no control
    case 0xf:                       // bus on, has control
        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
        cmd[1] = 1;                 // bus on, has control
        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
        break;
    default:
        break;
    }

    cmd[0] = sel;                   // PCA9546 Cont Reg sel channel enabled
    i2c.write( 0xe8, cmd, 1);       // Send command string
}
  
int main ()
{
    i2c.frequency(100000);
    pc.printf("PCT2075 Sample Program\r\n");
    set_ch(1);              // PCT2075はch0に接続

  // PCT2075
    cmd[0] = Tidle;
    cmd[1] = 0x1f;          // 3.1s
//    cmd[1] = 0x0;           // 0.1s
    i2c.write(PCT2075_ADDR, cmd, 2); // Tidle = 0x1f = 3.1s
    pc.printf("Tidle = %2d \r\n", cmd[1]);

    while(1)
    {
        cmd[0] = Temp;
        i2c.write(PCT2075_ADDR, cmd, 1, true); // pointer = Temp
        i2c.read(PCT2075_ADDR, cmd, 2);        // Read Temp register
        T = (cmd[0]<<8) | cmd[1];           // calculate temperature
        pc.printf("%.2f ℃\r\n", T / 256.0);
        wait(1.0);
    }
}