Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 2 months ago.
I2CSlaveで2回連続したWriteAddressedがうまく分割判定できない
I2CSlaveで、2回連続したWriteAddressedがうまく分割判定できず、困っています。
- 2つのWriteAddressedが1つになってしまう
- アドレスを示す0x40がデータとして取れてしまう
I2CSlaveの使い方に問題があるのでしょうか。 アドバイスをよろしくお願い致します。
使用ボード:TMPM066
I2CSlaveコード(Mbed5)
#include <mbed.h>
#define DLM "\r\n"
#define I2C_SLAVE_ADDRESS (0x40)
static I2CSlave _I2c(MBED_I2C0);
static void I2cSlaveDoWork()
{
int addressed = _I2c.receive();
if (addressed == I2CSlave::NoData) return;
printf("Enter I2C Slave" DLM);
switch (addressed)
{
case I2CSlave::WriteAddressed:
for (int i = 0; ; i++)
{
int data = _I2c.read();
printf("%#02x" DLM, data);
if (data < 0) break;
}
break;
case I2CSlave::ReadAddressed:
if (_I2c.write("ABC", 3) != 0) return;
break;
}
printf("Leave I2C Slave" DLM);
}
int main()
{
_I2c.address(I2C_SLAVE_ADDRESS);
while (true)
{
I2cSlaveDoWork();
}
}
I2CMasterコード (Arduino Uno)
#include <Wire.h>
void setup()
{
Wire.begin();
}
void loop()
{
Wire.beginTransmission(0x20);
Wire.write(0x12);
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x34);
Wire.write(0x56);
Wire.write(0x78);
Wire.endTransmission();
delay(1000);
}

実行時のログ出力 (Mbed5)
Enter I2C Slave 0x12 0x40 0x34 0x56 0x78 0xffffffff Leave I2C Slave Enter I2C Slave 0x12 0x40 0x34 0x56 0x78 0xffffffff Leave I2C Slave