I2C Comms with Orientation Sensor

This forum handles questions and discussions concerning Microchip’s MPLAB® Code Configurator (MC2).

Re: I2C Comms with Orientation Sensor

Postby GettinBetter » Thu Nov 21, 2019 10:51 pm

Arrgh... Well...progress...
With the help of this thread https://www.microchip.com/forums/m1020538.aspx#1022180 Post #8 ...
I proceed to change connections as stated (because I have the an Explorer board) and voila signals on the analyser akin to the expected..
Not out of the woods yet but .. still its progress. :)

Now trying to fix the errors... :?
Yes, the 0x34 was stupid, it was late and I was desperate. but now that I've got output confirming (I hope, haven't really looked yet) the start event was triggered. Although at first glance it looks as if the labels/pins are the wrong way round, I'd prefer the SCL at the top.

Now a very happy chappie :D :D
Attachments
Eureka2.jpg
Eureka2.jpg (46.11 KiB) Viewed 3700 times
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: I2C Comms with Orientation Sensor

Postby ric » Thu Nov 21, 2019 11:03 pm

Yes, plainly SCL and SDA are swapped.
That trace shows an address of 0b01010001 (0x51) being sent, and a high (=NAK) during the ACK cycle.
The small negative glitch straight after the ACK cycle is unusual, I wonder if it's coming from the Master or the Slave. It occurs during the SCL low cycle, so doesn't affect anything.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: I2C Comms with Orientation Sensor

Postby GettinBetter » Sun Dec 01, 2019 1:55 pm

Hi Guys,
Ok, So I'm back on this now, UART text sorted, but got caught out not saving code history. So by the time I tried the analyser I had modified the code in MPLABX without actually downloading it to the PIC.
I get my stuff delivered to my works address, and having received the analyser rushed home to play with it. Plugged straight into the 'old' version of the program already in the PIC and much to my amazement, it had something going on.
Anyway I'm back to the drawing board. I do know that I took the example code from the I2C.h generated file which gives an example of reading an EEPROM . I removed irrelevant stuff from the example such as the write buffer (as the sensor I am trying to communicate with only requires an single byte address), kept all the timeout & retry loops added some generic delays, but struggling a little with the code. Although it compiles, I have nothing from the analyser, except the UART pin which is working as normal.

None of the debug print statements are executed so it doesn't appear to be entering the I2C wrapper function.

Would appreciate any words of wisdom or suchlike if they're offered...

Here's the code & errors...

Code: Select all
/**
  Section: Included Files
*/
#include "mcc_generated_files/system.h"
#include <libpic30.h> //Defines __delay32();
#include <stdio.h> // Defines printf("");
#include <stdint.h>
#include "mcc_generated_files/uart1.h"
#include "mcc_generated_files/i2c1.h"


const uint16_t BNO_I2C_DEV_ADDR1  = 0x28;    //  define BNO device default address
const uint8_t BNO_TEMPREG_ADDR   = 0x34;    //  define Temperature register to read from
const uint8_t BNO_RETRY_MAX      = 100;     //  define the retry count
const uint8_t BNO_DEVICE_TIMEOUT = 50 ;     //  define slave timeout

    uint16_t *pDev_Address = &BNO_I2C_DEV_ADDR1;
    uint8_t *pReg_Address = &BNO_TEMPREG_ADDR;
    uint8_t DataRCVD;
    uint8_t *pDataRCVD;
    uint8_t status;
    uint8_t nBytes = 1;

