LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031J6M6のArduinoでの温度,STTS751で温度シリアル出力(i2c)

Last updated at Posted at 2021-02-16

x 愛用していたSTTS751の入手が難しくなったので代用品は、S-5851A。(おもにアドレスの違い)

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

x書き込みがうまくいかなくなる場合があるので上級者用
xSTM32G031J6M6のシリアル通信については、下記のリンク参照
STM32G031のArduinoでのシリアル通信(STM32G031J6M6)
https://qiita.com/caa45040/items/8b77fe33e80a54b47c3d
x電源を入れる手順を間違えるとうまく動かない 本体が先、シリアルが後
x再度リコンパイルで再起動させている


#include <Wire.h> //I2C library

//STM32G031J6M6
#define sdaPin PA12    // ArduinoA4
#define sclPin PA11    // ArduinoA5

// 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 lo_on = 1; // 1=on 0=off

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

void setup()
{  
    delay(3000); //not delete
    //Wire.begin(); // initialise the connection
    Wire.begin(sdaPin,sclPin); //STM32G031J6M6
    
    Serial.begin(9600);
    
    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);
}


i2c_stts751_031_j_1.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0