• AVR Freaks

AnsweredHot!Doesn't work I2C with XC32

Author
poross
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
2020/05/26 22:49:18 (permalink)
0

Doesn't work I2C with XC32

Hello Everyone,

I 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 believe that I have the basic functionality of the PIC working because I can send data via UART, blink an LED. I am using MPLAB Code Configurator for setting configuration of the PIC.
I really don't understand why I2C does't work. Thank you for helping me 
 
Here is the setup of the INA219 library and reading current code.
 
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));


    uint8_t retryTimeOut, slaveTimeOut;
     retryTimeOut = 0;
        slaveTimeOut = 0;

    writeBuffer[0] = reg;
    writeBuffer[1] = (value >> 8) & 0xFF;
    writeBuffer[2] = value & 0xFF; 


    while (status != I2C1_MESSAGE_FAIL) {

        I2C1_MasterWrite(writeBuffer,
                3,
                pcf_adres,
                &status);

        while(status == I2C1_MESSAGE_PENDING)
                        {

                            if (slaveTimeOut == MCHP24AA512_DEVICE_TIMEOUT)
                                return (0);
                            else
                                slaveTimeOut++;
                        }
        if (status == I2C1_MESSAGE_COMPLETE)

            break;

}
             free(writeBuffer);


}

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



    I2C1_MESSAGE_STATUS status;
    uint8_t retryTimeOut, slaveTimeOut;
    uint8_t counter;

    uint8_t *ReadBuffer;
ReadBuffer = (uint8_t*) malloc(2*sizeof(uint8_t));
        retryTimeOut = 0;
        slaveTimeOut = 0;

        while (status != I2C1_MESSAGE_FAIL) {
            // write one byte to EEPROM (2 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 (slaveTimeOut == MCHP24AA512_DEVICE_TIMEOUT)
                                return (0);
                            else
                                slaveTimeOut++;
                        }
        } 
            if (status == I2C1_MESSAGE_COMPLETE){
                

            while (status != I2C1_MESSAGE_FAIL) {

                I2C1_MasterRead(ReadBuffer,
                        2,
                        INA219_ADDRESS,
                        &status);


                // wait for the message to be sent or status has changed.
            while(status == I2C1_MESSAGE_PENDING)
                        {

            if (slaveTimeOut == MCHP24AA512_DEVICE_TIMEOUT)
                                return (0);
            else
                                slaveTimeOut++;
                        }

            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;
}
 


Here is main code.
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/26 22:52:09
#1
wltse601
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/10/06 22:12:01
  • Location: 0
  • Status: offline
Re: Doesn't work I2C with XC32 2020/05/27 20:16:33 (permalink) ☼ Best Answerby poross 2020/05/29 05:52:57
+2 (2)
Hi poross,
 
I've spotted use of uninitialized C variable "status" inside function "writeRegister(uint8_t reg, uint16_t value)".  Relevant codes are copied below:
 
"static int writeRegister(uint8_t reg, uint16_t value) {

    I2C1_MESSAGE_STATUS status;
    uint8_t *writeBuffer;
         writeBuffer = (uint8_t*) malloc(3*sizeof(uint8_t));


    uint8_t retryTimeOut, slaveTimeOut;
     retryTimeOut = 0;
        slaveTimeOut = 0;

    writeBuffer[0] = reg;
    writeBuffer[1] = (value >> 8) & 0xFF;
    writeBuffer[2] = value & 0xFF; 


    while (status != I2C1_MESSAGE_FAIL) {

        I2C1_MasterWrite(writeBuffer,
                3,
                pcf_adres,
                &status);

        while(status == I2C1_MESSAGE_PENDING) ..... "
 
When comparing "status" equal to I2C1_MESSAGE_FAIL, "status" is not assigned with pre-defined value such as with any value not equal to I2C1_MESSAGE_FAIL.  This can cause immediate quit of the while loop without executing codes within the loop including "I2C1_MasterWrite(...)".
 
This situation occurs to another while loop relating to variable "status" inside C function "readRegister(uint8_t reg, uint16_t *Rvalue)".  I hope my points can help.
 
Brian
#2
lachlanp
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/27 02:03:54
  • Location: 0
  • Status: offline
Re: Doesn't work I2C with XC32 2020/05/29 20:50:01 (permalink)
0
Cant see where you have setup the address for the INA219.
I presume that is in some other part of the program.
I see that the code refers to 'pcf_adres'. Does this hold the slave address of the INA219? Where is this setup?
Lachlan
#3
poross
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: Doesn't work I2C with XC32 2020/05/30 09:30:11 (permalink)
0
I setup header files pcf_adress
#4
lachlanp
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/27 02:03:54
  • Location: 0
  • Status: offline
Re: Doesn't work I2C with XC32 2020/05/30 16:13:50 (permalink)
0
Lets hope it is the correct INA219 address
#5
poross
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2020/05/21 04:21:18
  • Location: 0
  • Status: offline
Re: Doesn't work I2C with XC32 2020/05/31 23:20:43 (permalink)
0
 it is the correct INA219 address
#6
Jump to:
© 2020 APG vNext Commercial Version 4.5