pic24fv32ka302 i2c  - Page 1
Forum section: electronics / general

author: Cyberduke
author_rank: Contributor
timestamp: January 17, 2018, 05:47:03 pm
content: Hi guys, I have acquired the above mentioned pic and a mpu 6050 gyroscope and accelerator unit. See below all relevant documents/links for these units.https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdfhttps://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdfhttps://www.microchip.com/wwwproducts/en/PIC24F32KA302Now 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 copies and pasted and edited just what was absolutely necessary, Ill make it nice once it works)Code:#define MCHP24AA512_RETRY_MAX 100 // define the retry count#define MCHP24AA512_ADDRESS 0x68 // slave device addressuint8_t MCHP24AA512_Read(uint16_t address,uint8_t *pData,uint16_t nCount){I2C1_MESSAGE_STATUS status;uint8_t writeBuffer;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 memorywriteBuffer = (address >> 8); // high addresswriteBuffer = (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 transactiontimeOut = 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 byteif (timeOut == MCHP24AA512_RETRY_MAX)break;elsetimeOut++;}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 byteif (timeOut == MCHP24AA512_RETRY_MAX)break;elsetimeOut++;}}// exit if the last transaction failedif (status == I2C1_MESSAGE_FAIL){ret = 0;break;}pD++;address++;}return (ret);}Then I call the function from my main usingCode:uint16_t GyroData1 = 0;MCHP24AA512_Read(63,&GyroData1,1);UartSentint(GyroData1);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.