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?

I2C温度センサー S-5851A で温度計を作る。(printf文バージョン)ILI9341

Last updated at Posted at 2025-07-02

参考

いろいろ注意

  • 過去ログを見よ

結果

image_original(50).jpg

o_cop890.jpg

プログラム




//ili9341_s5851__printf_071_1

//インクルド
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>


//センサーのアドレス
#define I2C_ADD 0x48

//設定
#define TFT_RST 54  //A1
#define TFT_DC 3
#define TFT_CS 4

//液晶の定義
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

#define D_NUM 1

#if D_NUM == 1
#define P_V 10
#endif

#if D_NUM == 2
#define P_V 100
#endif

#if D_NUM == 3
#define P_V 1000
#endif

#if D_NUM == 4
#define P_V 10000
#endif

//初期化
void setup() {

  //シリアルの初期化
  Serial.begin(9600);
  Serial.println("ILI9341 Test!");

  //I2Cの初期化
  Wire.begin();

  //液晶の初期化
  tft.begin();

  //画面のクリア
  tft.fillScreen(ILI9341_BLACK);

}  //setup


//サブアドレス指定で2バイト読み込み
unsigned short Read2(int sub_add) {

  //サブアドレスの書き込み
  Wire.beginTransmission(I2C_ADD);  // transmit to device I2C_ADD
  Wire.write(sub_add);              // sends one byte
  Wire.endTransmission();           // stop transmitting

  //2バイト読み込み
  Wire.requestFrom(I2C_ADD, 2);                          // request 2 bytes from slave device I2C_ADD
  unsigned short re = (Wire.read() << 8) | Wire.read();  // receive 2 byte as character

  return re;
}  //Read2


//無限ループ
void loop(void) {

  //読み込み
  unsigned short re = Read2(0);

  //re = 0xE700; //-25
  //re = 0x1900;   //25
  //re = 0;
  //re = 0xfff0;   //-0.0625
  //re = 0xffc0;  //-0.250
  //re = 0xff00;   //-1.00
  //re = 0x1940;   //25.25

  //読み込みデータの出力 debug
  //Serial.println( re  , HEX); //debug

  //4ビットシフトして有効12ビットを取り出す。
  re = (re >> 4) & 0x0fff;

  //符号あり温度変数に変換する
  long tmp = re - ((re & 0x800) << 1);
  //Serial.println(tmp, HEX);  //debug
  //Serial.println( tmp ); //debug

  //2進固定小数点4ビットを10進固定小数点1桁
  tmp = (tmp * 625) / 1000;

  //温度の表示

  //クリア
  tft.fillRect(0, 0, 24 * 6, 24, ILI9341_BLACK);

  //表示位置を0,0にする
  tft.setCursor(0, 0);

  //テキストの表示
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);

  tft.print(" ");

  //負の数の時で0からnumの時の処理
  if ((tmp < 0) && (tmp > (-(P_V)))) {
    tft.printf("-0.%0*d\n", D_NUM, abs(tmp % P_V));
  } else {
    tft.printf("%d.%0*d\n", tmp / P_V, D_NUM, abs(tmp % P_V));
  }

  delay(1000);  //1秒待つ

}  //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?