STM32F767のArduinoでの温度,STTS751で温度表示


1.SCLとSDAを接続、プルアップも忘れずに
2.電源の接続
3.下記のソースコードを書き込む
4.コンパイル実行で表示されたら終了
5.おわり


#include <Wire.h> //I2C library

// STTS751のドライブは、下記のアドレスを参考にした。
// mike.saunby.net/2013_03_01_archive.html

// I2C temperature sensor - see table 1 of data sheet.  Resistor selects address. 
#define STTS751 0x39

//
byte i2c_sensor_read_byte( int deviceaddress, int eeaddress ) {
  byte rdata = 0xFF;
  int rc;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)eeaddress);
  rc = Wire.endTransmission(false);
  Wire.requestFrom(deviceaddress, 1);
  if (Wire.available()){
    rdata = Wire.read();
  }
  if(rc != 0){
    //Serial.print("Error ");
    //Serial.println(rc);
  } 
  return rdata;
}

int led     = 16; //adc2
int led_gnd = 17; //adc3

int lo_on = 1; // 1=on 0=off

//プロトタイプ宣言
void stts751_init();

void setup()
{  
    Wire.begin(); // initialise the connection
    Serial.begin(9600);
    //Serial.println("Setup done");

   // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  pinMode(led_gnd, OUTPUT);     

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(led_gnd, LOW);    // turn the LED off by making the voltage LOW

  stts751_init();


} //end setup

void stts751_init()
{
    //4回読み捨てる
    int loop_i;
    for(loop_i=1;loop_i<=4;loop_i++){
      byte lo;
      signed char hi;
      hi = i2c_sensor_read_byte(STTS751, 0);
      if(lo_on == 1 ) {
        lo = i2c_sensor_read_byte(STTS751, 2);
      }
      delay(500);
    } //end for    
} // stts751_init


int stts751_run()
{

    byte lo;
    signed char hi;
    float temperature;

    //Serial.println("satart");

    // read temperature
    hi = i2c_sensor_read_byte(STTS751, 0);
    lo = 0;
    if(lo_on == 1) {
      lo = i2c_sensor_read_byte(STTS751, 2);
    }
    if( hi > 0){ 
      temperature = hi + lo * 1.0/256.0;
    }else{
      temperature = hi - lo * 1.0/256.0;
    }
    //Serial.print(temperature);
    //Serial.println(" ");
    //delay(1000);

    int ret_q;
    ret_q = temperature * 100;
    return(ret_q);
}


void loop()
{
    //stts751 start 
    int stts751_tmp1 = 0;
    float temperature;
    int hi;
    int lo;
    //stts751 end

    //stts751 start  
    stts751_tmp1 = stts751_run();
    temperature = stts751_tmp1 /100.0;
    hi =  stts751_tmp1 >> 8;
    lo =  stts751_tmp1 & 0xFF;
    //stts751 end

    //stts751 start  
    Serial.print(temperature);
    Serial.println(" ");
    //stts751 end

    delay(1000);
}