LoginSignup
0
0

More than 1 year has passed since last update.

STM32G071で時計。(DS1307)(シリアル出力)(NUCLEO-G071RB)

Posted at

目的
DS1307のテスト

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

o_con799.jpg

o_con800.jpg

参考

STM32F767のプログラムと完全に同じ




#include <Wire.h>

// デバイスアドレス(スレーブ)
#define ADDR    0x68    // 2進数 1101000

int u_key;      // 受信データを格納するchar型の変数
int ch_in_key() {

  // 受信データがあった時だけ、処理を行う
  while(!Serial.available()) {}       // 受信データがあるか?
  u_key = Serial.read();            // 1文字だけ読み込む

  return(u_key);
}

int  ch_i;      //文字列のループカウンター
char ch_b[8+1]; //文字列のバッファー
int  ch_y;      //一文字分の一時バッファー

void ch_input()
{
    ch_i=0;
    while(1) {
        ch_y = ch_in_key();
        if(ch_y == 13){ch_b[ch_i]=0;Serial.println();break;}
        ch_b[ch_i]=ch_y;ch_i++;
        Serial.write( ch_y );
        if(ch_i >= 8){ch_b[ch_i]=0;Serial.println();break;}     
    } //while
} //ch_input

char ch_hex_b[3];
char *ch_hex(int l_num)
{
    ch_hex_b[0] = '0' + (l_num >> 4);
    ch_hex_b[1] = '0' + (l_num & 0xf);
    ch_hex_b[2] = 0;
    return(ch_hex_b);
}

void setup() {
  Serial.begin( 9600 );     // シリアル通信を初期化する。通信速度は9600bps

  // マスタとしてI2Cバスに接続する
  Wire.begin();

}

int h;
int m;
int a;
int b;

int p_val;
char data_read[16];
char cmd[2]={0,0};
int ii;     //ループカウンタ

void loop() {

///*INIT start *********  

    Serial.println( "" );  //改行

    Serial.print("H 0-23>");
    ch_input();         //文字列の入力
    h=atoi(ch_b);

    Serial.print("M 0-59>");
    ch_input();         //文字列の入力
    m=atoi(ch_b);

    a=(m/10)*16+(m%10);
    b=(h/10)*16+(h%10);

      // I2Cスレーブに対して送信処理を開始する
  Wire.beginTransmission(ADDR);

    // レジスタのアドレスを指定する(0-63の内、先頭から)
    Wire.write(0x00);

    // ---------------------------------
    //  タイマーキーパーレジスタ
    // ---------------------------------
    // 2進化10進数(BCD)で指定します。(03以外)

    // [00]Seconds(45秒)
    Wire.write(0);
    // [01]Minutes(59分)
    Wire.write(a);
    // [02]Hours(23時)
    Wire.write(b);
    // [03]Day(月)
    // 1:日 2:月 3:火 4:水 5:木 6:金 7:土
    Wire.write(3);
    // [04]Date(31日)
    Wire.write(1);
    // [05]Month(12月)
    Wire.write(1);
    // [06]Years(2017年)
    Wire.write(0x20);

  // I2Cスレーブへの送信完了
  Wire.endTransmission();

//*/  //INIT end *********

while(1){

  // レジスタのアドレスを先頭にする
  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();
  }


        ii=2;
        while(1){
            Serial.print(ch_hex(data_read[ii]));
            ii--;
            if(ii==-1){break;}else{Serial.print(":");}
        }
        Serial.println("");


  delay(1000);

}//while

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