I2C communication between LPC824 and ADC MCP3424

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C communication between LPC824 and ADC MCP3424

1,940 Views
charlesahaab
Contributor II

Hi, 

I need to communicate the LPC824 between the ADC MCP3424 using I2C. 

Problem: I'm using the LPCOpen examples to create an library for this communication, but I'm still not getting results.

Can anyone help me with communication between the LPC824 and the MCP3424?

Thanks!

Labels (1)
Tags (1)
8 Replies

1,181 Views
ianbenton
Senior Contributor I

I used the example code in Chapter 34 of the manual and it worked (and I also translated it into assembler so that it would still keep up a decent speed with a 12MHz clock, and that worked as well).

One thing to note is that the code example has 7-bit addresses and adds the 8th bit for read or write, whereas Microchip has the 7-bit address already shifted one place to the left.

0 Kudos

1,181 Views
Ray_V
Contributor V

I re-read your code and see that you do have the calls correctly. It should work.

The problem may be inside the LPC API calls.

I notice that when you are sending you do not set the receive pointer, and when you receive you do not set the send pointer.

Try to explicitly set them to null when not in use. If this does not work set them to point to a valid address.

It may be that the API expects the pointers to be null when size is 0, or maybe the API checks always for a non-null pointer even when size is 0. If you don't explicitly set them you don't know what value they hold.

The address should not be a problem since you use the same address when it works and when it doesn't.

0 Kudos

1,181 Views
vojtechhavlicek
Contributor III

Hi,

I read your question very roughly, try this:

I2C address in NXP manner means that you take expected I2C address of device and shifts one bit to the left, so address 0b00001101 becomes 0b00011010 I think that rom drivers will handle last bit of read/write, so if the address for you sensor is 0x0D change it to 0x1A. 

0 Kudos

1,181 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Charles Haab,

Thank you for your interest in NXP Semiconductor products and 
for the opportunity to serve you.

I was wondering if you can describe the problem in more details.

I'm looking forward to your reply.

Have a great day.

TIC

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,181 Views
charlesahaab
Contributor II

Hi jeremyzhou‌,

First step: I have tested send the data directly on the i²c bus.

/* In the main function I call the functions*/
sendI2CMaster();
readI2CMaster(); 

/* The functions are declared in the i2c.h */
void sendI2CMaster(void);
void readI2CMaster(void);

/* And in i2c.c i have*/
void sendI2CMaster(){
      
        /* Address os the MCP3424 and the read bit set high for read. */
     uint8_t SendData[] = {0xD1}; 
     I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;

     param.num_bytes_send    = sizeof(SendData);
     param.buffer_ptr_send   = &SendData[0];
     param.num_bytes_rec     = 0;
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

        /* I receive error_code = LPC_I2C_OK. */
        error_code = LPC_I2CD_API->i2c_master_transmit_poll(i2cHandleMaster, &param, &result);

}

void readI2CMaster()
{
     uint8_t recvData[5];
     I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;


     param.num_bytes_send    = 0;
     param.num_bytes_rec     = sizeof(recvData);
     param.buffer_ptr_rec    = &recvData[0];
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

     /* I receive error_code = LPC_I2C_OK */
     error_code = LPC_I2CD_API->i2c_master_receive_poll(i2cHandleMaster, &param, &result);

}

/* This code works perfectly! but I need one function with inputs vector data and the size of this vector */
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Second step: I created an function with inputs to send data. This code not works.

/* In the main function I call the functions*/
/* Address os the MCP3424 and the read bit set high for read. */
uint8_t dado[]={0xd1}; 
sendI2CMaster(dado,sizeof(dado));
readI2CMaster(); 

/* The functions are declared in the i2c.h */
void sendI2CMaster(uint8_t *data, uint32_t size);
void readI2CMaster(void);

/* And in i2c.c i have*/
void sendI2CMaster(uint8_t *data, uint32_t size);{
      
        uint8_t *p = data;
     
        I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;

     param.num_bytes_send    = size;
     param.buffer_ptr_send   = p;
     param.num_bytes_rec     = 0;
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

        /* I receive error_code = LPC_I2C_OK. */
        error_code = LPC_I2CD_API->i2c_master_transmit_poll(i2cHandleMaster, &param, &result);

}

void readI2CMaster()
{
     uint8_t recvData[5];
     I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;


     param.num_bytes_send    = 0;
     param.num_bytes_rec     = sizeof(recvData);
     param.buffer_ptr_rec    = &recvData[0];
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

     /* I receive error_code = ERR_I2C_NAK*/
     error_code = LPC_I2CD_API->i2c_master_receive_poll(i2cHandleMaster, &param, &result);

}

/* This code not works! I receive Non Acknowladge error */‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Third step: I tried another way in the pointers declaration, which I believe to be more correct, but also did not work.

/* In the main function I call the functions*/
/* Address os the MCP3424 and the read bit set high for read. */
uint8_t dado[]={0xd1}; 
sendI2CMaster(dado,sizeof(dado));
readI2CMaster(); 

/* The functions are declared in the i2c.h */
void sendI2CMaster(uint8_t *data, uint32_t size);
void readI2CMaster(void);

/* And in i2c.c i have*/
void sendI2CMaster(uint8_t *data, uint32_t size);{
      
     
     I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;

     param.num_bytes_send    = size;
     param.buffer_ptr_send   = data;
     param.num_bytes_rec     = 0;
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

        /* I receive error_code = LPC_I2C_OK. */
        error_code = LPC_I2CD_API->i2c_master_transmit_poll(i2cHandleMaster, &param, &result);

}

void readI2CMaster()
{
     uint8_t recvData[5];
     I2C_PARAM_T param;
     I2C_RESULT_T result;
     ErrorCode_t error_code;


     param.num_bytes_send    = 0;
     param.num_bytes_rec     = sizeof(recvData);
     param.buffer_ptr_rec    = &recvData[0];
     param.stop_flag         = 1;

     LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 500000);

     /* I receive error_code = ERR_I2C_NAK*/
     error_code = LPC_I2CD_API->i2c_master_receive_poll(i2cHandleMaster, &param, &result);

}

/* This code not works! I receive Non Acknowladge error */‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Do you have idea because it's occurs?

Regards, 

Charles Haab

0 Kudos

1,181 Views
Ray_V
Contributor V

Does this code compile?

On your i2c.c you have

/* And in i2c.c i have*/
void sendI2CMaster(uint8_t *data, uint32_t size);{

The semicolon highlighted in red makes this a function declaration with no body. It would produce a compiler error when it tries to build the body of the function.

0 Kudos

1,181 Views
charlesahaab
Contributor II

Hi raymundovelarde‌, how are you?

Yes, this code compile. 

The semicolon placed there was a mistake when passing the code here to the forum.
Sorry haha


Regards,
Charles Haab

0 Kudos

1,181 Views
Ray_V
Contributor V

If that semicolon is not there then the code as described should work.

2 possibilities why it doesn't.

  1. when making the call the wrong pointer to the data is passed to the function. Can you show the data declaration and the sendI2CMaster() call?
  2. Since the semicolon is not on your code I assume that you did not copy your entire function. It's possible that something you did in the function prior to the code you show is causing the problem.
0 Kudos