Author Topic: pic24fv32ka302 i2c  (Read 1375 times)

0 Members and 1 Guest are viewing this topic.

Offline CyberdukeTopic starter

  • Contributor
  • Posts: 18
  • Country: za
pic24fv32ka302 i2c
« on: January 17, 2018, 05:47:03 pm »
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.pdf
https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-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 copies and pasted and edited just what was absolutely necessary, Ill make it nice once it works)

Code: [Select]
#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]
  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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf