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.

How to read/write bytes with I2C module?

Hi,

I am learning the I2C module with F28069 and 

I use the F28069 as the master to communicate with MPU6050.

Now I have successfully driven the MPU6050 and get correct data with functions that can only write/read one byte.

I tried to write a function that can read multiple bytes but it did't work.

I use the GPIO32 and GPIO33 as the SDA and SCL ports.

Here is my I2C initialization codes:

void I2CA_Init(void)
{
   // Initialize I2C
   I2caRegs.I2CMDR.all = 0x0000;
   I2caRegs.I2CSAR = 0;		// Slave address

   I2caRegs.I2CPSC.all = 8;		    		// Prescaler - need 7-12 Mhz on module clk
   I2caRegs.I2CCLKL = 10;					// NOTE: must be non zero
   I2caRegs.I2CCLKH = 5;					// NOTE: must be non zero

   //I2caRegs.I2CIER.all = 0x24;			// Enable SCD & ARDY interrupts  0010 0100
   I2caRegs.I2CIER.bit.AAS = 0;				// Addressed as slave interrupt enable bit
   I2caRegs.I2CIER.bit.SCD = 1;				// Stop condition detected interrupt enable bit
   I2caRegs.I2CIER.bit.XRDY = 1;			// Transmit-data-ready interrupt enable bit
   I2caRegs.I2CIER.bit.XRDY = 1;			// Receive-data-ready interrupt enable bit
   I2caRegs.I2CIER.bit.ARDY = 1;			// Register-access-ready interrupt enable bit
   I2caRegs.I2CIER.bit.NACK = 0;			// No-acknowledgment interrupt enable bit
   I2caRegs.I2CIER.bit.ARBL = 0;			// Arbitration-lost interrupt enable bit

   //I2caRegs.I2CMDR.all = 0x0020;			// Take I2C out of reset,Stop I2C when suspended
   I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
   I2caRegs.I2CMDR.bit.FREE = 0;			// Stop I2C when suspended
   I2caRegs.I2CMDR.bit.STT = 0;				// START condition bit
   I2caRegs.I2CMDR.bit.STP = 0;				// STOP condition bit
   I2caRegs.I2CMDR.bit.MST = 0;				// Slave mode
   I2caRegs.I2CMDR.bit.TRX = 0;				// Receiver mode
   I2caRegs.I2CMDR.bit.XA = 0;				// 7-bit addressing mode
   I2caRegs.I2CMDR.bit.RM = 0;				// Nonrepeat mode
   I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
   I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
   I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
   I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
   I2caRegs.I2CMDR.bit.BC = 0;				// 8 bits per data byte

   // FIFO not used
   
   return;
}


The writing one byte function:
uint16 I2CA_WriteData(uint16 addr, uint16 reg, uint16 data)
{
	I2caRegs.I2CMDR.bit.IRS = 1; 				// reset I2C

	// Make sure I2C is not busy and has stopped
	while (I2caRegs.I2CSTR.bit.BB == 1);		// busy loop
	I2caRegs.I2CSTR.bit.SCD = 1;				// Clear the SCD bit (stop condition bit)
	while(I2caRegs.I2CMDR.bit.STP == 1);		// stop bit loop

	I2caRegs.I2CSAR = addr;           			// I2C slave address
	while (I2caRegs.I2CSTR.bit.BB == 1);		// still busy?

    I2caRegs.I2CCNT = 2;						// assume register address = 1 byte, and data is 1 byte

	//I2caRegs.I2CMDR.all = 0x6E20; 				// start, stop, no rm, reset i2c  01101110 00100000
	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
	I2caRegs.I2CMDR.bit.FREE = 1;				// Run free I2C when suspended
	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
	I2caRegs.I2CMDR.bit.STP = 1;				// STOP condition bit
	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
	I2caRegs.I2CMDR.bit.TRX = 1;				// Transmitter mode
	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte

    while(I2caRegs.I2CSTR.bit.XRDY == 0); 		// Do nothing till bus is free
    I2caRegs.I2CDXR = reg;						// register address of the sensor (1 byte)

    while(I2caRegs.I2CSTR.bit.XRDY == 0); 		// Do nothing till bus is free
    I2caRegs.I2CDXR = data;						// data to be sent (1 byte)

	I2caRegs.I2CMDR.bit.STP = 1;  				// stop bit when CNT=0

    while(!I2caRegs.I2CSTR.bit.SCD);       	// wait for STOP condition

	return I2C_SUCCESS;
}

