0
\$\begingroup\$

I am interfacing PIC18F2320 with MCP3221 ADC over I2C. When I try to read the converted value from ADC, only zeros are returned. I'm using MPLAB libraries. ADC is in A5 address mode. The AIN voltage is 2.503 V, what should be around 2048 after conversion.

Schematic looks like this (sorry for the mess, there was other stuff around and I'm debugging it so it isn't clean, just ignore MAX232, as removing it doesn't affect the result):

Schematic

Captured lines look like this (upper is clock; it's easy to observe the three groups of 9b each):

Clock and data line

The code I'm using is following:

// Set the ports to input, as the manual instructs
TRISCbits.TRISC3 = 1;
TRISCbits.TRISC4 = 1;

// Open I2C in master mode, slew off, as I'm aiming for 100kHz
OpenI2C(MASTER, SLEW_OFF);
SSPADD = 10; // A bit under 100kHz, because the MCU is on 4.1925 MHz

StartI2C();
WriteI2C(0x9B);      // Address of the ADC is 0x9A, 0x01 is read mode
voltage = ReadI2C(); // voltage is a global int; read upper byte
AckI2C();
voltage <<= 8;       // Make room for the lower byte
voltage |= ReadI2C();// Read lower byte
NotAckI2C();
StopI2C();

I have tried adding a delay after the address byte, but the result stays the same.

I have tried addressing with 0x9A only, in which case the data line is always high during reading.

Any other ideas?

\$\endgroup\$
  • 1
    \$\begingroup\$ My advice is that its only marginally useful to put your effort into getting a simulation like this to work. You're better off working with a real system. \$\endgroup\$ – Scott Seidman Feb 2 '16 at 16:30
  • \$\begingroup\$ What library are u using? Why are the I2C pins set as input when you are using the PIC as a master? Why are you even touching the pins or SSPADD at all? From what I recall, any library that has a function like OpenI2C() usually does the all the pin/register manipulation internally, especially considering that the SDA pin acts as both input and output in I2C operations. I recommend you look at the I2C source code to determine the arguments you need to pass to the functions to start the I2C peripheral, read and write to it as well. \$\endgroup\$ – SoreDakeNoKoto Feb 2 '16 at 17:07
  • \$\begingroup\$ I'm using MPLAB C18. I'm setting pins as input because the PIC18F2320 manual says that it's required to use I2C. I've gone over the source code and SSPADD isn't read or written anywhere, nor is PORT C. Furthermore, one of the examples in the library's docs sets it this exact way. I'm afraid that the library doesn't handle the mentioned things. \$\endgroup\$ – Alfis Feb 2 '16 at 17:15
1
\$\begingroup\$

To answer my own question almost a year later:

The teaching assistant who gave me this task told me that this behavior is a known "bug" in Proteus and not my fault. Quite anticlimactic.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.