• AVR Freaks

AnsweredHot!I2C responds NACK every time

Author
poross
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
2020/05/29 07:10:08 (permalink)
0

I2C responds NACK every time

Hi,

iI am a newcomer to the PIC32 and XC32.  I am using XC32 v2.40, MPLABX v5.35, and a pic32mx570f512l.  The PIC is on a protoboard with an INA219 current/voltage sensor using I2C. I send write and read command by the PIC to INA219(sensor), but the PIC responds NACK after the write bit. This means that the adress is correct and the communication isn't going on. I checked i2c sda and scl signal by oscilloscope. I2C generates start bit, adress byte, write bite and nack, respectively.
void INA219_Init(void) {


    ina219_currentDivider_mA = 0.5;
    ina219_powerMultiplier_mW = 0;

    // Option 1: setCalibration_32V_2A();
    setCalibration_32V_2A();
    // setCalibration_16V_400mA(); // Option 3
}

static int writeRegister(uint8_t reg, uint16_t value) {

    I2C1_MESSAGE_STATUS status;
    uint8_t *writeBuffer;
         writeBuffer = (uint8_t*) malloc(3*sizeof(uint8_t));
    writeBuffer[0] = reg;
    writeBuffer[1] = (value >> 8) & 0xFF;
    writeBuffer[2] = value & 0xFF;

            // write 3 byte to INA219 sensor(3 is the count of bytes to write)
        I2C1_MasterWrite(writeBuffer,
                3,
                pcf_adres,
                &status);
  // wait for the message to be sent or status has changed.
        while(status == I2C1_MESSAGE_PENDING);
        if (status == I2C1_MESSAGE_COMPLETE)
            break;

             free(writeBuffer);
}

static int readRegister(uint8_t reg, uint16_t *Rvalue) {

    I2C1_MESSAGE_STATUS status;
     uint8_t *ReadBuffer;
ReadBuffer = (uint8_t*) malloc(2*sizeof(uint8_t));
       

            // write one byte to INA219 sensor(1 is the count of bytes to write)
            I2C1_MasterWrite(&reg,
                    1,
                    slaveadreess,
                    &status);

            // wait for the message to be sent or status has changed.
            while(status == I2C1_MESSAGE_PENDING);
            if (status == I2C1_MESSAGE_COMPLETE)
            break;
            if (status == I2C1_MESSAGE_COMPLETE){
                   I2C1_MasterRead(ReadBuffer,
                        2,
                        INA219_ADDRESS,
                        &status);

                // wait for the message to be sent or status has changed.
            while(status == I2C1_MESSAGE_PENDING);
                   if (status == I2C1_MESSAGE_COMPLETE)
                  break;
             }


    *Rvalue = (ReadBuffer[0] << 8) | (ReadBuffer[1]);

free(ReadBuffer);
    return (1);

}

void setCalibration_32V_2A() {
    ina219_calibrationValue = 4096;

    ina219_currentDivider_mA = 10;
    ina219_powerMultiplier_mW = 2;

    writeRegister(INA219_REG_CALIBRATION, ina219_calibrationValue);

    writeRegister(INA219_REG_CONFIG, (INA219_CONFIG_BVOLTAGERANGE_32V |
            INA219_CONFIG_GAIN_8_320MV |
            INA219_CONFIG_BADCRES_12BIT |
            INA219_CONFIG_SADCRES_12BIT_1S_532US |
            INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS));
}


 
uint16_t getCurrent_mA() {
uint16_t gcrvalue;
 
writeRegister(INA219_REG_CALIBRATION, ina219_calibrationValue);
 
readRegister(INA219_REG_CURRENT, &gcrvalue);
 
return (uint16_t) gcrvalue;
}
 

int main(void)
{
     
 __XC_UART = 1;
    // initialize the device
    SYSTEM_Initialize();

        
  
printf ("Welcome: \r\n" );
     // printf (" siz: v\r\n");
         
      
    while (1)
    {
        float current_mA;
         float loadvoltage;
           current_mA= getCurrent_mA();
  float shuntVoltage_mV=10;
     //shuntVoltage_mV= getShuntVoltage_mV();
  float busVoltage_V;
       busVoltage_V = getBusVoltage_V();
       loadvoltage=busVoltage_V+(shuntVoltage_mV/1000);
    printf ("Current: %f mA\r\n", current_mA);
        //printf ("volt: %f v\r\n", loadvoltage);
         delay_mss(500);
   
    }

    return -1;
}

