This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I²C unexpected behaviour

Hi folks,

I'm trying to read a constant voltage through the MCP3425 ADC connected to a LPC1768 microcontroller, using the i2c 1 port. My code is based in one of the Keil i2c usage examples.
I'm getting a very strange behaviour, I ended up stucked in a point in which I can't get into a conclusion: if I'm getting this issue due to the way I'm reading the channel through the microcontroller or if this behavious is normal to this ADC.

Look at the for loop inside the Firwmare.c file. I start configuring the MCP3425 address, and then I follow to a reading in a loop. The only way I found to get the two bytes of reading is setting the I2CReadLength[PORT_USED] as multiples of 2.

Firmware.c

#include <rtl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "LPC17xx.h"
#include "type.h"
#include "i2c.h"


#define      WAIT_SI      while (!(LPC_I2C1->I2CONSET & (1<<3)))
#define      CLEAR_SI      LPC_I2C1->I2CONCLR = 1<< 3
#define      ACTION       LPC_GPIO0->FIOSET0 = 1
#define      ACTION_E      LPC_GPIO0->FIOCLR0 = 1
#define      ADDRESS       LPC_GPIO0->FIOSET0 = 2
#define      ADDRESS_E      LPC_GPIO0->FIOCLR0 = 2

#define      WRITE      LPC_GPIO0->FIOSET0 = 1<<4
#define      WRITE_E            LPC_GPIO0->FIOCLR0 = 1<<4
#define      READ       LPC_GPIO0->FIOSET0 = 1<<5
#define      READ_E     LPC_GPIO0->FIOCLR0 = 1<<5


#define PORT_USED               1
extern volatile uint8_t I2CMasterBuffer[I2C_PORT_NUM][BUFSIZE];
extern volatile uint8_t I2CSlaveBuffer[I2C_PORT_NUM][BUFSIZE];
extern volatile uint32_t I2CReadLength[I2C_PORT_NUM];
extern volatile uint32_t I2CWriteLength[I2C_PORT_NUM];

int main(void) {
        uint32_t i;

        SystemInit();

        I2C1Init();

        I2CWriteLength[PORT_USED] = 2;
        I2CReadLength[PORT_USED] = 0;
        I2CMasterBuffer[PORT_USED][0] = 0xD0; //MCP3425 DEVICE CODE ADDRESS FROM DATASHEET
        I2CMasterBuffer[PORT_USED][1] = 0x18;    //continuos conversion

        I2CEngine(PORT_USED);

        DELAY_ms(2000);

        //here i'm reading a voltage in a loop
for(i=0; i < 128;i = i+2){
        I2CWriteLength[PORT_USED] = 1;
        I2CReadLength[PORT_USED] = i+2;
        I2CMasterBuffer[PORT_USED][0] = 0xD1;
        I2CEngine(PORT_USED);

        DELAY_ms(2000);

}

        while (1);

}

0