STM32G031と温度,S-5851でOLED温度計 (Arduino)(AL12832AWWB)


x がんばつて　OLEDをリマップした。PA11-CLK,PA12-IO

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


SSD1306 128x32

有機ＥＬディスプレイ　１２８×３２　白色
[AL12832AWWB-H-U02]
通販コード　P-14686


![h_con19.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/c62b8b0c-4d7d-eb94-6cdc-bc71e9b4e198.jpeg)





https://akizukidenshi.com/catalog/g/gP-14686/



![h_con20.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/1144ed0c-dfeb-6b34-df1e-2fde650786dc.jpeg)




![h_con16.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/3aae1b85-f89c-ce16-e0dd-224300d1c491.jpeg)

データシートを確認して

![h_con17.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/0d71494c-1c2b-6d6b-21c6-fa28a961a0a8.jpeg)




```c++


#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h> //I2C library

U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ PA11, /* data=*/ PA12);   // pin remapping with ESP8266 HW I2C

//i2c温度センサーのアドレス
#define S5851A 0x48

int     tempval;      //温度
int     n0;           //温度　小数点以上
int     cursor1;      //カーソルの位置
char    data_read[8]; //i2cバッファー

void i2c_oled_w(char *s)
{
  //文字の表示
  u8x8.drawString(
  ((cursor1-0) & 0x07  )*2, // x
  (cursor1>>3)*2,           // y
  s);

  while(s[0] != 0) {
    s++;
    cursor1++;
  }

  //cursor1=cursor1 + 3; 
} //i2c_oled_w

void setup(void)
{
  //I2Cの初期化
  Wire.begin(PA12,PA11); //stm32g031

  //OLEDの初期化
  u8x8.begin();

  //フォントの設定
  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);

  //スタートロゴ
  cursor1 = 0;
  i2c_oled_w("START");
  delay(500);

  //画面の初期化
  u8x8.drawString( 0,0,"        ");
  u8x8.drawString( 0,2,"        ");
  cursor1 = 0;

}//setup

void loop(void)
{
  //カーソルのクリア
  cursor1 = 0;

  //0番目のレジスター
  Wire.beginTransmission(S5851A);
  Wire.write(0);
  Wire.endTransmission();
  delay(1);

  //温度の読み込み
  tempval = 99;
  Wire.requestFrom(S5851A, 1);
  while(Wire.available())  {    // 要求より短いデータが来る可能性あり
    tempval = (int)Wire.read(); // 1バイトを受信
  }//while
  delay(1);

  //温度をセット
  int n0 = tempval;

  //n0=20; //debug

  //画面に表示
  i2c_oled_w("TMP=");
  data_read[0] = '0' + n0/10;;
  data_read[1] = '0' + (  n0-(  (n0/10)  *10)  );
  data_read[2] = '.';
  data_read[3] = '0' + 0;
  data_read[4] = 0; 
  i2c_oled_w(data_read);  
 
  //1秒待つ
  delay(1000);

}//loop




```


いろいろ改良前

```c++


#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h> //I2C library

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif

//U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED
//U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ 21, /* data=*/ 20, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather M0 Basic Proto + FeatherWing OLED
//U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ PA11, /* data=*/ PA12, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather M0 Basic Proto + FeatherWing OLED
//U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);   // Adafruit ESP8266/32u4/ARM Boards + FeatherWing OLED
//U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ SCL, /* data=*/ SDA);   // pin remapping with ESP8266 HW I2C
//U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ 15, /* data=*/ 14);   // pin remapping with ESP8266 HW I2C
//U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ PB6, /* data=*/ PB7);   // pin remapping with ESP8266 HW I2C
//U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ PB6, /* data=*/ PB7);   // pin remapping with ESP8266 HW I2C

U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ PA11, /* data=*/ PA12);   // pin remapping with ESP8266 HW I2C

//U8X8_SSD1306_128X32_WINSTAR_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ SCL, /* data=*/ SDA);   // pin remapping with ESP8266 HW I2C
//U8X8_SSD1306_128X32_WINSTAR_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE, /* clock=*/ PA9, /* data=*/ PA10);   // pin remapping with ESP8266 HW I2C


void setup(void)
{
  Wire.begin(PA12,PA11); //uno
  
  /* U8g2 Project: SSD1306 Test Board */
  //pinMode(10, OUTPUT);
  //pinMode(9, OUTPUT);
  //digitalWrite(10, 0);
  //digitalWrite(9, 0);		
  
  /* U8g2 Project: KS0108 Test Board */
  //pinMode(16, OUTPUT);
  //digitalWrite(16, 0);	
  
  u8x8.begin();

  //Wire.begin(); //uno
  //Wire.begin(PA12,PA11); //STM32G031J6M6
}

#define S5851A 0x48
int     tempval;                //温度
char    data_read[3];           //バッファー

void loop(void)
{
  //0番目のレジスター
  Wire.beginTransmission(S5851A);
  Wire.write(0);
  Wire.endTransmission();
  delay(1);

//  //温度の読み込み
  tempval = 99;
  Wire.requestFrom(S5851A, 1);
  while(Wire.available())  {    // 要求より短いデータが来る可能性あり
    tempval = (int)Wire.read(); // 1バイトを受信
  }//while
  delay(1);

  //表示
  data_read[0] = '0' + (tempval / 10);
  data_read[1] = '0' + (tempval % 10);
  data_read[2] = 0;
  
  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);
  //u8x8.drawString(0, 0, "ABC defg");

  u8x8.drawString(0, 0, data_read);

  //u8x8.setFont(u8x8_font_px437wyse700b_2x2_r);
  //u8x8.drawString(0, 2, "ABC defg");
  
  u8x8.refreshDisplay();		// only required for SSD1606/7  
  delay(2000);
}




```