post edited by poross - 2020/05/29 09:53:56
#1
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3984
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 09:29:19 (permalink) ☄ Helpfulby poross 2020/06/03 22:35:56
+1 (3)
"but the PIC responds NACK after the adress byte. This means that the adress is correct"
 
It's "NACKered" !
 
You have the logic reversed.
 
ack (0) is success
nak (1) is not
 
       ___
ACK/NAK
 
Slow chips need to be polled.  eeproms

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#2
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3984
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 09:41:41 (permalink) ☄ Helpfulby poross 2020/05/29 10:00:51
+1 (1)
i.e.
 
 i2c_stop(); // eeprom now busy writing latch buffer to memory
while ( i2c_poll(EEPROM) ); // wait for ACK
..
write next latch buffer or to eeprom that
is not busy
..

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#3
poross
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 10:00:40 (permalink)
0
I'm sorry , i forgot to delete the comments. I didn't use eeprom. 
I just use INA219 current sensor.
 Thanks for answering
#4
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3984
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 11:10:50 (permalink) ☄ Helpfulby poross 2020/06/03 22:35:48
+1 (3)
The device is irrelevant, it is the I2C code that is wrong.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#5
poross
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 11:24:37 (permalink)
0
I see. I didn't understand where I was wrong. I wrote according to the sample I2C code
#6
ric
Super Member
  • Total Posts : 28024
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: I2C responds NACK every time 2020/05/29 15:08:28 (permalink) ☄ Helpfulby poross 2020/06/03 22:35:43
+1 (1)
poross
I see. I didn't understand where I was wrong. I wrote according to the sample I2C code

What does this mean?
As Gort states, a NACK is an error, regardless of the device.
It always means the slave rejected what you sent.
The only other meaning is when writing to a memory device, it will NACK a valid address while it is busy.
That does not apply to your sensor.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3984
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/29 17:51:23 (permalink) ☄ Helpfulby poross 2020/06/03 22:35:36
+1 (3)
You wired it up, don't forget what
the resistors are doing.
 
NAK is the affect of a pull-up resistor
in an open drain circuit.  nak is always set.
Not Active high.
 
ACK is the affect of an over-ride of a weak pullup resistor.
Data to 0v.  Active low.  Imagine a tiny switch in the circuit.
 
You could do it yourself just by flicking a switch.
1000's of times.
 
If a chip is busy it can not respond.
NAK on bit 9 (pullup resistor)
 
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#8
poross
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/05/30 09:33:23 (permalink)
0
I setup 4k7 pull-up resistor with 3.3 volt.
 "I2C1_MasterWrite(writeBuffer, 3, pcf_adres, &status);" Can i send 3 byte to write ? 
"https://imgur.com/a/Y2OxikN" I2C config.

 
My adress is ‭01001010‬ but  when I look at the signals with the oscilloscope, I see "10010100"  
post edited by poross - 2020/05/30 09:48:59
#9
NKurzman
A Guy on the Net
  • Total Posts : 18860
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: I2C responds NACK every time 2020/05/30 10:12:31 (permalink) ☼ Best Answerby poross 2020/06/03 22:35:24
+2 (2)
Just a note a device doesn’t respond with a NACK. A NACK is an idle bus condition ( pulled High) caused when no device responds because none, have the address you were talking to. Or the start bit was not send properly.

An I2c address is 7 bits. The low bit is the read/write bit. Some documentation will refer to the seven bits. Others will include it as a 7 bits shifted left. ( The same way you would see it on a scope).
Different I2c library’s Will accept the data different ways, seven bits, or eight.
Compare what you see on the scope with what your device data Sheets says.
If it is wrong. Shift your address in code so that it is correct on the scope.
#10
ric
Super Member
  • Total Posts : 28024
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: I2C responds NACK every time 2020/05/30 14:55:08 (permalink) ☄ Helpfulby poross 2020/06/03 22:35:30
+1 (1)
poross
... My adress is ‭01001010‬ but  when I look at the signals with the oscilloscope, I see "10010100"  

Plainly that function only wants the 7 bit address, which it is left shifting and adding the R/W bit to.
Pass 0b00100101 instead.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
poross
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: I2C responds NACK every time 2020/06/01 01:04:14 (permalink)
0
Thanks your helping. Now, I2C responds ack with right adress. But  I cannot read on the computer. 
shows only 0 volts and 0 currents. Maybe my I2C reading code is wrong. But i couldn't find where am i wrong?
 

 


post edited by poross - 2020/06/01 05:04:23
#12
Jump to:
© 2020 APG vNext Commercial Version 4.5