看到文章說,最大只能傳送32 bytes,有看到說更改底層的define就可使用超過 32 bytes
而這邊是為了要讀取64 bytes因為是練習,所以就把他拆而兩次讀,重點是要了解其I2C R/W程式寫法:
參考:
https://www.arduino.cc/en/Reference/Wire
#include <Wire.h>
bool send_step1_ok_flag;
bool send_step2_ok_flag;
byte buffer[64];
byte buffer_count;
byte slave_address=0x55;
void setup()
{
Serial.begin(9600);
while(!Serial) ;
Serial.println("Serail port working...");
send_step1_ok_flag=false;
send_step2_ok_flag=false;
Wire.begin();
}
void I2C_cmd_send(byte start_address)
{
byte length=32;
Wire.beginTransmission(slave_address);
Wire.write(start_address);
Wire.endTransmission();
Wire.requestFrom(slave_address,length);
}
void loop()
{
if((!send_step1_ok_flag)&&(!send_step2_ok_flag))
{
Serial.println("Master Send I2C cmd");
send_step1_ok_flag=true;
I2C_cmd_send(0);
buffer_count=0;
}
else if((send_step1_ok_flag)&&(!send_step2_ok_flag))
{
if(buffer_count==32)
{
send_step2_ok_flag=true;
I2C_cmd_send(32);
}
}
else
{
Serial.println("Master Read finish");
for(byte i=0;i<64;i++)
{
Serial.print(buffer[i],HEX);
Serial.print(" ");
}
Serial.println();
send_step1_ok_flag=false;
send_step2_ok_flag=false;
delay(1000);
}
while(Wire.available())
{
buffer[buffer_count]=Wire.read();
buffer_count=buffer_count+1;
}
}