STM32G031のArduinoでの時計,DS1307で時刻表示OLED,OLED096UNO-A


stm32g031,stm32l010用に最軽量化したもの

x時間合わせは、別
xリセット(PA0)をGPIOとして使用、OBを2
xリセットを使用するため注意が必要

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

OLEDのライブラリーは、次のリンク参照
STM32G031J6M6のArduinoでOLED,OLED096UNO-AでABC defgを表示
https://qiita.com/caa45040/items/0e68df93773637a5f4f7


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

U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ PA0, /* data=*/ PB7, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display


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

#define ADDR    0x68  // 2進数 1101000

int     ii;           //ループカウンター
int     jj;           //ループカウンター
int     cursor1;      //カーソルの位置
char    data_read[8]; //i2cバッファー

//時間をoled表示形式に変換
char ch_hex_a_b[4];
char *ch_hex_a(int l_num)
{
    ch_hex_a_b[0] = '0' + (l_num >> 4);
    ch_hex_a_b[1] = '0' + (l_num & 0xf);
    ch_hex_a_b[2] = ':';
    ch_hex_a_b[3] = 0;
    return(ch_hex_a_b);
}

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

void setup()
{
  delay(3000);  //3秒待つ 消すな bugの場合ST-LINKを繋ぎ直すに必要

  //Wire.begin(sdaPin,sclPin); //STM32G031J6M6

  u8x8.begin();

  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);

  i2c_oled_w("START");
  delay(1000);

  //画面のクリア
  for(jj=0;jj<4;jj++){
    //              x    y   12345678
    u8x8.drawString( 0,jj*2,"        ");
  }//enf for
  //カーソルのクリア
  cursor1 = 0;


  Wire.begin(sdaPin,sclPin); //STM32G031J6M6
} //end setup

void loop()
{
  //行のクリア
  //              x    y   12345678
  //u8x8.drawString( 0,jj*2,"        ");
  //カーソルのクリア
  cursor1 = 0;


  // レジスタのアドレスを先頭にする
  Wire.beginTransmission(ADDR);
    Wire.write(0x00);
  Wire.endTransmission();

  // I2Cスレーブに8byteのレジスタデータを要求する
  Wire.requestFrom(ADDR, 8);

  // 8byteのデータを取得する
  for (ii = 0; ii < 8; ii++) {
    while (Wire.available() == 0 ) {}
    data_read[ii] = Wire.read();
  }//for

  //画面に表示
  i2c_oled_w(ch_hex_a(data_read[2]));
  i2c_oled_w(ch_hex_a(data_read[1]));
  ch_hex_a(data_read[0]);ch_hex_a_b[2]=0;
  i2c_oled_w(ch_hex_a_b);

  delay(1000);  //1秒待つ

}//loop


時間合わせ


#include <Wire.h> //I2C library

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

#define ADDR    0x68  // 2進数 1101000

char    data_read[8]; //i2cバッファー

#define HH  12
#define MM  34

void setup()
{
  Wire.begin(sdaPin,sclPin); //STM32G031J6M6

//i2c書き込み
  Wire.beginTransmission(ADDR);

  Wire.write((char) 0    ); //0 レジスターの位置は0
  Wire.write((char) 0    ); //1 秒
  Wire.write((char) ( MM /10)*16+( MM %10) ); //2 分
  Wire.write((char) ( HH /10)*16+( HH %10) ); //3 時

  Wire.write((char) 3    ); //4 曜日
  Wire.write((char) 1    ); //5 日
  Wire.write((char) 1    ); //6 月
  Wire.write((char) 0x20 ); //7 年

  Wire.endTransmission();

} //end setup

void loop()
{

} //loop