The reading one byte function:
uint16 I2CA_ReadData(uint16 addr, uint16 reg)
{
	Uint16 tempdata;

	I2caRegs.I2CMDR.bit.IRS = 1; 				// reset I2C

	// Make sure I2C is not busy and has stopped
	while (I2caRegs.I2CSTR.bit.BB == 1);		// busy loop
	I2caRegs.I2CSTR.bit.SCD = 1;				// Clear the SCD bit (stop condition bit)
	while(I2caRegs.I2CMDR.bit.STP == 1);		// stop bit loop

	I2caRegs.I2CSAR = addr;				// I2C slave address

	while (I2caRegs.I2CSTR.bit.BB == 1);		// still busy?

	//I2caRegs.I2CMDR.all = 0x2620;				// start, no stop bit, master, tx, reset I2C 00100110
	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
	I2caRegs.I2CMDR.bit.STP = 0;				// STOP condition bit
	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
	I2caRegs.I2CMDR.bit.TRX = 1;				// Transmitter mode
	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte

	I2caRegs.I2CCNT		= 1;					// assume register address is one byte

	while(I2caRegs.I2CSTR.bit.XRDY == 0); 		// Do nothing till bus is free
	I2caRegs.I2CDXR		= reg;					// register address of the sensor (1 byte)

	while(!I2caRegs.I2CSTR.bit.ARDY);			// all ready?
	//I2caRegs.I2CMDR.all = 0x2C20;				// start, stop bit when CNT =0, master, rx, reset I2C 00101100
	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
	I2caRegs.I2CMDR.bit.STP = 1;				// STOP condition bit
	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
	I2caRegs.I2CMDR.bit.TRX = 0;				// Receiver mode
	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte
	I2caRegs.I2CCNT		= 1;					// only read one byte data

	if(I2caRegs.I2CSTR.bit.NACK == 1)
	{
		 I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT; 	// 0x0002
	}
	I2caRegs.I2CMDR.bit.STP = 1;					// stop bit when CNT=0

	while(!I2caRegs.I2CSTR.bit.SCD);				// stop bit detected?

	tempdata	= I2caRegs.I2CDRR;					// read one byte data

	return(1);
}

The multi-read function I have tried:

uint16 I2CA_ReadDataN(uint16 addr, uint16 reg, uint16 len, uint16 *buf)
{
	uint8 i;

	I2caRegs.I2CMDR.bit.IRS = 1; 				// reset I2C

	// Make sure I2C is not busy and has stopped
	while (I2caRegs.I2CSTR.bit.BB == 1);		// busy loop
	I2caRegs.I2CSTR.bit.SCD = 1;				// Clear the SCD bit (stop condition bit)
	while(I2caRegs.I2CMDR.bit.STP == 1);		// stop bit loop

	I2caRegs.I2CSAR = addr;				// I2C slave address

	while (I2caRegs.I2CSTR.bit.BB == 1);		// still busy?
	//I2caRegs.I2CMDR.all = 0x2620;				// start, no stop bit, master, tx, reset I2C 00100110
	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
	I2caRegs.I2CMDR.bit.STP = 0;				// STOP condition bit
	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
	I2caRegs.I2CMDR.bit.TRX = 1;				// Transmitter mode
	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte

	I2caRegs.I2CCNT		= 1;					// assume register address is one byte
	while(I2caRegs.I2CSTR.bit.XRDY == 0); 		// Do nothing till bus is free
	I2caRegs.I2CDXR		= reg;					// register address of the sensor (1 byte)

	while(!I2caRegs.I2CSTR.bit.ARDY);			// all ready?
	//I2caRegs.I2CMDR.all = 0x2C20;				// start, stop bit when CNT =0, master, rx, reset I2C 00101100
	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
	I2caRegs.I2CMDR.bit.STP = 1;				// STOP condition bit
	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
	I2caRegs.I2CMDR.bit.TRX = 0;				// Receiver mode
	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte

	I2caRegs.I2CCNT		= len;					// only read one byte data

//	if(I2caRegs.I2CSTR.bit.NACK == 1)
//	{
//		 I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT; 	// 0x0002
//	}
//	I2caRegs.I2CMDR.bit.STP = 1;					// stop bit when CNT=0
//
//	while(!I2caRegs.I2CSTR.bit.SCD);				// stop bit detected?

	for (i = 0; i < len; i++)
	{
		while(I2caRegs.I2CSTR.bit.RRDY == 0); 		// Do nothing till bus is free
		buf[i] = I2caRegs.I2CDRR;
		I2caRegs.I2CMDR.all = 0x2C20;
	}

	return 1;
}

