pic24fv32ka302 I2C connection help

Discussion in 'Embedded Systems and Microcontrollers' started by Cyberduke, Jan 17, 2018.

  1. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    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 copied and pasted and edited just what was absolutely necessary, Ill make it nice once it works)

    Code (Text):
    1.  
    2. #define MCHP24AA512_RETRY_MAX 100 // define the retry count
    3. #define MCHP24AA512_ADDRESS 0x68 // slave device address
    4.  
    5.  
    6. uint8_t MCHP24AA512_Read(
    7.                                             uint16_t address,
    8.                                             uint8_t *pData,
    9.                                             uint16_t nCount)
    10.             {
    11.                 I2C1_MESSAGE_STATUS status;
    12.                 uint8_t writeBuffer[3];
    13.                 uint16_t timeOut;
    14.                 uint16_t counter;
    15.                 uint8_t *pD, ret;
    16.  
    17.                 pD = pData;
    18.  
    19.                 for (counter = 0; counter < nCount; counter++)
    20.                 {
    21.  
    22.                     // build the write buffer first
    23.                     // starting address of the EEPROM memory
    24.                     writeBuffer[0] = (address >> 8); // high address
    25.                     writeBuffer[1] = (uint8_t)(address); // low low address
    26.  
    27.                     // Now it is possible that the slave device will be slow.
    28.                     // As a work around on these slaves, the application can
    29.                     // retry sending the transaction
    30.                     timeOut = 0;
    31.                     while(status != I2C1_MESSAGE_FAIL)
    32.                     {
    33.                         // write one byte to EEPROM (2 is the count of bytes to write)
    34.                         I2C1_MasterWrite( writeBuffer,
    35.                                                 2,
    36.                                                 MCHP24AA512_ADDRESS,
    37.                                                 &status);
    38.  
    39.                         // wait for the message to be sent or status has changed.
    40.                         while(status == I2C1_MESSAGE_PENDING);
    41.  
    42.                         if (status == I2C1_MESSAGE_COMPLETE)
    43.                             break;
    44.  
    45.                         // if status is I2C_MESSAGE_ADDRESS_NO_ACK,
    46.                         // or I2C_DATA_NO_ACK,
    47.                         // The device may be busy and needs more time for the last
    48.                         // write so we can retry writing the data, this is why we
    49.                         // use a while loop here
    50.  
    51.                         // check for max retry and skip this byte
    52.                         if (timeOut == MCHP24AA512_RETRY_MAX)
    53.                             break;
    54.                         else
    55.                             timeOut++;
    56.                     }
    57.  
    58.                     if (status == I2C1_MESSAGE_COMPLETE)
    59.                     {
    60.  
    61.                         // this portion will read the byte from the memory location.
    62.                         timeOut = 0;
    63.                         while(status != I2C1_MESSAGE_FAIL)
    64.                         {
    65.                             // write one byte to EEPROM (2 is the count of bytes to write)
    66.                             I2C1_MasterRead( pD,
    67.                                                     1,
    68.                                                     MCHP24AA512_ADDRESS,
    69.                                                     &status);
    70.  
    71.                             // wait for the message to be sent or status has changed.
    72.                             while(status == I2C1_MESSAGE_PENDING);
    73.  
    74.                             if (status == I2C1_MESSAGE_COMPLETE)
    75.                                 break;
    76.  
    77.                             // if status is I2C_MESSAGE_ADDRESS_NO_ACK,
    78.                             // or I2C_DATA_NO_ACK,
    79.                             // The device may be busy and needs more time for the last
    80.                             // write so we can retry writing the data, this is why we
    81.                             // use a while loop here
    82.  
    83.                             // check for max retry and skip this byte
    84.                             if (timeOut == MCHP24AA512_RETRY_MAX)
    85.                                 break;
    86.                             else
    87.                                 timeOut++;
    88.                         }
    89.                     }
    90.  
    91.                     // exit if the last transaction failed
    92.                     if (status == I2C1_MESSAGE_FAIL)
    93.                     {
    94.                         ret = 0;
    95.                         break;
    96.                     }
    97.  
    98.                     pD++;
    99.                     address++;
    100.  
    101.                 }
    102.                 return (ret);
    103.  
    104.             }
    105.  
    Then I call the function from my main using
    Code (Text):
    1.  
    2.   uint16_t GyroData1 = 0;
    3.  
    4.     GyroData1 = MCHP24AA512_Read(63,0x68,1);
    5.         UartSentint(GyroData1);
    6.  
    I think my main problem is that I am unsure of what all the parameters are/mean. I have tried quite a bit different things etc.
    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.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,436
    703
  3. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    Trust me if I say I did read the datasheet. That is how I got the Uart,Adc,interrupts,IO etc. working

    But I2C is somehow beating me. The whole purpose of the MCC is not ease the use of this. I feel like I might be close but is still struggling.
     
  4. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,696
    335
    MCC There is only one problem with it that's when it don't work LOL
    I just spent 5 days trying to get it to work just to figure out it was naming it's own files wrong.

    If you want help you have to zip your project there is no way to help you with just a line or two of code
    The MMC makes like 20 files and mplabx don't haft the time even come close to where you going wrong it build a bad file.
     
  5. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    Yeah, I believe the error is going to be something weird as that. (Or my absolute inability to work with pointers) Thank you

    See attached my whole project.
     
  6. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,696
    335
    I looked at your code you missed a lot with the MMC setting up i2c and uart
     
  7. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    The Uart is actually working.

    Thank you, I will go and look further into it.
    Do you maybe have a link to a guide or something where setting up with MCC is explained thoroughly?
     
  8. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    Below is the only setup options I have for I2C in MCC. So I reckon I must re read the datasheet again to set the right registers there on the right tab?



    upload_2018-1-18_19-21-44.png upload_2018-1-18_19-22-1.png
     
  9. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,696
    335
  10. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    So I am reading the datasheet again and this is where I get stuck.

    According to what I read the I2CxSTAT<4> bit needs to be 1 in order for the bus to be in idle and ready to be used. Now this always is zero. (Figure 5-2: shows very clear)

    I tried by setting the following bit I2C1CONbits.I2CEN = 1;
    and also calling the initializing function from the I2C generated library from mcc.

    I2C1_Initialize();

    But the bus does not seem to go to idle. (I checked the clock line with my scope and it seems to stay high)
     
  11. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,436
    703
    Did anyone notice that we keep linking APPLICATION NOTE and the dude keep talking about datasheet?
     
  12. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,696
    335
    I posted the one he needs "APPLICATION NOTE" i don't no that much about 6050 gyroscope and accelerator unit.
    But I've messed alot with MMC and I2C You don't have it set to work on it's own.
     
  13. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,436
    703
    You posted the one that I posted yesterday.
     
  14. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,696
    335
    Good deal guess we both no where to look huh.
    It's in the datasheet I been using the 16F18855 there a great chip but you got read them appnote's not just datasheet.
     
  15. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    The abovementioned document(http://ww1.microchip.com/downloads/en/DeviceDoc/70000195f.pdf) is found under the "Reference Manual" Tab on the website, not APPLICATION NOTE. Is it still called an application note then?
    Anyway sorry If I offended anyone by referring to that bunch of documents all as the datasheet.
     
  16. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,436
    703
    "I say what I mean.
    I mean what I say."

    English is not my first language. For this reason I strive to be precise when I use it.
     
  17. Cyberduke

    Thread Starter Active Member

    Mar 5, 2011
    49
    1
    English is not my home language either. so once again apologies for errors.

    So I plugged an arduino back in and looked at the I2C comms over a scope. So at this point I can probably draw an I2C conversation happening.
    But when I plug the pic back in the Data line never gets pulled low to start a conversation. (Start condition). Doesn't matter what I do. I have tried soo many approaches etc.
    if you look here http://ww1.microchip.com/downloads/en/DeviceDoc/70000195f.pdf then it says the P status bit (page 21) needs to be in idle then you can call a start condition. But the P status bit also never goes to idle.(HIGH) This convinces me that I am setting this up wrong.
    I really would appreciate it if anyone just could maybe give me a pointer maybe of what register I am forgetting or something so I can actually start the conversation.

    I don't want the complete answer, but any point in the right direction will really be appreciated.
     
Loading...