0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Arduino I2C シリアルEEPROMの利用

Last updated at Posted at 2023-05-18

概要

ディスプレイ付きで便利なマイコン「Waveshare RP2040-LCD-0.96」ですが、ちょっとしたデータを
保存するためのEEPROMが付属していません。そこでI2CシリアルEEPROMを利用します。
いろいろ調べたところ便利そうなライブラリがありましたので活用させて頂きました。
float型やString型でも簡単なコードで書き込みができました。

スクリーンショット 2023-05-18 131014.png

環境

マイコン:Waveshare RP2040-LCD-0.96
EEPROM型番:24LC512-I/P
エディタ:VisualStudioCode(拡張機能:PlatformIO IDE)
ライブラリはPlatformIOで以下の3つをインストール
 1. SparkFun_External_EEPROM
 2. U8g2_for_Adafruit_GFX
 3. Adafruit ST7735 and ST7789 Library

配線図

スクリーンショット 2023-05-18 133118.png

コード

#include <Arduino.h>
#include <Adafruit_ST7735.h>

#include <SPI.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <Wire.h>
#include "SparkFun_External_EEPROM.h"
ExternalEEPROM myMem;

//EEPROMデバイスアドレス
#define EEPROM_ADDRESS 0X50

//データ保存アドレス
#define DAT1_ADD 0
#define DAT2_ADD 20

//ディスプレイ設定
#define TFT_DC      8   // DC
#define TFT_CS      9   // CS
#define TFT_SCLK    10  // Clock
#define TFT_MOSI    11  // MOSI
#define TFT_RST     12  // Reset 
Adafruit_ST7735 tft = Adafruit_ST7735(&SPI1, TFT_CS, TFT_DC, TFT_RST);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;


//ディスプレイ表示
  void disp(int x,int y,String msg)
  {
    if((x==0) &&(y==0))
    {
      u8g2_for_adafruit_gfx.print(msg);
    }else{
      u8g2_for_adafruit_gfx.setCursor(x, y);
      u8g2_for_adafruit_gfx.print(msg);
    } 
  }

void setup() {
  //LCD初期化
    SPI1.setTX(TFT_MOSI);                       //H/W SPI 設定
    SPI1.setSCK(TFT_SCLK);
    tft.initR(INITR_GREENTAB);
    tft.initR(INITR_MINI160x80);                //Init ST7735S初期化
    tft.invertDisplay(true);
    tft.setRotation(3);
    tft.fillScreen(ST77XX_BLACK);
    u8g2_for_adafruit_gfx.begin(tft);
    u8g2_for_adafruit_gfx.setFont(u8g2_font_t0_14_me);//9 Pixel Height
    u8g2_for_adafruit_gfx.setFontMode(0);                 // use u8g2 none transparent mode
    u8g2_for_adafruit_gfx.setFontDirection(0);            // left to right (this is default)
    u8g2_for_adafruit_gfx.setForegroundColor(ST77XX_YELLOW);      // apply Adafruit GFX color

  //I2C設定
    Wire.setSDA(16);
    Wire.setSCL(17);
    Wire.begin();
    Wire.setClock(400000);

  //EEPROMライブラリ初期化
    if (myMem.begin(EEPROM_ADDRESS,Wire) == false)
    {
      disp(2,12,"No memory detected.");
      while (1);
    }
    disp(2,12,"Memory detected!");
    disp(2,24,"Mem size: ");
    disp(0,0,String(myMem.length())+"byte");

  //float形式 4byte
    float myValue3 = -3.125;
    myMem.put(DAT1_ADD, myValue3); //(location, data)
    float myRead3;
    myMem.get(DAT1_ADD, myRead3); //location to read, thing to put data into
    disp(2,36,"Value:"+String(myRead3,3));

  //Strig形式 1文字1byte
    String myString="Hello World";
    unsigned long nextEEPROMLocation = myMem.putString(DAT2_ADD, myString);
    String myRead4 = "";
    myMem.getString(DAT2_ADD, myRead4);
    disp(2,48,"String:"+myRead4);

  //次に利用可能なアドレス
    disp(2,60,"Next Add:"+String(nextEEPROMLocation));

}

void 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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?