//  MEANINGS OF STATUS INDEX NUMBERS IN THE PRINT STATEMENTS
//    0 - I2C1_MESSAGE_FAIL,
//    1 = I2C1_MESSAGE_PENDING,
//    2 - I2C1_MESSAGE_COMPLETE,
//    3 - I2C1_STUCK_START,
//    4 - I2C1_MESSAGE_ADDRESS_NO_ACK,
//    5 - I2C1_DATA_NO_ACK,
//    6 - I2C1_LOST_STATE
   
 // Start Wrapper Function
            uint8_t BNO055_Read_Temp(uint16_t pDev_Address, uint8_t nBytes, uint8_t pReg_Address,uint8_t pDataRCVD)
            {
                I2C1_MESSAGE_STATUS status;
                uint16_t retryTimeOut, slaveTimeOut;
                printf("Read Function initiated..\r\n");         // Print prog status to terminal for debug;
                retryTimeOut = 0;                       //Initialise timeout & retry variables to zero
                slaveTimeOut = 0;
                    while(status != I2C1_MESSAGE_FAIL)
                    {
                        I2C1_MasterWrite(pReg_Address,nBytes,pDev_Address,&status);   // start write
                         printf("Write initiated..\r\n");         // Print prog status to terminal for debug;
                        while(status == I2C1_MESSAGE_PENDING)
                            {
                                __delay32(50000);
                                if (slaveTimeOut == BNO_DEVICE_TIMEOUT)
                                    return (0);
                                else
                                    slaveTimeOut++;                                 // slave timeout inc
                            }
                    if (retryTimeOut == BNO_RETRY_MAX)
                        break;
                    else
                        retryTimeOut++;                                         // retry timeout inc
                           
                    if (status == I2C1_MESSAGE_COMPLETE)
                        break;
                    }                                                                // status write complete
                    if (status == I2C1_MESSAGE_COMPLETE)
                        {
                            retryTimeOut = 0;                                           // reset counters
                            slaveTimeOut = 0;
                        }
                                    while(status != I2C1_MESSAGE_FAIL)
                                    {
                                        I2C1_MasterRead(pDataRCVD,nBytes,pDev_Address,&status);   // start read

                                    while(status == I2C1_MESSAGE_PENDING)
                                        {
                                            __delay32(50000);
                           
                                        if (slaveTimeOut == BNO_DEVICE_TIMEOUT)
                                            return (0);
                                        else
                                            slaveTimeOut++;                                 // slave timeout inc
                               
                                        if (status == I2C1_MESSAGE_COMPLETE) 
                                            break;
                                        if (retryTimeOut == BNO_RETRY_MAX)
                                            break;
                                        else
                                            retryTimeOut++;                                     // retry timeout inc
                                        }
                                if (status == I2C1_MESSAGE_FAIL)
                                {
                                    return(0);
                                    break;                                                      // read fail
                                }
                 return(1);
                                    }
        return(status);         
            }



/*
                         Main application
 */
int main(void)
{
// initialise the device
    SYSTEM_Initialize();
//  ALSO INITIALISED IN SYSTEM.C ARE
//    PIN_MANAGER_Initialise();
//    CLOCK_Initialise();
//    INTERRUPT_Initialise();
//    UART1_Initialise();
//    I2C1_Initialise();
//  ************************************** 
   
    printf("Explorer 16/32, ICD3, PIC24FJ256GB110 PIM\n\r");
    printf("MPLABX, MMC, XC16\n\r");
    printf("BNO055 temperature out to terminal test - 01-12-19 \n\n\r");
     
   
    while (1)
    {
        // Call Wrapper Function
         uint8_t BNO055_Read_Temp(uint16_t pDev_Address, uint8_t nBytes, uint8_t pReg_Address,uint8_t pDataRCVD);
        __delay32(50000);
        DataRCVD = *pDataRCVD;
        printf("BNO055 Temp is  %u , Status is: %u\r", DataRCVD,status);
        __delay32(50000);   
    }

}
/**
 End of File
*/


main.c:61:30: warning: initialization discards qualifiers from pointer target type
main.c:62:29: warning: initialization discards qualifiers from pointer target type
main.c: In function 'BNO055_Read_Temp':
main.c:87:25: warning: passing argument 1 of 'I2C1_MasterWrite' makes pointer from integer without a cast
mcc_generated_files/i2c1.h:274:6: note: expected 'uint8_t *' but argument is of type 'uint8_t'
main.c:112:41: warning: passing argument 1 of 'I2C1_MasterRead' makes pointer from integer without a cast
mcc_generated_files/i2c1.h:447:6: note: expected 'uint8_t *' but argument is of type 'uint8_t'


Regards
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Previous

Return to MPLAB® Code Configurator

Who is online

Users browsing this forum: No registered users and 4 guests

cron