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桁)その2

Last updated at Posted at 2023-11-26

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

目的
(高速)温度センサーのデバッグ用
でも普通に使える

o_cop812.jpg

メイン



//STTS751_to_SER_2


//インクルド
#include "hh.h"
#include "i2c_run.h"


//定義
//オブジェクトの定義


 void NA_ST7735_P::setTextSize(uint8_t s) { }

 void NA_ST7735_P::setTextColor(uint16_t c) {  }

 void NA_ST7735_P::setCursor(int16_t x, int16_t y) {  }


 void NA_ST7735_P::println(void)
  { 
  Serial.println();
  } 

 void NA_ST7735_P::println(char *str1)
  {
      Serial.println(str1);
  }

 void NA_ST7735_P::print(char *str1)
  {
      Serial.print(str1);
  }

 void NA_ST7735_P::println(char ch)
  {
      Serial.println(ch);
  }

 void NA_ST7735_P::print(char ch)
  {
      Serial.print(ch);
  }

 void NA_ST7735_P::println(int al)
  {
      Serial.println(al);
  }

 void NA_ST7735_P::print(int al)
  {
      Serial.print(al);
  }

//実体の作成
NA_ST7735_P display;


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

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


  delay(3000); //not delete

  i2c_setup();

} //setup

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

  i2c_run(&display);

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

} //loop


hh.h



#define BLACK NA_ST7735_P_BLACK     ///< Draw 'off' pixels
#define WHITE NA_ST7735_P_WHITE     ///< Draw 'on' pixels
#define INVERSE NA_ST7735_P_INVERSE ///< Invert pixels

/// fit into the SSD1306_ naming scheme
#define NA_ST7735_P_BLACK 0   ///< Draw 'off' pixels
#define NA_ST7735_P_WHITE 1   ///< Draw 'on' pixels
#define NA_ST7735_P_INVERSE 2 ///< Invert pixels


//クラスの定義
struct NA_ST7735_P
{
  void setTextSize(uint8_t s);          //メソッドの宣言
  void setTextColor(uint16_t c);        //メソッドの宣言
  void setCursor(int16_t x, int16_t y); //メソッドの宣言

  void println(void); //メソッドの宣言

  void println(char *str1);//メソッドの宣言
  void print(char *str1);//メソッドの宣言

  void println(char ch);//メソッドの宣言
  void print(char ch);//メソッドの宣言

  void println(int al);//メソッドの宣言
  void print(int al);//メソッドの宣言

};



i2c_run.h





#include <Wire.h>

#define STTS751 0x39

void i2c_setup(){

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

}

void i2c_run(NA_ST7735_P *display_bk){

  NA_ST7735_P display = *(display_bk);

  //温度の読み込み
  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]=26;           //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;  

  // テキストサイズを設定
  display.setTextSize(3);
  // テキスト色を設定
  display.setTextColor(WHITE);
  // テキストの開始位置を設定
  display.setCursor(0, 0);

  // 1行目に46を表示
  display.println();
  display.println("-----------");
  display.println("temperature");
  display.println();
  display.print("   ");
  display.print(str1);
  display.print("C");
  display.println();
  display.println("-----------");

}//i2c_run






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?