LoginSignup
0
0

More than 1 year has passed since last update.

STM32L010でI2C EEPROで遊ぶ1

Posted at

目的
I2C EEPROMのテスト

使用ハード

IchigoJam用外部記憶装置キット IchigoROM
[AE-IchigoROM]
通販コード K-12061
発売日 2017/08/23

I2C アドレス 0x50 と 0x51(2バンク目)

o_con576.jpg

o_con586.jpg

読み取り




//SER_I2C_EEPROM_read1_010_1

#include "mbed.h"

I2C i2c(PA_10, PA_9); //010

//#define S5851A 0x48
#define ADDR        ((0x50)<<1)   //  address


//四桁の変換
//#define DIV10(n) (((n*409)+((n*154)>>8))>>12)
int DIV10(int n)
{
    return    (((n*409)+((n*154)>>8))>>12)    ;
}
char data[16];
char *itoa9999(int d)
{

    data[4] = 0;
    data[3] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
    d = DIV10(d);
    data[2] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
    d = DIV10(d);
    data[1] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
    data[0] = '0' +  DIV10(d);                  // '0'+(d/10)

    return (data);

}//itoa99

//仮想シリアルへの一文字出力 9600bps
DigitalOut TX(PA_2);
//DigitalIn  RX(PA_3);
#define UART_DELAY 101 //  1/9600 98-105
int pc_putc(char ch)
{
    TX=1;
    TX=0;//Start
    wait_us(UART_DELAY);

    for(int ii=0; ii<8; ii++) { //Data x 8
        TX=(ch>>ii)&1;
        wait_us(UART_DELAY);
    }; //for

    TX=1;//Stop
    wait_us(UART_DELAY);

    return(0);
} //pc_putc


//文字列の表示 nana_seg
int pc_puts(char *str1)
{
    //文字の中身がゼロか
    while(*str1) {
        pc_putc( *str1 ++);
    } //while

    //戻り値
    return(0);
}//pc_puts

int main()
{

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

    char ad_hl[2];

    pc_puts("\r\n<START>\r\n");

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

        pc_puts("for 0 to 15\r\n");

        for(int ii = 0; ii<16; ii++) {

            //アドレス
            //set address
            ad_hl[0] =    ii >> 8   ;
            ad_hl[1] =    ii & 0xff ;
            //pc_puts( itoa9999(ad_hl[0]) );
            //pc_puts( "#" );
            //pc_puts( itoa9999(ad_hl[1]) );
            //pc_puts( " " );
            i2c.write(ADDR, ad_hl, 2); //addres 0

            //データの読み込み
            // Read temperature register
            i2c.read(ADDR, data_read,1,false);
            //data_read[0] = 123;
            pc_puts( itoa9999(data_read[0]) );
            pc_puts("\r\n");

            //1秒待つ
            wait_ms(1000);

        }//for

    }//while

}//main

//容量削減
void error(const char* format, ...) {}



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