• AVR Freaks

Hot!I2C on PIC32

Author
PeterRQ
Super Member
  • Total Posts : 306
  • Reward points : 0
  • Joined: 2012/03/14 01:30:36
  • Location: 0
  • Status: offline
2012/09/07 04:28:09 (permalink)
0

I2C on PIC32

I am attempting to get I2C communication running using the PIC32 Ethernet Evaluation Kit & I/O Expansion board.
I have implemented a bitbang solution which communicates with a Keller 4LD pressure sensor. I can read temperature and pressure, watch temperature rise as I hold sensor etc.
I would like to use the hardware I2C module and here the fun starts.
My first attempt was to use the I2CSendByte(channel, value) style functions (PIC32MX Peripheral Library).
 

I2CConfigure(I2C2, I2C_EN);
I2CSetFrequency(I2C2, PB_FREQ, PRESSURE_BPS);
I2CEnable(I2C_CHANNEL, TRUE);
I2CClearStatus(I2C2, 0x400);
I2CStart(I2C2);
I2CSendByte(I2C2, 0);
I2CSendByte(I2C2, 0xA5);

(the sensor's default address is 0)
Produces no clock.
 
Next I try the older style routines:

OpenI2C2(I2C_EN, brg);
IdleI2C2();
StartI2C2();
IdleI2C2();
MasterWriteI2C2(0);
IdleI2C2();
MasterWriteI2C2(0xA5);
IdleI2C2();
StopI2C2();
IdleI2C2();

Which I catch on my logic analyser as:
S, GC(0),A, HW 52,A,P
see enclosed trace.

Why has 0xA5 become 0x52 ????
 
I have checked the logic analyser trace and it seems that the logic analyser is decoding correctly.
Ideally I would use the I2CSendByte(i2c_chan,...) style of routines as that style of code would be easier to configure.
Either way I could do with some help as this one has me stumped. 
 

Attached Image(s)

#1

12 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:I2C on PIC32 2012/09/07 13:43:54 (permalink)
    0
    Hmmm, 52 is 1/2 of A5 ... strange though...

    GENOVA :D :D ! GODO
    #2
    PeterRQ
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2012/03/14 01:30:36
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2012/09/10 04:09:08 (permalink)
    0
    Indeed.
    I believe that the logic analyser has decoded the data correctly as if I bit bang my command 0xA5 then the device signals busy for 10 mS whilst it performs a conversion, which is as expected.
    Using the hardware I2C and 0x52 & the device is ready within 1 mS.
    Bit shifting suggests that I screwed the device initialisation, configured it whilst on or something but I cannot see it.
    Part of my confusion is that there are two libraries. Which should I use?
    Secondly the I2C posts I have found talk about some versions of the libraries not working.
    Will I be ok with MPLAB 8.84?
    As always; baseline code someone can give a big thumbs up to with PIC32 helps!
    #3
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:I2C on PIC32 2012/09/10 04:12:57 (permalink)
    0
    I mostly used I2C with software libraries, especially due to starting with baseline PICs and with the "plus" that I could use any pin for them.
    Anyway, you might want to try accessing hardware directly, just to be sure about libraries...

    GENOVA :D :D ! GODO
    #4
    VenkatA
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2011/09/15 09:33:55
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2012/09/10 09:16:39 (permalink)
    0
     
    Peter,
    Please try the PIC32 I2C code example and modify as you need.
    The link below is for the PIC32MX795F512L.
    http://ww1.microchip.com/downloads/en/DeviceDoc/PIC32_I2C_CodeExample_101911.zip
    #5
    PeterRQ
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2012/03/14 01:30:36
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2012/09/13 05:07:41 (permalink)
    0
    Thanks.
    Looking at the routines then the code example repeats what I have already done.
     
    If I send:
    S, 0x00, 0xA5, P
    then I get:
    S, 0x00, A, 0x52, A, P
    (slave recognises address but command is corrupted).
    If I send:
    S, 0x10, 0xA5, P
    then I get:
    S, 0x10, N, 0xA5, N, P
    (slave does not recognise address but command is not corrupt)
     
    Seems that there is some difference in using slave address 0. 
    Unfortunately my device comes as slave address 0.
    #6
    PeterRQ
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2012/03/14 01:30:36
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2012/09/14 03:59:41 (permalink)
    0
    I have now got writes to work.
    I had mistyped the command as A5 rather than AC.
    Quite why the Logic Analyser decodes A5 as 52 is beyound me.......
     
    I am now stuck reading from the device. If I:
    I2CStart(I2C2);
    Delay();
    I2CSendByte(I2C2, 1);
    Delay();
    I2CReceiverEnable(I2C2, TRUE);
    while(!I2CReceivedDataIsAvailable(I2C2)) {}
    Then I end up stuck waiting for data.
    The slave has acknowledged the read, both SCL and SDA are low.
    When I bit bang I have to clock SCL. How do I make the PIC HW clock SCL?
    #7
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:I2C on PIC32 2012/09/14 06:03:30 (permalink)
    0
    for bitbanging a I2C, usually Pin is set to 0 (LATx) and then TRIS is used to make it a "dominant" 0 (pull down) or a "recessive" 1 (open collector + resistor)

    GENOVA :D :D ! GODO
    #8
    PeterRQ
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2012/03/14 01:30:36
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2012/09/14 08:40:53 (permalink)
    0
    Thanks, I have that and my bit bang works.
    I would like to use the PIC 32MX hardware.
    Now that I understand my logic analyser's 'issues' (and my typo) I can see that I am correctly sending I2C data.
    So all I now need to know is how do I encourage the PIC32 HW to produce the clock signal when I am reading from the slave.
    I tried the function calls as previously posted, but no clock is produced.
    If I was SPI then I would send a dummy byte but I2C only has SDA so that does not appear correct approach.
     
    #9
    rpg7
    Super Member
    • Total Posts : 1374
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re:I2C on PIC32 2012/09/14 11:27:10 (permalink)
    0
    PeterRQ
    So all I now need to know is how do I encourage the PIC32 HW to produce the clock signal when I am reading from the slave.
    I tried the function calls as previously posted, but no clock is produced.

    Sorry, I don't use the libraries. Here is how I read using register access.

      I2C1CONbits.RCEN = 1;
       while (I2C1CONbits.RCEN) continue;
      *dat = I2C1RCV;
      dat++;
     

    I'm not that hot with the comments. But maybe have a look at the data sheet.
    #10
    EmbeddedEngineer
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2016/02/24 06:56:35
    • Location: 0
    • Status: offline
    Re:I2C on PIC32 2019/04/10 03:00:44 (permalink)
    0
    where to get all code examples of PIC32MZ family modules based on register level 
    #11
    Mysil
    Super Member
    • Total Posts : 3321
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re:I2C on PIC32 2019/04/10 05:05:36 (permalink)
    0
    Hi,
    There isn't many examples for PIC32MZ demonstrating register level programming,
    and what there is, isn't collected in one place.
     
    This forum: General PIC32 Topics is maybe the closest you will get.
     
    In the product webpages for PIC32MZ devices, there are a couple of register access examples hidden in plain sight, between all the Harmony stuff.
    https://www.microchip.com...s/en/PIC32MZ2048EFG100
    Select the Documents tab, and scroll down to Code Examples.
     
    Many peripherals in PIC32MZ devices,
    are very similar to corresponding peripherals in PIC32MX, PIC24 and dsPIC33 devices, 
    so with sensible coding, and use of device support files: <xc.h>,
    it is possible to make code portable between these devices, for many peripherals.
     
    There are differences in Oscillator Setup, many more options in MZ, especially around PLL.
    On top of that, there are Errata on Crystal oscillators, so most development boards use selfcontained external oscillator modules.
     
    Differences in Interrupt handling: All IRQ and Vector numbering is different.
    There are other differences and some tricks with Interrupt setup also, most will be taken care of by the compiler.
     
    ADC in PIC32MZ is the same as in PIC32MK, and very different from PIC32MX.
    Family Reference Manual for the ADC, have good descriptions, and source code examples using register access.
     
    I have example program running on PIC32MZ2048EFG100
    on a Digilent Wi-Fire board:
    I2C Master and Slave,
    FatFs filesystem from ELM Chan writing to Micro SD card,
    ADC inputs from on-board Pot and Power voltage divider, and from external Analog connector,
    UART output  U4TX to RF8 (and U4RX input from RF2)  to USB bridge, at 38400 bit/s.
     
    But with FatFs  long filename configuration enabled, and corresponding Unicode support source file,
    the Package file is 794 kB, and that is too large to upload in this Forum.
     
        Mysil
    post edited by Mysil - 2019/04/10 05:33:14
    #12
    MisterHemi
    Junior Member
    • Total Posts : 112
    • Reward points : 0
    • Joined: 2017/11/02 12:24:21
    • Location: 0
    • Status: online
    Re:I2C on PIC32 2019/04/11 17:57:59 (permalink)
    0
    Note 0xA5 when right shifted (>>) is 0x52 
    which is the same as dividing by 2, as DarioG said.

    My configuration:
    MacBook Pro (Retina, 15-inch, Mid 2015) with MacOS High Sierra (10.13.3) and MPLAB X IDE v4.15
     
    Curiosity PIC MZ EF, PIC24F Curiosity, and XPRESS EVAL BOARD (PIC16F18855).
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5