STM32F103C8のMbedでの時計,DS1307で時刻表示OLED,OLED096UNO-A


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

x時間合わせは、別
x大幅に説明を省略

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

忙しい人よう
https://os.mbed.com/users/caa45040/code/i2c_oled_ds1307_103_1/



#include "mbed.h"
#include "SSD1306.h"

#define ADDR_OLED       (0x3c<<1) //  address
//#define ADDR_OLED     (0x3d<<1) //  address
#define ADDR        (0xD0)   //  address

//I2C i2c(I2C_SDA, I2C_SCL);
I2C i2c(PB_7, PB_6); // STM32F103C8

// Host PC Communication channels
//Serial pc(USBTX, USBRX); // tx, rx
Serial pc(PA_9, PA_10); // tx, rx 103

// Instantiate OLED
SSD1306 oled(i2c, ADDR_OLED);

int ii; //ループカウンター
int jj; //ループカウンター
int cursor1; //文字カウンター

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

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)
{
    ii=0;
    while(1){
        if(s[ii] == 0) break;
        //                y   x
        oled.writeBigChar(0, cursor1*16,s[ii]);
        cursor1=cursor1 + 1;
        ii=ii+1;
    } //for
} //i2c_oled_w

int main() {       
    pc.baud(9600);    
    //oled.writeString(0, 0, "Hello World !");  

    char n1_8[] = "12345678";

    ii=0;
    for(jj=0;jj<8;jj++){
        //                y   x
        oled.writeBigChar(0, ii*16,n1_8[jj]);
        ii = ii + 1;
    } //for
    pc.printf("Printed something\r");    
    wait_ms(500);

    //for(ii=0;ii<8;ii++){
    //    oled.writeString(ii, 0, "Hello World !"); 
    //}
    //wait_ms(1000);

    while(1){

        //液晶のクリア
        cursor1 = 0;      

        //時間の読み込み
        i2c.write(ADDR, "\000", 1);
        i2c.read(ADDR | 1, data_read, 7);

        //画面に表示
        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);

        wait_ms(1000);  

    } //while
}



時間合わせ



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

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

#define ADDR        (0xD0)  //  address
#define ADDR_LCD    (0x7C)  //  address

//I2C i2c(I2C_SDA, I2C_SCL); //767
//I2C i2c(dp5, dp27); //1114
//I2C i2c(PA_10, PA_9); //010
I2C i2c(PB_7, PB_6); // STM32F103C8

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

#define HH  12
#define MM  34

int main() {

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

    data_read[0]=0;
    data_read[1]=0;
    data_read[2]= ( MM /10)*16+( MM %10);
    data_read[3]= ( HH /10)*16+( HH %10);
    data_read[4]=3;    
    data_read[5]=1;
    data_read[6]=1;
    data_read[7]=0x20;

    i2c.write(ADDR, data_read, 8);

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

    while(1){} //無限ループ

} // main