ArduinoにFRAMを接続する、その1


 成以来気に入って使ってるこちら
 使いながら、やっぱり直前の周波数を再現する機能があった方が面白いかなというところが気がかりでして、いろいろ調べてみましたら…
 EEPROMより数桁書き換え回数が多いFRAMというものの存在を知りまして(今さら)。10兆回ってすごいですね。
 小さい容量のものは全然高くないようなので、試しに入手してみました。


 品物は大陸から届いたんでちょっと身構えましたが、偽物だったとしてもまぁそれはそれで。
 これ、扱いは普通の外付けシリアルEEPROMと同じでいいということなんですが、調べ方が良くないのか24C04のI2Cアドレスって結局どれなん? 状態。
 なので、こちらで紹介されているi2c_scannerなるものを走らせてみます。

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

 なるほど、まず最初は0x50でいいようです。
 ちなみに最初上記リンク先のように接続してみたところなぜかうまく動かず、しばらく悩むことに。
 結局単純に間違ってる? よく分からないものの SDAをA4 SCLをA5 に接続するとうまくいきました。(写真は正しく接続したところ)

 ともあれI2Cのアドレスが分かったので、今度は実際にちゃんと読み書きできるか試してみます。
 いくつかやり方があるようなんですが、まずはこちらの中のサンプルが簡単そうなので試してみます。(文字数など適当に変えてます)

/**************************************************************************//**
 * \brief EEPROM 24C04 / 24C16 library for Arduino - Demonstration program
 * \author Copyright (C) 2012  Julien Le Sech - www.idreammicro.com
 * \version 1.0
 * \date 20120218
 *
 * This file is part of the EEPROM 24C04 / 24C16 library for Arduino.
 *
 * This library is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 ******************************************************************************/

/**************************************************************************//**
 * \file WriteReadBytes.ino
 ******************************************************************************/
 
/******************************************************************************
 * Header file inclusions.
 ******************************************************************************/

#include <Wire.h>

#include <Eeprom24C04_16.h>

/******************************************************************************
 * Private macro definitions.
 ******************************************************************************/

/**************************************************************************//**
 * \def EEPROM_ADDRESS
 * \brief Address of EEPROM memory on TWI bus.
 ******************************************************************************/
#define EEPROM_ADDRESS 0x50

/******************************************************************************
 * Private variable definitions.
 ******************************************************************************/

static Eeprom24C04_16 eeprom(EEPROM_ADDRESS);

/******************************************************************************
 * Public function definitions.
 ******************************************************************************/

/**************************************************************************//**
 * \fn void setup()
 *
 * \brief
 ******************************************************************************/
void setup()
{
    // Initialize serial communication.
    Serial.begin(9600);
    
    // Initiliaze EEPROM library.
    eeprom.initialize();

    const word address = 0;
    const byte count = 9;

    // Declare byte arrays.
    byte inputBytes[count] = { 0 };
    byte outputBytes[count] = { 0 };

    // Fill input array with printable characters. See ASCII table for more
    // details.
    for (byte i = 0; i < count; i++)
    {    
        inputBytes[i] = i + 49;
    }

    // Write input array to EEPROM memory.
    Serial.println("Write bytes to EEPROM memory...");
    eeprom.writeBytes(address, count, inputBytes);

    // Read array with bytes read from EEPROM memory.
    Serial.println("Read bytes from EEPROM memory...");
    eeprom.readBytes(address, count, outputBytes);
    
    // Print read bytes.
    Serial.println("Read bytes:");
    for (byte i = 0; i < count; i++)
    {
        Serial.write(outputBytes[i]);
        Serial.print(" ");
    }
    Serial.println("");
}

/**************************************************************************//**
 * \fn void loop()
 *
 * \brief
 ******************************************************************************/
void loop()
{

}

 結果をキャプチャし忘れましたが、見たところ一応動いてるような感じ。

 ここまで来たら、あとは読み書きの関数を作ってやれば目的達成できそう…なんですが。
 いやいやここからが問題・悩みどころやん、さてどうしたものかと思いながらふと『ChatGPTが教えてくれる』なんて巷の噂を思い出したので、厚かましくも尋ねてみることに。










 いやー、なんとも素晴らしいですね! 自分で考えることなくこういうことをやってると、瞬く間に退化してしまいそう。

 と言いながら、せっかくですからこのコードを活用させていただくことにします(いつになるやら分かりませんが続く)。

コメントする

この記事について

このページは、ji3kdhが2023年8月10日(木) 0:45に書いた記事です。

ひとつ前の記事は「冷やかしアンテナ」です。

次の記事は「メテオスキャッター」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。