I2C with 16F628A mystery


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118

    Default I2C with 16F628A mystery

    I am having some difficulty understanding the results I am getting, maybe it is due to something simple but I've been trying to figure it out for the past few hours.

    I am using 16F628A reading and writing to 24LC16 and displaying results on LCD.
    I am also using another application called PonyProg to read/write to my same EEPROM.

    So here is the story;
    I write to the 2416 these values:
    Code:
    for i2caddr = 0 to 2048 step 16
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$FF]:pause 10
    next
    then I read back the values and display on the LCD in this way:
    Code:
    i2caddr = 0
    i2cread d1,d2,i2cdevice,i2caddr,[dat[0],dat[1],dat[2],dat[3],dat[4],dat[5],dat[6],dat[7],dat[8],dat[9],dat[10],dat[11],dat[12],dat[13],dat[14],dat[15]]
    pause 10
    errchk = 0
    for i = 0 to 15
        errchk = errchk + dat[i]
    next
        LCDOUT $FE,1
        lcdout $FE,$80,dec errchk: pause 3000
    Now I am expecting to get errchk = 741 but I get 716.
    I cycled through each value after the addition within the loop and the last number to add up is 230 although the value I wrote was $FF = 255

    Furthermore if I display on the LCD all values I see this:
    1113c3c3c3c3c
    3c888212121cc

    but $cc = 204

    Reading the content of 2416 with PonyProg shows properly:
    01 01 01 3c 3c 3c 3c 3c 3c 08 08 08 21 21 21 ff


    My code is not cleaned up because I was trying different ways but I always get to the same results I tried changing dat[] to word instead of byte but all the data gets messed up on the 2416.

    Code:
    @ __config _INTOSC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _LVP_OFF & _CP_OFF
    clear
    DEFINE OSC 4
    TRISB = 0                   ' Make PORTB all outputs
    CMCON = 7                   ' Disable comparator
    
    
    Dat         VAR byte[15]
    I           var byte
    D1          var PORTA.2
    D2          VAR PORTA.3
    I2CAddr     Var byte
    I2CDevice   var byte
    ERRchk      var word
    OK          VAR PORTA.4
    ;NOTOK       var PORTA.5
    ERRStatus   var bit
    
    LCD_DB4     VAR PORTB.7       ' Set port for 4 bits bus
    LCD_DB5     VAR PORTB.6
    LCD_DB6     VAR PORTB.5
    LCD_DB7     VAR PORTB.4
    LCD_RS      VAR PORTB.0       ' Set RS bit port
    LCD_E       VAR PORTB.1       ' Set Enable bit port
    LCD_Lines     CON 2         ' # of Lines on LCD,  1 or 2 (Note: use 2 for 4 lines)
    LCD_DATAUS    CON 50        ' Data delay time in us 
    LCD_COMMANDUS CON 2000      ' Command delay time in us 
    INCLUDE "LCD_AnyPin.pbp"    ' Must go after LCD initialization 
    Pause 500: LCDOUT $FE,1: pause 250   ' clear screen and relax
    
    Start:
    
    i2cdevice =160
    
    LCDOUT $FE,$80,"Start" 
    pause 500 
    
    for i2caddr = 0 to 2048 step 16
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr,[$FF]:pause 10
    next
    
    i2caddr = 0
    i2cread d1,d2,i2cdevice,i2caddr,[dat[0],dat[1],dat[2],dat[3],dat[4],dat[5],dat[6],dat[7],dat[8],dat[9],dat[10],dat[11],dat[12],dat[13],dat[14],dat[15]]
    pause 10
    errchk = 0
    for i = 0 to 15
        errchk = errchk + dat[i]
    next
        LCDOUT $FE,1
        lcdout $FE,$80,dec errchk: pause 3000
    
    
    if errchk = 741 then ERRStatus = 0
    i2caddr = 2032
    i2cread d1,d2,i2cdevice,i2caddr,[dat[0],dat[1],dat[2],dat[3],dat[4],dat[5],dat[6],dat[7],dat[8],dat[9],dat[10],dat[11],dat[12],dat[13],dat[14],dat[15]]
    pause 10
    Errchk = 0
    for i = 0 to 15
        errchk = errchk + dat[i]
       ; LCDOUT $FE,1
       ; lcdout $FE,$80,dec ERRCHK: pause 2000
    
    next
    if errchk = 741 and errstatus = 0 then 
        high ok
        pause 10000
        low ok
    else
    lcdout $FE,$C0,dec ERRCHK
    LCDOUT $FE,$80,hex dat[0],hex dat[1],hex dat[2],hex dat[3],hex dat[4],hex dat[5],hex dat[6],hex dat[7]    
    LCDOUT $FE,$C0,hex dat[8],hex dat[9],hex dat[10],hex dat[11],hex dat[12],hex dat[13],hex dat[14],hex dat[15]
    endif
    
    END
    Thanks for any help.

    Mike

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Dat VAR byte[15]

    must be 16
    </pre>
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Hi Steve,

    Thank you...!

    I tried changing it to 16 and my total is now as expected = 741
    and also showing as FF on LCD.

    But why should it be 16?
    if Dat[0] is working then 0 to 15 = 16 addresses isn't it?


    Mike

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    an array that range from 0 to 15 is an array of 16 elements... hence the 16. Facile hein?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Ok that makes sens.

    Actually I am feeling kind of stupid right now!
    After a glass of wine I will be fine.

    Un gros merci!

    Mike

  6. #6
    Join Date
    Jun 2005
    Location
    Surrey, England
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lilimike View Post
    Ok that makes sens.

    Actually I am feeling kind of stupid right now!
    After a glass of wine I will be fine.

    Un gros merci!

    Mike
    BTW you defined I2CAddr as a byte, but treat it as a word (2048) - should be a word.
    Peter

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts