0
votes

I'm using PIC18F87J11 as the master and LiPower Shield as the slave, and all I want to do is to be able to read the battery voltage value from the LiPower Shield. I'm using MPLAB C18 libraries for the I2C communication. I'm not able to get correct readings as I think the communication between the two devices is not setup correctly.

I'm looking for interpretations of the waveform signals in order to detect the issue. Also I would like to know if I'm missing something in the code. Any recommendations to improve the code would be helpful.

The LiPower Shield came with a sample code for Arduino but I'm using PIC18 chip from Microchip. The sample code is found here.

Here is the signals I'm getting while trying to read the battery voltage.

enter image description here

enter image description here

enter image description here

enter image description here

Code:

SSP2ADD = 19;
OpenI2C2(MASTER,SLEW_OFF);


StartI2C2();
IdleI2C2();   
WriteI2C2(0x36);
IdleI2C2();  

data = ReadI2C2();       // Read byte of data
printf ("\r\nAddress 32");
printf (" Byte:");
PrintChar(data);
IdleI2C2();
AckI2C2();
IdleI2C2();
WriteI2C2(0x02);
IdleI2C2();  
data = ReadI2C2();    // Read byte of data
printf ("\r\nAddress 02");
printf (" Byte:");
PrintChar(data);
IdleI2C2();
AckI2C2();
StopI2C2(); // Stop condition I2C on bus

Output: Which I think is wrong.

Address 32 Byte:FF
Address 02 Byte:FF

I'm not really sure if I'm writing/reading from the correct address, but that's the address they used in the their sample code. I hope I can get some interpretations on the signals and feedback on the code if possible.

1
Any comments would be appreciated! - Ammar

1 Answers

3
votes

I'm not familiar with the PIC but your code looks nowhere near right. Per the MAX17043DS datasheet, page 12, a memory read must consist of the following:

  1. I2C start condition
  2. Write device I2C write address (0x6C)
  3. Check for ACK from slave
  4. Write 8-bit memory address
  5. Check for ACK from slave
  6. I2C repeated start condition
  7. Write device I2C read address (0x6D)
  8. Read first byte of data
  9. Send ACK
  10. Read second byte of data
  11. Send NACK
  12. I2C Stop condition

What I see in your code is an I2C Start condition followed by a write 0f 0x36. Since this is not the device address the slave recognizes, it sends a NACK (as seen on your logic analyzer traces) and ignores everything else.

This question and answer has a lot of information on I2C on a PIC18. You also should probably find a read a basic I2C tutorial.