Menu

PIC16F18857 AND I2C

jackjames
2019-08-29
2019-08-29
  • jackjames

    jackjames - 2019-08-29

    I have to use the I2C bus with a PIC16F18857 as master.
    I have to read and write on the eeprom 24C32 mounted in the DS3231 module.
    To do this I decided to use the SDA1 and SCL1 pins of the micro.
    In no way have I been able to talk to these modules, practically the module in question remains inactive.
    The hardware is mounted exactly, it is not the first time I use these functions, but it is the first time I use this micro.
    I'm probably wrong about something... but what?
    I attach a source of tests.

     
  • Anobium

    Anobium - 2019-08-29

    Does not look in correct.

    Have you used I2C Discovery to find the I2C devices? Use one of the demos as your basis.
    Have you got pullsup on the SDA and SCL lines?

    The 16f18857 is a sister part to the 16F18855. And the 16F18855 has been extensively tested by use and Microchip - so, I do not think this library is at fault - I may wrong but I would be check the items mentioned above.

    :-)

     
  • Anobium

    Anobium - 2019-08-29

    I have spotted something.

    Please upload the following (as attachments)
    1. 16f18857.dat
    2. 16f18855.dat

    The FamilyVariant may be incorrect !! Which is correct.....

     
  • jackjames

    jackjames - 2019-08-29

    Thanks of your quick response.
    The circuit is OK. The address of the devices is exact and was found via I2cSniffer.
    The code is an adapted part of the test code in the demos folder.

     
  • jackjames

    jackjames - 2019-08-29

    Files:

     
  • jackjames

    jackjames - 2019-08-29

    File:

     
  • jackjames

    jackjames - 2019-08-29

    File:

     
  • Anobium

    Anobium - 2019-08-29

    Please remove FamilyVariant=1 from the .dat file.

    That may have a side effect but does this resolve your current issue?

     
  • jackjames

    jackjames - 2019-08-29

    Nothing to do, the problem remained.

     
  • Anobium

    Anobium - 2019-08-29

    OK. Revert the dat file. This is not the issue.

    I just tried. C:\GCB@Syn\GreatCowBasic\Demos\vendor_boards\mplab_xpress_board_pic16f18855\16_show_i2c_devices_to_serial_terminal.gcb demo. Changed the part to a 16F18877. Works fund the I2C EPPROM device as follows:

    The 16F18877 is the same memory part as the 16F18857 - so, this should be valid.

    Hardware I2C 
    
       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
    00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    80 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    90 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    A0 A0 A1 A2 A3 -- -- -- -- -- -- -- -- -- -- -- --
    B0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    C0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    D0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    E0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    F0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    
    End of Search
    

    and again

    Hardware I2C 
    
       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
    00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    80 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    90 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    A0 A0 A1 A2 A3 -- -- -- -- -- -- -- -- -- -- -- --
    B0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    C0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    D0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    E0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    F0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    
    End of Search
    

    Same test works with with you?

     

    Last edit: Anobium 2019-08-29
  • jackjames

    jackjames - 2019-08-29

    Ok. Right now I can't verify it because I'm on my way to work. As soon as I get there, I try it.

     
  • Anobium

    Anobium - 2019-08-29

    My guess... at the moment. The I2C Discovery will work.

    There is an issue in the EEPROM write. Not sure where - yet.

    Nothing wrong with EEPROM write.... see below

     

    Last edit: Anobium 2019-08-29
  • Anobium

    Anobium - 2019-08-29

    Working code..... a few errors in your source code.

    1. You LOCKED the PPS.... I2C was not operational. Using PPSTool with the cut and paste works for me.
    2. The addressing using BYTEs to the EEPROM library needs to be WORD
    3. EEpAddr not set to correct address prior to Read ops.
    4. Not cleaning the array... before reading.... this is not important but makes sense when you see it.
    '       $formattagcb
    '       -
    '       Configuration
            #Chip 16F18877, 32
            #Config MCLRE = On
            #Config PWRTE = Off
    '       -
            #startup InitPPS, 85
            #Define PPSTOOLPART 16f18857
    '       -
    '       -
            #Option Explicit
    '       #Config Osc = intOSC, MCLRE_OFF, PLLEN_ON
            #Include <i2ceeprom.h>
    '       -
    '       LCD connection settings
            #Define LCD_IO 4
            #Define LCD_SPEED SloW       -
    ; ----- Define Hardware settings
      #define LCD_RS PORTE.0
      #define LCD_RW PORTE.1
      #define LCD_Enable PORTE.2
      #define LCD_DB4 PORTd.4
      #define LCD_DB5 PORTd.5
      #define LCD_DB6 PORTd.6
      #define LCD_DB7 PORTd.7
    '       -
    '       -
    '       ----- Define Hardware settings
    '       Define I2C settings - CHANGE PORTS if required
            #Define HI2C_BAUD_RATE 100
            #Define HI2C_DATA PORTC.4
            #Define HI2C_CLOCK PORTC.3
            HI2CMode Master
    '       -
            #Define EEPDEV 0xA0               'Change this ADDRESS to suit
            #Define EEPROMPAGESIZE 16         'Change this typical 16, 32 Or 64.
    '       -
            Dir HI2C_DATA  In
            Dir HI2C_CLOCK In
    '       -
    '       -
            Dim XLoop      As Byte
    Dim EEpAddr    As Word  'this needs to be a WORD. Easy thinl to miss.
            Dim DataString As String * 16
    '       -
            Dim DatArray (EEPROMPAGESIZE) As Byte
    '       -
    '       -
            DatArray = 0x55,9,8,7,6,5,4,3,2,1
            Cls
            Locate 0, 0
            Print "Initial Array"
    '       -
            EEpAddr = 0
    
            eeprom_wr_array( EEPDEV, EEPROMPAGESIZE, EEpAddr , DatArray(), 10 )
    '       -
    
            Locate 1,0
            Print "Read"
    EEpAddr = 0  'this is indexed by the WR_ARRAY
    DatArray = 0,0,0,0,0,0,0,0,0  'You need to empty this! How will you know....
    
            eeprom_rd_array (EEPDEV, EEpAddr , DatArray(), 10)
    
            Locate 2,0
            For XLoop = 1 to 10
               Locate 3,0
               Print XLoop
               Locate 3,6
               Print DatArray (XLoop)
               Print " "
               Wait 500 ms
            Next
    '       -
            Locate 3,10
            Print "End"
    '       -
    '       -
    '       -
    Sub InitPPS
    'This celar not PPS!!! You are locking the PPS!!!!
    '        LockPPS
    
    'My I2C
    SSP1DATPPS = 0x0013    'RC3 > SDA1
    RC3PPS = 0x0015    'SDA1 > RC3 (bi-directional)
    RC4PPS = 0x0014    'SCL1 > RC4
    SSP1CLKPPS = 0x0014    'RC4 > SCL1 (bi-directional)
    'Your i2c
    '
    '            RC3PPS = 0x0014    'SCL1 > RC3
    '            SSP1CLKPPS = 0x0013    'RC3 > SCL1 (bi-directional)
    '            RC4PPS = 0x0015    'SDA1 > RC4
    '            SSP1DATPPS = 0x0014    'RC4 > SDA1 (bi-directional)
    
    '        UnLockPPS
    End Sub
    
            End
    
     
  • Anobium

    Anobium - 2019-08-29

    I just verified the Write and Read using PICKitPlus to read the EEPROM when attached directly to a PICkit programmer.

    The first 10 bytes in the Program Memory window is the data saved to the EEProm by the Great Cow BASIC program.

    :-)

    P.S. I use 0x55 as write data, as this is 01010101 binary. As good test I used to ensure we are sync'ed up during transmissions.

     

    Last edit: Anobium 2019-08-29
  • Anobium

    Anobium - 2019-08-29

    And, as this took me too long figure out. I have updated the EEProm library to show which method is being used. I was too easy not spot the differences between the overloaded methods.

    ASM will now generate code as shown below - note the detail with respect to the use of WORDs and BYTEs (code not shown in the code segment below).

    EEPROM_RD_ARRAY8
    ;`eeprom_rd_array - uses a word for eepaddr. intended for larger memory eeproms that require address_h & address (two bytes, therefore passed as a word ) to set addreess`
    SysDoLoop_S3
      call  HI2CRESTART
      movlw 160
      movwf I2CBYTE
      call  HI2CSEND
      movf  hi2cackpollstate,F
      btfss STATUS,Z
      goto  SysDoLoop_S3
    ...
    ...
    ...
    ...
    
    EEPROM_WR_ARRAY6
    ;`eeprom_wr_array- uses a word for eepaddr. intended for larger memory eeproms that require address_h & address (two bytes, therefore passed as a word ) to set addreess`
    SysDoLoop_S1
      call  HI2CRESTART
      movlw 160
      movwf I2CBYTE
    ...
    ...
    ...
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.