Thanks for your help!

:)

-Di

  • I need your help...
    :)
    -Di
  • Hi,

    You can see this document. I hope it's helpfull.

    3583.F28335 - I2C.pdf

    Regards,

    Phien Nguyen Thanh

  • Di,

    I see that you said that your following code works with single byte i2c writes and reads of the MPU6050. I have been trying to develop code that would just work period and had no success. I copied and pasted your code but I am seeing the exact same issues that I do with my code. So I must be addressing the wrong registers. I wanted to to check with you to see what are the exact values you input into your read and write functions to get the MPU6050 to work properly. I am inputting the following values into your functions:

    Slave address = 0x68

    Power management 1 register = 0xD6

    accelerometer x axis high byte register = 0x76

    I appreciate your help in getting my MPU6050 to work. Thank you!

  • Hi,

    I am very glad to share my codes with you.

    BTW I hope you can keep on trying to read/write several bytes with me.

    Thanks!

    -Di

    mpu6050.c
    # include "mpu6050.h"
    
    //static float gyro_coeff;
    
    
    //**************************************
    //��ʼ��
    //**************************************
    void MPU6050_Init()
    {
    	i2cWrite(MPU6050_ADDR, PWR_MGMT_1, 0x00);	//�������״̬
    	i2cWrite(MPU6050_ADDR, SMPLRT_DIV, 0x07);
    	i2cWrite(MPU6050_ADDR, CONFIG, 0x06);
    
    	//ѡ������
    	#if GYRO_SCALE == GYRO_SCALE_250dps
    	i2cWrite(MPU6050_ADDR, GYRO_CONFIG, 0x00);
    	//gyro_coeff = 131.072;
    	#endif
    	#if GYRO_SCALE == GYRO_SCALE_500dps
    	i2cWrite(MPU6050_ADDR, GYRO_CONFIG, 0x08);
    	//gyro_coeff = 65.536;
    	#endif
    	#if GYRO_SCALE == GYRO_SCALE_1000dps
    	i2cWrite(MPU6050_ADDR, GYRO_CONFIG, 0x10);
    	//gyro_coeff = 32.768;
    	#endif
    	#if GYRO_SCALE == GYRO_SCALE_2000dps
    	i2cWrite(MPU6050_ADDR, GYRO_CONFIG, 0x18);
    	//gyro_coeff = 16.384;
    	#endif
    
    	i2cWrite(MPU6050_ADDR, ACCEL_CONFIG, 0x00);
    }
    
    //**************************************
    //��ȡ���ϳ�����
    //**************************************
    int16 MPU6050_GetData(char REG_Address)
    {
    	int8 H,L;
    	H = i2cRead(MPU6050_ADDR, REG_Address);
    	L = i2cRead(MPU6050_ADDR, REG_Address+1);
    	return (H<<8)+L;   //�ϳ�����
    }
    
    
    

    mpu6050.h

    i2c.c
    # include "i2c.h"
    
    void I2CA_Init(void)
    {
       // Initialize I2C
       I2caRegs.I2CMDR.all = 0x0000;
       I2caRegs.I2CSAR = 0;		// Slave address
    
       I2caRegs.I2CPSC.all = 6;		    		// Prescaler - need 7-12 Mhz on module clk
       I2caRegs.I2CCLKL = 10;					// NOTE: must be non zero
       I2caRegs.I2CCLKH = 5;					// NOTE: must be non zero
    
       //I2caRegs.I2CIER.all = 0x24;			// Enable SCD & ARDY interrupts  0010 0100
       I2caRegs.I2CIER.bit.AAS = 0;				// Addressed as slave interrupt enable bit
       I2caRegs.I2CIER.bit.SCD = 1;				// Stop condition detected interrupt enable bit
       I2caRegs.I2CIER.bit.XRDY = 0;			// Transmit-data-ready interrupt enable bit
       I2caRegs.I2CIER.bit.XRDY = 0;			// Receive-data-ready interrupt enable bit
       I2caRegs.I2CIER.bit.ARDY = 1;			// Register-access-ready interrupt enable bit
       I2caRegs.I2CIER.bit.NACK = 0;			// No-acknowledgment interrupt enable bit
       I2caRegs.I2CIER.bit.ARBL = 0;			// Arbitration-lost interrupt enable bit
    
       //I2caRegs.I2CMDR.all = 0x0020;			// Take I2C out of reset,Stop I2C when suspended
       I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
       I2caRegs.I2CMDR.bit.FREE = 0;			// Stop I2C when suspended
       I2caRegs.I2CMDR.bit.STT = 0;				// START condition bit
       I2caRegs.I2CMDR.bit.STP = 0;				// STOP condition bit
       I2caRegs.I2CMDR.bit.MST = 0;				// Slave mode
       I2caRegs.I2CMDR.bit.TRX = 0;				// Receiver mode
       I2caRegs.I2CMDR.bit.XA = 0;				// 7-bit addressing mode
       I2caRegs.I2CMDR.bit.RM = 0;				// Nonrepeat mode
       I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
       I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
       I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
       I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
       I2caRegs.I2CMDR.bit.BC = 0;				// 8 bits per data byte
    
       //I2caRegs.I2CFFTX.all = 0x6000;			// Enable FIFO mode and TXFIFO
       I2caRegs.I2CFFTX.bit.I2CFFEN = 1;		// Enable the I2C FIFO mode
       I2caRegs.I2CFFTX.bit.TXFFRST = 1;		// Enable the transmit FIFO operation
       I2caRegs.I2CFFTX.bit.TXFFINTCLR = 0;		// Clear the TXFFINT flag
       I2caRegs.I2CFFTX.bit.TXFFIENA = 0;		// TXFFINT flag does not generate an interrupt when set
       I2caRegs.I2CFFTX.bit.TXFFIL = 0;			// Transmit FIFO interrupt level
    
       //I2caRegs.I2CFFRX.all = 0x2040;			// Enable RXFIFO, clear RXFFINT
       I2caRegs.I2CFFRX.bit.RXFFRST = 1;		// Enable the receive FIFO operation
       I2caRegs.I2CFFRX.bit.RXFFINTCLR = 1;		// Clear the RXFFINT flag
       I2caRegs.I2CFFRX.bit.RXFFIENA = 0;		// RXFFINT flag does generate an interrupt when set
       I2caRegs.I2CFFRX.bit.RXFFIL = 0;			// Receive FIFO interrupt level
    
       return;
    }
    
    uint16 I2CA_WriteData(uint16 addr, uint16 reg, uint16 data)
    {
    	I2caRegs.I2CMDR.bit.IRS = 1; 				// reset I2C
    
    	// Make sure I2C is not busy and has stopped
    	while (I2caRegs.I2CSTR.bit.BB == 1);		// busy loop
    	I2caRegs.I2CSTR.bit.SCD = 1;				// Clear the SCD bit (stop condition bit)
    	while(I2caRegs.I2CMDR.bit.STP == 1);		// stop bit loop
    
    	I2caRegs.I2CSAR = addr;           			// I2C slave address
    	while (I2caRegs.I2CSTR.bit.BB == 1);		// still busy?
    
        I2caRegs.I2CCNT = 2;						// assume register address = 1 byte, and data is 1 byte
    
    	I2caRegs.I2CMDR.all = 0x6E20; 				// start, stop, no rm, reset i2c  01101110 00100000
    //	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
    //	I2caRegs.I2CMDR.bit.FREE = 1;				// Run free I2C when suspended
    //	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
    //	I2caRegs.I2CMDR.bit.STP = 1;				// STOP condition bit
    //	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
    //	I2caRegs.I2CMDR.bit.TRX = 1;				// Transmitter mode
    //	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
    //	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
    //	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
    //	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
    //	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
    //	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
    //	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte
        I2caRegs.I2CDXR = reg;						// register address of the sensor (1 byte)
        I2caRegs.I2CDXR = data;						// data to be sent (1 byte)
    
    	I2caRegs.I2CMDR.bit.STP = 1;  				// stop bit when CNT=0
    
        while(!I2caRegs.I2CSTR.bit.SCD);       	// wait for STOP condition
    
    	return I2C_SUCCESS;
    }
    
    
    uint16 I2CA_ReadData(uint16 addr, uint16 reg)
    {
    	Uint16 tempdata;
    
    	I2caRegs.I2CMDR.bit.IRS = 1; 				// reset I2C
    
    	// Make sure I2C is not busy and has stopped
    	while (I2caRegs.I2CSTR.bit.BB == 1);		// busy loop
    	I2caRegs.I2CSTR.bit.SCD = 1;				// Clear the SCD bit (stop condition bit)
    	while(I2caRegs.I2CMDR.bit.STP == 1);		// stop bit loop
    
    	I2caRegs.I2CSAR = addr;				// I2C slave address
    
    	while (I2caRegs.I2CSTR.bit.BB == 1);		// still busy?
    
    	I2caRegs.I2CMDR.all = 0x2620;				// start, no stop bit, master, tx, reset I2C 00100110
    //	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
    //	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
    //	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
    //	I2caRegs.I2CMDR.bit.STP = 0;				// STOP condition bit
    //	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
    //	I2caRegs.I2CMDR.bit.TRX = 1;				// Transmitter mode
    //	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
    //	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
    //	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
    //	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
    //	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
    //	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
    //	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte
    
    	I2caRegs.I2CCNT		= 1;					// assume register address is one byte
    	I2caRegs.I2CDXR		= reg;					// register address of the sensor (1 byte)
    
    	while(!I2caRegs.I2CSTR.bit.ARDY);			// all ready?
    
    	I2caRegs.I2CMDR.all = 0x2C20;				// start, stop bit when CNT =0, master, rx, reset I2C 00101100
    //	I2caRegs.I2CMDR.bit.NACKMOD = 0;			// NACK mode bit
    //	I2caRegs.I2CMDR.bit.FREE = 0;				// Stop I2C when suspended
    //	I2caRegs.I2CMDR.bit.STT = 1;				// START condition bit
    //	I2caRegs.I2CMDR.bit.STP = 1;				// STOP condition bit
    //	I2caRegs.I2CMDR.bit.MST = 1;				// Master mode
    //	I2caRegs.I2CMDR.bit.TRX = 0;				// Receiver mode
    //	I2caRegs.I2CMDR.bit.XA = 0;					// 7-bit addressing mode
    //	I2caRegs.I2CMDR.bit.RM = 0;					// Nonrepeat mode
    //	I2caRegs.I2CMDR.bit.DLB = 0;				// Digital loopback mode is disabled
    //	I2caRegs.I2CMDR.bit.IRS = 1;				// The I2C module is enabled
    //	I2caRegs.I2CMDR.bit.STB = 0;				// The I2C module is not in the START byte mode
    //	I2caRegs.I2CMDR.bit.FDF = 0;				// Free data format mode is disabled
    //	I2caRegs.I2CMDR.bit.BC = 0;					// 8 bits per data byte
    //	I2caRegs.I2CCNT		= 1;					// only read one byte data
    
    	if(I2caRegs.I2CSTR.bit.NACK == 1)
    	{
    		 I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT; 	// 0x0002
    	}
    	I2caRegs.I2CMDR.bit.STP = 1;					// stop bit when CNT=0
    
    	while(!I2caRegs.I2CSTR.bit.SCD);				// stop bit detected?
    
    	tempdata	= I2caRegs.I2CDRR;					// read one byte data
    
    	return(tempdata);
    }
    
    
    uint8 i2cWrite(uint8 addr, uint8 reg, uint8 data)
    {
    	return I2CA_WriteData(addr, reg, data);
    }
    uint8 i2cRead(uint8 addr, uint8 reg)
    {
    	return I2CA_ReadData(addr, reg);
    }
    

    i2c.h

  • Hi Di,

    Have you managed to solve the problem?
    I am also having the same trouble when trying to read multiple bytes and can't figure out why it isn't working.

    Managed to solve the problem. If anyone crosses this post in the future, the multi-read function should end like:

        while(!I2caRegs.I2CSTR.bit.SCD);
        for (i = 0; i < len; i++)
        {
            buf[i] = I2caRegs.I2CDRR;
        }


    Best,
    Joao

  • Hello Joao,

    Thank you for the update!

    Best Regards,
    Adam Dunhoft
  • Hey Joao!

    Have you managed read out several registers at once? For example on the MPU 6050 from register 0x3B to 0x48 in a burst?

    At the moment i do think that the I2C read works only for one register, like:

    Start // Slave Address R/W=0 // ACK // Register Address // ACK // Slave Address R/W=1 // ACK // (n) *Data Byte From Register ... // ... (n) *Data Byte From Register ... // NACK // STOP

    But for examlpe the MPU only has one byte of data each register. So for me it looks like i can only read out one byte and need to start aigan incrementing register address on my own. Is there a autoincrement?

    Thanks!

  • Hi,

    I am actually communicating with a MPU9250, and I'm able to sequentially read multiple bytes at once.

    Have you enabled the RX and TX FIFO?

  • I've already solved the problem. I haven't known that there is an register autoincrement (depends on the slave i think). In addition, I had problems with the Bosch BMP280 API library, but now everything works.

    Thanks for your respond!