LoginSignup
0
0

More than 3 years have passed since last update.

STM32F767のMbedでの時計,DS1307+で時刻表示(i2c)

Last updated at Posted at 2021-02-07

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


#include "mbed.h"
#include <stdio.h>

//Serial pc(USBTX, USBRX); // tx, rx
Serial pc(SERIAL_TX, SERIAL_RX);

#define ADDR     (0x68<<1) //  address

I2C i2c(I2C_SDA, I2C_SCL);

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

void ch_input()
{
    ch_i=0;
    while(1) {
        ch_y = pc.getc();
        if(ch_y == 13){ch_b[ch_i]=0;pc.printf("\n\r");break;}
        ch_b[ch_i]=ch_y;ch_i++;
        pc.putc(ch_y);
        if(ch_i >= 8){ch_b[ch_i]=0;pc.printf("\n\r");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);
}

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

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

int main() {

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

    pc.printf("\n\r");  //改行

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


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

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

    data_read[0]=0;
    data_read[1]=0;
    data_read[2]=a;
    data_read[3]=b;
    data_read[4]=3;    
    data_read[5]=1;
    data_read[6]=1;
    data_read[7]=0x20;

    p_val = i2c.write(ADDR, cmd, 1);
    p_val = i2c.write(ADDR, data_read, 8);

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

    while(1){

        p_val = i2c.write(ADDR, cmd, 1);
        p_val = i2c.read(ADDR | 1, data_read, 7);

        ii=2;
        while(1){
            pc.printf("%s",ch_hex(data_read[ii]));
            ii--;
            if(ii==-1){break;}else{pc.printf(":");}
        }
        pc.printf("\n\r");

        wait(1);

    } //while
}


H 0-23>19
M 0-59>18
19:18:00
19:18:01
19:18:02
19:18:03
19:18:04
19:18:05

i2c_time_1.jpg

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