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


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
#define ADDR_LCD      (0x7C) //  address

I2C i2c(I2C_SDA, I2C_SCL);


char ch_b[8+1]; //文字列のバッファー
char *ch_input()
{
    int  ch_i=0;    //文字列のループカウンター
    int  ch_y;      //一文字分の一時バッファー
    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
    return(ch_b);
} //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);
}

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


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

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

char INIT_com[]={0x0,0x38,
0x0,0x39,
0x0,0x4,
0x0,0x14,
0x0,0x70,
0x0,0x56,
0x0,0x6C,
0x0,0x38,
0x0,0xC,
0x0,0x1,
0x40,0x41,0x99,0x99};

char INIT_cls[]={0x0,0x1};


int main() {

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

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

    pc.printf("H 0-23>");

    for(ii=0;ii<5000;ii++){
        wait_ms(1);
        if(pc.readable() == 1) {break;}
    }//for
    if(ii == 5000){pc.printf("\n\r");}
    if(ii != 5000) { //jump bbb

    h=atoi( ch_input() ); //文字列の入力
    if(h != 99 ) { //jump aaa

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

    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, data_read, 8);
    } //jump aaa
    } //jump bbb

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

    for(ii=0;ii<11;ii++){
        //pc.printf("%x %x \n\r",INIT_com[ii*2],INIT_com[(ii*2)+1]); //debug     
        p_val = i2c.write(ADDR_LCD, &INIT_com[ii*2], 2);
    } //for


    while(1){

        //pc.printf("INIT_cls:");
        p_val = i2c.write(ADDR_LCD,INIT_cls,2);

        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]));
            p_val = i2c.write(ADDR_LCD, ch_hex_a(data_read[ii]) ,3);
            ii--;
            if(ii==-1){break;}
            else{
                pc.printf(":");
                p_val = i2c.write(ADDR_LCD,"@:",2);
            }
        }
        pc.printf("\n\r");
        //p_val = i2c.write(ADDR_LCD,INIT_cls,2);

        wait(1);

    } //while
}