LoginSignup
0
0

More than 3 years have passed since last update.

ロジックアナライザを試すその5:Wire.setClock()

Posted at

Wire.setClock()確認

指定した値どおり、I2Cのクロック(SCL)が出ているかをロジックアナライザで確認する。

用いたもの

ESP32搭載のM5Stackおよび光センサー(TLS2561搭載)。ロジックアナライザは以前の記事参照。Arduino IDE利用。

ソースコード

#include <Wire.h>
#include <Digital_Light_TSL2561.h>

void writeRegisterValue(int deviceAddress, int address, uint8_t val) {
    Wire.beginTransmission(deviceAddress);  // start transmission to device
    Wire.write(address);                    // send register address
    Wire.write(val);                        // send value to write
    Wire.endTransmission();                 // end transmission
}
uint8_t readRegisterValue(int deviceAddress, int address) {
    uint8_t value;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address);                // register to read
    Wire.endTransmission();
    Wire.requestFrom(deviceAddress, 1); // read a byte
    while (!Wire.available());
    value = Wire.read();
    return value;
}

void setup() {
    Wire.begin();
    Wire.setClock(20000);
    Serial.begin(9600);
    TSL2561.init();
}

void loop() {
    writeRegisterValue(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
    delay(20);
    Serial.printf("Channel 0 Low value = 0x%x\n", readRegisterValue(TSL2561_Address, TSL2561_Channal0L));
    writeRegisterValue(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down

    delay(2000);
}

setup()中にある

Wire.setClock(20000);

でクロックを指定する。上記は20kHZの指定。
なお、TLS2561のハンドリングについては、以前の記事参照。参照ソースコードはこちら。ここでは、チャネル0のLow値を定期的に取得している。

実験

100kHz指定時のデータ(デフォルトらしい、Wire.setClock()未使用時)

100kHz.png
D0がクロック(SCL)(以降同様)。約10μsごとに、SCLが繰り返されているのが見える。

50kHz指定時のデータ(Wire.setClock(50000))

50kHz.png
約20μsごとに、SCLが繰り返されているのが見える。

20kHz指定時のデータ(Wire.setClock(20000))

20kHz.png
約50μsごとに、SCLが繰り返されているのが見える。

終わりに

当たり前であるが、指定値どおりにクロックが出ている。

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