• AVR Freaks

Hot!How do I implement I2C on PIC18F25K50?

Author
zavodnyrichard
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2021/01/26 23:20:33
  • Location: 0
  • Status: offline
2021/03/01 04:05:53 (permalink)
0

How do I implement I2C on PIC18F25K50?

Hi, I have three slave devices (ADS1114) and I need them to work with PIC18F25K50 using I2C. I heard XC8 compiler has I2C library, but I can't find it. I found also this on the net but it doesn't work. 

Thank you in advance.
#1

5 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How do I implement I2C on PIC18F25K50? 2021/03/01 04:16:08 (permalink)
    +1 (1)
    Old versions of XC8 (up to version 1.36) used to come with PLIB ("Peripheral Libraries").
    They were discontinued, and Microchip now want you to use MCC to generate the equivalent code for newer PIC devices.
     
     
    That code you linked to is for a PIC16F877, which is much older than your PIC18F25K50.
    It's hard to know what you tried that didn't work, as that page shows lots of building blocks for both Master and Slave code.
    You really need to have a good understanding of how I2C works to get this sort of thing working nicely.
    Have you read the Wikipedia page about I2C, or read the NXP specificaton for I2C ?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How do I implement I2C on PIC18F25K50? 2021/03/01 10:03:38 (permalink)
    +1 (1)
    Hi,
    Have started looking at the example code provided by electrosome,
    and it look like a reasonable starting point, although some mistakes stand out immediately.
     
    It have been pointed out many times in this forum,
    that there shall be No #include files Before #pargma config ... statements.
    Then different PIC microcontrollers have different specifications for configuration settings,
    so corrections will be needed for PIC18F25K50.
     
    Also, that both #include <xc.h>
    and the device specific hardware support header file are not needed.
    //#include <xc.h>                /* This shall be After #pragma config ... // configuration settings, Mysil. */
    //#include <pic16f877a.h>        /* Remove this, Mysil. */  
     
     
        Mysil
    #3
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How do I implement I2C on PIC18F25K50? 2021/03/01 10:53:07 (permalink)
    +1 (1)
    The MSSP peripheral used for I2C communication in PIC16F877  is  very similar to the corresponding
    MSSP  peripheral in PIC18F25K50, at least when regarding I2C Master Mode.
     
    Both PIC16F877  and PIC18F25K50  have just One I2C peripheral module, 
    but PIC18F25K50  belong in a family of PIC microcontrollers, where some products have 2 instances of the MSSP peripheral.
    Thus naming of SFR registers are different, 
    so you may have to change names of registers starting with SSP...
    to SSP1...
     
    Apart from this, code provided by electrosome is very sloppy with regards to error checking and handling,
    but that is the same with a lot of I2C example code published in this forum,
    and code provided by Microchip Code Configurator (MCC) example code generator, is no better.
     
        Mysil
     
    #4
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How do I implement I2C on PIC18F25K50? 2021/03/01 11:26:02 (permalink)
    +2 (2)
    Note, that I2C addresses specified in Table 4 of datasheet for the ADS1114   devices,
    are 7-bit I2C addresses: 0x48, 0x49, 0x4A and 0x4B
    When transmitted by the I2C peripheral,
    these address values shall be shifted into the high 7 bits of 8 bit SSP1BUF register. 
    Thus becoming 0x90, 0x92, 0x94 or 0x96  for 8-bit Write address,
    and 0x91, 0x93, 0x95 or 0x97 for 8-bit Read address.
     
        Mysil
     
     
    #5
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How do I implement I2C on PIC18F25K50? 2021/03/01 14:33:00 (permalink)
    +2 (2)
    Because the PIC18F25K50  device have USB hardware,
    pins RC3 and RC4 are Not used for I2C signals.
    Instead, RB1 and RB0 are used for I2C signaling.
     
    Attached is a modified of the Master part of project linked to in original message #1.
    It have been corrected to compile without warnings for PIC18F25K50.
    Using MPLAB X v5.45  and XC8 v2.31
    Program have not been tested on real hardware.
     
        Mysil
    post edited by Mysil - 2021/03/01 14:41:22
    #6
    Jump to:
    © 2021 APG vNext Commercial Version 4.5