help using I2C functions generated by MCC
Posted: Fri Jan 19, 2018 11:17 am
Hi guys, I have acquired a pic24fv32ka302 and a mpu 6050 gyroscope and accelerator unit. See below all relevant documents/links for these units.
https://www.invensense.com/wp-content/u ... sheet1.pdf
https://www.invensense.com/wp-content/u ... r-Map1.pdf
https://www.microchip.com/wwwproducts/en/PIC24F32KA302
Now I recently graduated from arduinos where I have used I2C on this exact gyro unit. So I know the theory. I have been struggling to implement this on MPlab using my pic. I am stuck for a while now and would really appreciate some input. I have succesfully setup the UART so that I can read results using a serial monitor on my pc.
1. I setup the I2C using the microchip code configurator.(mcc) This generated a I2c.c and I2c.h file.
2. In the I2C.h file there is some examples. And I implemented one as follows, yeah I know I literally copied and pasted and edited just what was absolutely necessary, Ill make it nice once it works)
Then I call the function from my main using
So Using this http://ww1.microchip.com/downloads/en/D ... 00195f.pdf I have been trying to just manually also play around with the registers and it seems that a start condition never gets initiated.
now I know that the returned result will be rubbish since I am only reading 1 byte from a 16bit unsigned int(in the register of the gyro) but I am getting no result yet. Once I can read something, getting it right should be easy.
I am stuck for a while now so I would appreciate any help.
https://www.invensense.com/wp-content/u ... sheet1.pdf
https://www.invensense.com/wp-content/u ... r-Map1.pdf
https://www.microchip.com/wwwproducts/en/PIC24F32KA302
Now I recently graduated from arduinos where I have used I2C on this exact gyro unit. So I know the theory. I have been struggling to implement this on MPlab using my pic. I am stuck for a while now and would really appreciate some input. I have succesfully setup the UART so that I can read results using a serial monitor on my pc.
1. I setup the I2C using the microchip code configurator.(mcc) This generated a I2c.c and I2c.h file.
2. In the I2C.h file there is some examples. And I implemented one as follows, yeah I know I literally copied and pasted and edited just what was absolutely necessary, Ill make it nice once it works)
- Code: Select all
#define MCHP24AA512_RETRY_MAX 100 // define the retry count
#define MCHP24AA512_ADDRESS 0x68 // slave device address
uint8_t MCHP24AA512_Read(
uint16_t address,
uint8_t *pData,
uint16_t nCount)
{
I2C1_MESSAGE_STATUS status;
uint8_t writeBuffer[3];
uint16_t timeOut;
uint16_t counter;
uint8_t *pD, ret;
pD = pData;
for (counter = 0; counter < nCount; counter++)
{
// build the write buffer first
// starting address of the EEPROM memory
writeBuffer[0] = (address >> 8); // high address
writeBuffer[1] = (uint8_t)(address); // low low address
// Now it is possible that the slave device will be slow.
// As a work around on these slaves, the application can
// retry sending the transaction
timeOut = 0;
while(status != I2C1_MESSAGE_FAIL)
{
// write one byte to EEPROM (2 is the count of bytes to write)
I2C1_MasterWrite( writeBuffer,
2,
MCHP24AA512_ADDRESS,
&status);
// wait for the message to be sent or status has changed.
while(status == I2C1_MESSAGE_PENDING);
if (status == I2C1_MESSAGE_COMPLETE)
break;
// if status is I2C_MESSAGE_ADDRESS_NO_ACK,
// or I2C_DATA_NO_ACK,
// The device may be busy and needs more time for the last
// write so we can retry writing the data, this is why we
// use a while loop here
// check for max retry and skip this byte
if (timeOut == MCHP24AA512_RETRY_MAX)
break;
else
timeOut++;
}
if (status == I2C1_MESSAGE_COMPLETE)
{
// this portion will read the byte from the memory location.
timeOut = 0;
while(status != I2C1_MESSAGE_FAIL)
{
// write one byte to EEPROM (2 is the count of bytes to write)
I2C1_MasterRead( pD,
1,
MCHP24AA512_ADDRESS,
&status);
// wait for the message to be sent or status has changed.
while(status == I2C1_MESSAGE_PENDING);
if (status == I2C1_MESSAGE_COMPLETE)
break;
// if status is I2C_MESSAGE_ADDRESS_NO_ACK,
// or I2C_DATA_NO_ACK,
// The device may be busy and needs more time for the last
// write so we can retry writing the data, this is why we
// use a while loop here
// check for max retry and skip this byte
if (timeOut == MCHP24AA512_RETRY_MAX)
break;
else
timeOut++;
}
}
// exit if the last transaction failed
if (status == I2C1_MESSAGE_FAIL)
{
ret = 0;
break;
}
pD++;
address++;
}
return (ret);
}
Then I call the function from my main using
- Code: Select all
uint16_t GyroData1 = 0;
GyroData1 = MCHP24AA512_Read(63,0x68,1);
UartSentint(GyroData1);
So Using this http://ww1.microchip.com/downloads/en/D ... 00195f.pdf I have been trying to just manually also play around with the registers and it seems that a start condition never gets initiated.
now I know that the returned result will be rubbish since I am only reading 1 byte from a 16bit unsigned int(in the register of the gyro) but I am getting no result yet. Once I can read something, getting it right should be easy.
I am stuck for a while now so I would appreciate any help.