This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

I2C Communication with ADXL345 Accelerometer

Other Parts Discussed in Thread: HALCOGEN

Hello people,

I am trying to communicate with the ADXL345 accelerometer via I2C with the generated drivers from HalCoGen. It looks that I am currently sending correctly the data to the SDA line, but I cannot receive anything.

The frame for this device for I2C must be as follows:

My first step is to read the device id (should read 0xE5).

  • The slave address is 0x53, which interprets to 0xA6 for write, 0xA7 for read.
  • The register to get the device id is 0x00.

The screenshot of my logic analyzer currently is:


I assume it looks good for sending.

My problem is that I cannot receive anything.

I tried to keep my code as simple as possible:

	i2cInit();

	/* Configure address of Slave to talk to */
	i2cSetSlaveAdd(i2cREG1, 0x53);

	/* Set mode as Master */
	i2cSetMode(i2cREG1, I2C_MASTER);

	/* Transmit Start Condition */
	i2cSetStart(i2cREG1);

	/* Tranmit 3 bytes of data in Polling mode */
	i2cSend(i2cREG1, 3, TX_Data_Master);

	i2cSetDirection(i2cREG1, I2C_RECEIVER);

	i2cSetStart(i2cREG1);

	i2cReceive(i2cREG1, 1, RX_Data_Master);


	while(1);

In any case I attach my whole project. I would be grateful for any suggestions.

3632.I2C.zip

Kind regards,
Stelios

  • Hi Stelios,

    I will read your code, then come back to you later.

    regards,
    QJ
  • Hello QJ,

    Just an update:

    I noticed that my 3rd byte should be preceded by a new Start condition. I tried to setStart again, but no luck. By the way, the option "Enable Repeat Mode", is for consecutive sends (i.e i2cSendByte, i2cSendByte, ...)?

    I am looking forward for your suggestions.

    Kind regards,
    Stelios
  • QJ,

    Any updates?
  • Hello,

    I am still struggling with it if anyone is wondering.

    with the following code it is working:

    void main(void)
    {
    /* USER CODE BEGIN (3) */
    	sint16 x,y,z=0;
    	uint8 data_in[10];
    	int i;
    	i2cInit();
    	i2cSetSlaveAdd(i2cREG1, ADXL345Addr);
    
    
      while(1)
      {
    
    	i2cSetCount(i2cREG1, 1);
    	i2cREG1->MDR = i2cREG1->MDR | I2C_MASTER | I2C_TRANSMITTER;
    	i2cSetStart(i2cREG1);
    	i2cSendByte(i2cREG1, 0x00);													// Read address
    	for (i=0;i<10000;i++);
    
    	i2cSetCount(i2cREG1, 1);
    	i2cREG1->MDR =  I2C_START_COND | I2C_STOP_COND |I2C_MASTER | I2C_RECEIVER | I2C_RESET_OUT;
    	i2cReceive(i2cREG1,1,data_in);
    	for (i=0;i<10000;i++);
    
    
    	i2cSetCount(i2cREG1, 2);
    	i2cREG1->MDR = i2cREG1->MDR | I2C_MASTER | I2C_TRANSMITTER;
    	i2cSetStart(i2cREG1);
    	i2cSendByte(i2cREG1, 0x31);													// Read address
    	i2cSendByte(i2cREG1, 0x01);													// Read address
    	for (i=0;i<10000;i++);
    
    	i2cSetCount(i2cREG1, 2);
    	i2cREG1->MDR = i2cREG1->MDR | I2C_MASTER | I2C_TRANSMITTER;
    	i2cSetStart(i2cREG1);
    	i2cSendByte(i2cREG1, 0x2D);													// Read address
    	i2cSendByte(i2cREG1, 0x08);													// Read address
    	for (i=0;i<10000;i++);
    
    
    	i2cSetCount(i2cREG1, 1);
    	i2cREG1->MDR = i2cREG1->MDR | I2C_MASTER | I2C_TRANSMITTER;
    	i2cSetStart(i2cREG1);
    	i2cSendByte(i2cREG1, 0x32);													// Read address												// Read address
    	for (i=0;i<10000;i++);
    
    
    	i2cSetCount(i2cREG1, 6);
    	i2cREG1->MDR =  I2C_START_COND | I2C_STOP_COND |I2C_MASTER | I2C_RECEIVER | I2C_RESET_OUT;
    	i2cReceive(i2cREG1,6,data_in);
    	for (i=0;i<10000;i++);
    
    	x = (sint8) data_in[1] << 8 | (sint8) data_in[0];
    	y = (sint8) data_in[3] << 8 | (sint8) data_in[2];
    	z = (sint8) data_in[5] << 8 | (sint8) data_in[4];
    	printf("x = %d, y = %d, z = %d \n", x, y, z );
    
    
    
      }
    
    
    /* USER CODE END */
    }
    

    and I get the right results. If I remove the for loops between the send and receive it waits in the line "while ((i2c->STR & (uint32)I2C_RX_INT) == 0U)". 

    Any suggestions now??? I would be really grateful.

    Kind regards,
    Stelios