Hi everyone,
I have an I2C connection between the DS28E18 as Master and the Arduino slave.
It is attempting to send 0x00 to the Arduino slave and receive 2 Bytes of data, in this case 0x41 and 0x42. However, these 2 Bytes are not stored in the placeholder and the initial values, 0xFA and 0xFB, are read.
I checked the I2C signal with an oscilloscope and found that only 0x90 and 0x00 were signaled. Normally, 0x91 should be output after this.
The Arduino slave sketch sends 2 bytes of data immediately upon receiving 0x00.
I suspect my sketch either mishandles the DS28E18's handling of I2C commands, or it is not properly delayed.
What do you guys think?
DS28E18 Master:
#include <OneWire.h>
#include "OneWireOpenDrain.h"
OneWireOpenDrain ds28e18(12); //D12 pin, 330-ohm pullup required
byte data[8];
void setup() {
Serial.begin(115200);
Serial.println("init....");
delay(500);
ds28e18.reset();
/* Clear POR bit */
Serial.println("Send Device Status Command.(7A)");
ds28e18.reset();
ds28e18.write(0xCC); //SKIP_ROM
ds28e18.write(0x66); //COMMAND_START
ds28e18.write(0x01); //Length 1
ds28e18.write(0x7A); //Command 7A(Device Status)
for (int i = 0; i < 2; i++) {
data[i] = ds28e18.read(); //Rx:CRC-16(8bit*2)
}
ds28e18.write(0xAA); //Release Byte
delay(1); //delay <t_OP> or more
Serial.print(" Result; ");
for (int i = 0; i < 8; i++) {
data[i] = ds28e18.read(); //Rx: Dummy Byte, Length Byte, Result Byte, Device Status Byte, Version Byte, MANID[0], MANID[1], CRC-16(8bit*2)
Serial.print(data[i], HEX);
Serial.print(",");
}
ds28e18.reset();
Serial.print("\n");
/* Disable internal pullup resistor */
Serial.println("Write GPIO Configuration.(83)");
ds28e18.reset();
ds28e18.write(0xCC); //SKIP_ROM
ds28e18.write(0x66); //COMMAND_START
ds28e18.write(0x05); //Length 5
ds28e18.write(0x83); //Command 83(Write GPIO Configuration)
ds28e18.write(0x0B); //CFG_REG_TARGET 0B
ds28e18.write(0x03); //CFG_REG_MOD
ds28e18.write(0x00); //GPIO_CTRL_HI External pullup resistor, open drain
ds28e18.write(0x00); //GPIO_CTRL_LO GPIO Output low
for (int i = 0; i < 2; i++) {
data[i] = ds28e18.read(); //Rx:CRC-16(8bit*2)
}
ds28e18.write(0xAA); //Release Byte
delay(1); //delay <t_OP> or more
Serial.print(" Result; ");
for (int i = 0; i < 5; i++) {
data[i] = ds28e18.read(); //Rx: Dummy Byte, Length Byte, Result Byte, CRC-16(8bit*2)
Serial.print(data[i], HEX);
Serial.print(",");
}
ds28e18.reset();
Serial.print("\n");
/* Write I2C sequence to SRAM in DS28E18 */
Serial.println("Store I2C sequence in SRAM.(11)");
ds28e18.reset();
ds28e18.write(0xCC); //SKIP_ROM
ds28e18.write(0x66); //COMMAND_START
ds28e18.write(0x16); //Length 22
ds28e18.write(0x11); //01 Command 11(Write Sequencer Command Transfer)
ds28e18.write(0x00); //02 ADDR_LO
ds28e18.write(0x00); //03 ADDR_HI
ds28e18.write(0xCC); //04 0x0000 01 SENS_VDD On Command
ds28e18.write(0xDD); //05 0x0001 02 Delay Command
ds28e18.write(0x00); //06 0x0002 03 1ms
ds28e18.write(0x02); //07 0x0003 04 I2C Start Command
ds28e18.write(0xE3); //08 0x0004 05 I2C Write Data Command
ds28e18.write(0x02); //09 0x0005 06 I2C Write Length
ds28e18.write(0x90); //10 0x0006 07 I2C Address 0x90(Write)
ds28e18.write(0x00); //11 0x0007 08 Send "0x00"
ds28e18.write(0x03); //12 0x0008 09 I2C Stop Command
ds28e18.write(0x02); //13 0x0009 10 I2C Start Command
ds28e18.write(0xE3); //14 0x000A 11 I2C Write Data Command
ds28e18.write(0x01); //15 0x000B 12 I2C Write Length
ds28e18.write(0x91); //16 0x000C 13 I2C Address 0x91(Read)
ds28e18.write(0xD4); //17 0x000D 14 I2C Read Data Command
ds28e18.write(0x02); //18 0x000E 15 Read Length
ds28e18.write(0xFA); //19 0x000F 16 Read Data Placeholder - Byte 1
ds28e18.write(0xFB); //20 0x0010 17 Read Data Placeholder - Byte 2
ds28e18.write(0x03); //21 0x0011 18 I2C Stop Command
ds28e18.write(0xBB); //22 0x0012 19 SENS_VDD Off Command
for (int i = 0; i < 2; i++) {
data[i] = ds28e18.read(); //Rx:CRC-16(8bit*2)
}
ds28e18.write(0xAA); //Release Byte
delay(1); //delay <t_OP> or more
Serial.print(" Result; ");
for (int i = 0; i < 5; i++) {
data[i] = ds28e18.read(); //Rx: Dummy Byte, Length Byte, Result Byte, CRC-16(8bit*2)
Serial.print(data[i], HEX);
Serial.print(",");
}
ds28e18.reset();
Serial.print("\n");
}
void loop() {
/* Run a sequence written to SRAM */
Serial.println("Run stored sequence.(33)");
ds28e18.reset();
ds28e18.write(0xCC); //SKIP_ROM
ds28e18.write(0x66); //COMMAND_START
ds28e18.write(0x04); //Length 4
ds28e18.write(0x33); //Command 33(Run Sequencer)
ds28e18.write(0x00); //ADDR_LO
ds28e18.write(0x26); //SLEN_LO:ADDR_HI Length 19
ds28e18.write(0x00); //SLEN_HI
for (int i = 0; i < 2; i++) {
data[i] = ds28e18.read(); //Rx:CRC-16(8bit*2)
}
ds28e18.write(0xAA); //Release Byte
delay(300); //delay <t_OP> or more
Serial.print(" Result; ");
for (int i = 0; i < 8; i++) {
data[i] = ds28e18.read(); //Rx: Dummy Byte,Length Byte,Result Byte,CRC-16(8bit*2)
Serial.print(data[i], HEX);
Serial.print(",");
}
ds28e18.reset();
Serial.print("\n");
/* Transmits data received in SRAM to the host */
Serial.println("Read I2C results.(22)");
ds28e18.reset();
ds28e18.write(0xCC); //SKIP_ROM
ds28e18.write(0x66); //COMMAND_START
ds28e18.write(0x03); //Length 3
ds28e18.write(0x22); //Command 22(Read Sequencer)
ds28e18.write(0x0F); //ADDR_LO Placeholder Address
ds28e18.write(0x04); //SLEN:ADDR_HI (2byte)
for (int i = 0; i < 2; i++) {
data[i] = ds28e18.read(); //Rx:CRC-16(8bit*2)
}
ds28e18.write(0xAA); //Release Byte
delay(1); //delay <t_OP> or more
Serial.print(" Result; ");
for (int i = 0; i < 7; i++) {
data[i] = ds28e18.read(); //Rx: Dummy Byte,Length Byte,Result Byte,Data Byte,CRC-16(8bit*2)
Serial.print(data[i], HEX);
Serial.print(",");
}
ds28e18.reset();
Serial.print("\n");
/* Output received data to serial monitor */
Serial.print("Received data: 0x");
Serial.print(data[3], HEX);
Serial.print(", 0x");
Serial.print(data[4], HEX);
Serial.print("\n");
}
Arduino Slave:
#include <Wire.h>
#define SLAVE_ADDRESS 0x48 // 7-bit Slave Address
void receiveData(int byteCount) {
while (Wire.available()) {
byte receivedData = Wire.read(); // Read the received data from the master
// Process the received data and send appropriate responses
switch (receivedData) {
case 0x00:
Wire.write(0x41); // Send "0x41"
Wire.write(0x42); // Send "0x42"
break;
case 0x01:
Wire.write(0x51); // Send "0x51"
Wire.write(0x52); // Send "0x52"
break;
default:
// Handle any other cases if needed
break;
}
}
}
void setup() {
Wire.begin(SLAVE_ADDRESS); // Initialize I2C communication with the provided slave address
Wire.onReceive(receiveData); // Register the receive event handler function
}
void loop() {
// Additional code for the main loop, if needed
}