0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

STTS751温度センサーの値をシリアルポートに出力する。(STM32G031)(小数点2桁)

Last updated at Posted at 2023-11-24

STTS751温度センサーの値をシリアルポートに出力する。(STM32G031)(小数点2桁)

目的

前後するがカラー液晶用
8ピンのマイコンで6GPIOは、
書き込みが難しいので
デバッグの回数を減らす為に作成

o_cop813.jpg


●結果

o_cop807.jpg

o_cop808.jpg


●プログラム




//STTS751_to_SER_1


//インクルド
#include <Wire.h>

//定義
#define STTS751 0x39


//初期化処理
void setup()
{

  delay(3000); //not delete

  //シリアルの初期化
  Serial.setTx(PA2_ALT1);
  Serial.setHalfDuplex();
  Serial.begin(9600);


  //i2cの初期化
  Wire.setSDA(PA12);
  Wire.setSCL(PA11);
  Wire.begin(); //pa12 pa11

} //setup

//メインループ
void loop()
{

  //温度の読み込み
  char s1[2] = {0,0}; //センサーの値
  char s2[2] = {0,0}; //センサーの値
  int ii=0;

  //0番目のレジスター 温度整数部
  Wire.beginTransmission(STTS751);
  Wire.write(0);
  Wire.endTransmission();
  delay(1);
  //1
  Wire.requestFrom(STTS751, 1);
  s1[0]=99;
  while(Wire.available())  {    // 要求より短いデータが来る可能性あり
    s1[ii++] = (int)Wire.read();       // 1バイトを受信
  }//while
  delay(1);

  //2番目のレジスター 温度小数部
  Wire.beginTransmission(STTS751);
  Wire.write(2);
  Wire.endTransmission();
  delay(1);
  //2
  ii=0;
  Wire.requestFrom(STTS751, 1);
  s2[0]=0xc0;
  while(Wire.available())  {    // 要求より短いデータが来る可能性あり
    s2[ii++] = (int)Wire.read();       // 1バイトを受信
  }//while

  //s1[0]=27;           //debug
  //s1[1]=0x12;         //debug
  //s2[0]=0x00; //0.0   //debug
  //s2[0]=0x40; //0.25  //debug
  //s2[0]=0x80; //0.5   //debug
  //s2[0]=0xc0; //0.75  //debug

  //配列の定義
  char str1[]={'2','6','.','5','0',0};
  char ti1[]={'0','2','5','7'};
  char ti2[]={'0','5','0','5'};

  //変換処理 たぶん難しいのでわかる人に聞いて!!
  //小規模マイコン用のリッチでは、ない処理
  //DIV10は、10の割り算処理
  //ex  (19*204.8)/2048=1.9
  //仮に (19*205  )/2048=1 (小数点以降切り捨て)
  //余りは、10で割って10掛けて引けばいい
  //ex 19/10=1 1*10=10 19-10=9 (小数点以降切り捨て)
  //2進固定小数点は、1/2,1/4,1/8,...となる
  //8ビットを6回シフトして11Bでマスクすると1/2,1/4が
  //取り出せて、表引きしている。
  //情報系の大学で習う大人な処理
#define DIV10(xx) ((xx*205)>>11)
  str1[0]='0'+DIV10(s1[0]);
  if(str1[0]=='0'){str1[0]=' ';}
  str1[1]='0'+s1[0]-(DIV10(s1[0])*10);
  str1[2]='.';
  str1[3]=ti1[(s2[0]>>6)&3];
  str1[4]=ti2[(s2[0]>>6)&3];
  str1[5]=0;

  //温度の表示
  Serial.print(str1);
  Serial.println();

  //1秒の待ち
  delay(1000);

} //loop




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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?