Using EEPROM to store variables...


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default Test Code Results

    Ralph:

    Thanks for the demo code, here is what the result was - pasted from the Hyperterminal window. (I added "writing:" to the string so I could see that it was becuse it was not writing anything that showed up on the screen.)
    ---------------------------------------------------------------------

    ** MCU started

    ** EEPROM written

    writing:
    writing:
    writing:
    writing:
    writing:
    writing:
    writing:
    writing:
    writing:
    writing:

    --------------------------------------------------------------------------

    Here is the code I used:

    '--------------------------------------------------------------------------------

    'Tom,

    'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
    'The following code is tested.
    'could you give it a try and let us know what the result was?


    ' -----[ Fuses ]------------------------------------------------
    @ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
    '
    ' -----[ Includes/Defines ]---------------------------------------------------------
    include "modedefs.bas" 'include serout defines

    OSCCON=%01111000
    DEFINE OSC 8
    While OSCCON.2=0:Wend


    SCL var PortC.5 ' I2C Clock
    SDA var PortC.4 ' I2C Data
    ROM0 CON $A0 ' EEPROM 0 Address
    ADDR VAR word ' ROM Location
    MonPort var PortC.6 ' Monitor Port
    Mode con 16572 ' 4800 Baud 8N1 for Monitor Port

    Test var byte[21] ' Test Array
    I2Cdelay con 10 ' Delay after I2Cwrite

    Temp var byte

    CLEAR

    SEROUT2 MonPort,Mode,[13,10,"** MCU started",13,10,13,10]

    ADDR=100

    I2CREAD SDA,SCL,ROM0,ADDR,[Temp]

    IF Temp="*" THEN GOTO SkipWrite

    I2CWRITE SDA,SCL,ROM0,ADDR,_
    [$2A,$20,$54,$65,$73,$74,$20,$45,$45,$50,$52,$4F,_
    $4D,$20,$2D,$2D,$3E,$20,$4F,$4B,$21]

    PAUSE I2Cdelay

    SEROUT2 MonPort,Mode,["** EEPROM written",13,10,13,10]

    GOTO Loop

    SkipWrite:

    SEROUT2 MonPort,Mode,["** EEPROM write skipped",13,10,13,10]

    I2CREAD SDA,SCL,ROM0,addr,[str test\21]

    Loop:
    SEROUT2 MonPort,Mode,["writing: ", STR Test\21,13,10]
    PAUSE 1000
    GOTO Loop

    END

    ----------------------------------------------------------------


    Is this what you expected?

    Tom

  2. #2
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Exclamation EEPROM program again with error trapping

    I got to wondering why I was getting nothing in the last build. So I trapped for failures to read and write. I did see that it was just not getting there, so tweaked the port assignments, etc. Here is what I get now:

    ** MCU started

    ** EEPROM write skipped

    Reading:* Test EEPROM --> OK!
    Reading:* Test EEPROM --> OK!
    Reading:* Test EEPROM --> OK!
    Reading:* Test EEPROM --> OK!
    Reading:* Test EEPROM --> OK!

    (I added "reading" to it)

    The program is as follows:

    ------------------------------------------------------------------


    '--------------------------------------------------------------------------------

    'Tom,

    'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
    'The following code is tested.
    'could you give it a try and let us know what the result was?


    ' -----[ Fuses ]------------------------------------------------
    @ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
    '
    ' -----[ Includes/Defines ]---------------------------------------------------------
    include "modedefs.bas" 'include serout defines

    OSCCON=%01111000
    DEFINE OSC 8
    While OSCCON.2=0:Wend

    SCL var PortC.3 ' I2C Clock
    SDA var PortC.4 ' I2C Data
    ROM0 con %10100000 ' address of chip1
    ADDR VAR word ' ROM Location
    MonPort var PortC.6 ' Monitor Port
    Mode con 16572 ' 4800 Baud 8N1 for Monitor Port
    Test var byte[21] ' Test Array
    I2Cdelay con 10 ' Delay after I2Cwrite

    Temp var byte

    CLEAR

    SEROUT2 MonPort,Mode,[13,10,"** MCU started",13,10,13,10]

    ADDR=100

    I2CREAD SDA,SCL,ROM0,ADDR,[Temp]

    IF Temp="*" THEN GOTO SkipWrite

    I2CWRITE SDA,SCL,ROM0,ADDR,_
    [$2A,$20,$54,$65,$73,$74,$20,$45,$45,$50,$52,$4F,_
    $4D,$20,$2D,$2D,$3E,$20,$4F,$4B,$21],failw

    PAUSE I2Cdelay

    SEROUT2 MonPort,Mode,["** EEPROM written",13,10,13,10]

    GOTO Loop

    SkipWrite:

    SEROUT2 MonPort,Mode,["** EEPROM write skipped",13,10,13,10]

    Loop:
    I2CREAD SDA,SCL,ROM0,addr,[str test\21],failr
    SEROUT2 MonPort,Mode,["Reading:", STR Test\21,13,10]
    PAUSE 1000
    GOTO Loop

    failr:
    serout2 monport, mode,["failure to read",10,13]
    return

    failw:
    serout2 monport, mode,["failure to write",10,13]
    return


    END

  3. #3
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    So your Hardware is OK.

    There must be something wrong with your code.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  4. #4
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Question

    Hmm.. I am keeping trying to unravel this -- I have a version of your program, but it won't compile.. Can you tell my why I have a 'bad expression'??? It says the I2Cwrite, I2Cread commands are wrong.. I cannot for the life of me make them compile - defining X as a word, byte, whatever..

    TG

    'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
    'The following code is tested.
    'could you give it a try and let us know what the result was?
    ' -----[ Fuses ]------------------------------------------------
    @ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
    '
    ' -----[ Includes/Defines ]---------------------------------------------------------
    include "modedefs.bas" 'include serout defines

    OSCCON=%01111000
    DEFINE OSC 8
    While OSCCON.2=0:Wend

    SCL var PortC.3 ' I2C Clock
    SDA var PortC.4 ' I2C Data
    ROM0 con %10100000 ' address of chip1
    ADDR VAR word ' ROM Location
    portout var PortC.6
    portin var PortC.7
    Mode con 16572 ' 4800 Baud 8N1 for Monitor Port
    Test var byte[21] ' Test Array
    I2Cdelay con 10 ' Delay after I2Cwrite

    Temp var byte
    X var byte
    dta var byte

    CLEAR

    loop:
    SEROUT2 portout,Mode,[13,10,"** MCU started",13,10,13,10]
    serout2 portout, mode, ["enter 1 to write data, 2 to read it"]
    serin2 portin, mode, [DEC temp]

    select case temp
    case 1
    for X = 0 to 256
    I2Cwrite SDA, SCL,ROMO,X,[X],failw
    pause 10
    next x
    case 2
    for x = 0 to 256
    I2Cread SDA,SCL,ROMO,X,[X],failr
    Serout2 portout, mode, ["Address: ",#addr," Data:",#dta]
    pause 100
    next x
    end select

    goto loop:

    failr:
    serout2 portout, mode,["failure to read",10,13]
    return

    failw:
    serout2 portout, mode,["failure to write",10,13]
    return


    END

  5. #5
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Question

    ... ahem nevermind.. I was using ROMO instead of ROM0... shortened it to ROM so I could not make that mistake, and it works.

    NOW I have a program that tests memory.. and the use of WORD or BYTE.. This will read or write to memory. VERY odd behavour tho, leading me to think I am missing some key understanding:

    I write 0-640 to memory 0-640.. When I read it back I get REALLY odd stuff... NOT 0-640:

    'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
    'The following code is tested.
    'could you give it a try and let us know what the result was?
    ' -----[ Fuses ]------------------------------------------------
    @ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
    '
    ' -----[ Includes/Defines ]---------------------------------------------------------
    include "modedefs.bas" 'include serout defines

    OSCCON=%01111000
    DEFINE OSC 8
    While OSCCON.2=0:Wend

    SCL var PortC.3 ' I2C Clock
    SDA var PortC.4 ' I2C Data
    ROM con %10100000 ' address of chip1
    ADDR VAR word ' ROM Location
    portout var PortC.6
    portin var PortC.7
    Mode con 16572 ' 4800 Baud 8N1 for Monitor Port
    Test var byte[21] ' Test Array
    I2Cdelay con 10 ' Delay after I2Cwrite

    Temp var byte
    X var word
    dta var word

    CLEAR

    Addr=100
    loop:
    SEROUT2 portout,Mode,[13,10,"** MCU started",13,10,13,10]
    serout2 portout, mode, ["enter 1 to write data, 2 to read it"]
    serin2 portin, mode, [DEC1 temp]

    select case temp
    case 1
    for X = 0 to 640
    I2Cwrite SDA, SCL,ROM,X,[X],failw
    Serout2 portout, mode, ["Write Address: ",#x," Data:",#X,10,13]
    pause 10
    next x
    case 2
    for x = 0 to 640
    I2Cread SDA,SCL,ROM,X,[dta.byte0],failr
    I2Cread SDA,SCL,ROM,X+1,[dta.byte1],failr
    Serout2 portout, mode, ["Address: ",#x," Data:",#dta,10,13]
    pause 10
    next x
    case else
    goto loop
    end select

    goto loop:

    failr:
    serout2 portout, mode,["failure to read",10,13]
    return

    failw:
    serout2 portout, mode,["failure to write",10,13]
    return


    END

  6. #6
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    OK, I am drained.. here is the last program. I have been experimenting with BYTE and WORD and notice they make the memory behaviour REALLY different.. In any event, I have noticed:

    1. I can WRITE from 0-640 to memory
    2. When I READ this back, it is all '0's..
    3. When I ERASE it ($FFFF), and read it back it is all 65355..

    I THINK somehow I am writing PAGES using the [x] brackets..(???) I want to write to SPECFIC memory locations.. but can't figure out the syntax for that. maybe that is why the data is all messed up? So I tried putting it in increments of 128.. still reading back '0'..

    Anyway - here is the sample program that lets you write, read and erase from an external 24LC512... and get back zeros..

    TG

    ------------------------

    'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
    'The following code is tested.
    'could you give it a try and let us know what the result was?
    ' -----[ Fuses ]------------------------------------------------
    @ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
    '
    ' -----[ Includes/Defines ]---------------------------------------------------------
    include "modedefs.bas" 'include serout defines

    OSCCON=%01111000
    DEFINE OSC 8
    While OSCCON.2=0:Wend

    SCL var PortC.3 ' I2C Clock
    SDA var PortC.4 ' I2C Data
    ROM con %10100000 ' address of chip1
    ADDR VAR word ' ROM Location
    portout var PortC.6
    portin var PortC.7
    Mode con 16468 ' 4800 Baud 8N1 for Monitor Port
    Test var byte[21] ' Test Array
    I2Cdelay con 10 ' Delay after I2Cwrite

    Temp var byte
    X var word
    dta var word

    CLEAR

    Addr=100
    loop:
    SEROUT2 portout,Mode,[13,10,"** MCU started",13,10,13,10]
    serout2 portout, mode, ["enter 1 to write data, 2 to read it, 3 to erase"]
    serin2 portin, mode, [DEC1 temp]

    select case temp
    case 1
    for X = 0 to 640 step 128
    I2Cwrite SDA, SCL,ROM,X,[X],failw
    Serout2 portout, mode, ["Read Address: ",#X," Data:",#X,10,13]
    pause 10
    next x
    case 2
    for x = 0 to 640 step 128
    I2Cread SDA,SCL,ROM,X,dta,failr
    Serout2 portout, mode, ["Read Address: ",#X," Data:",#dta,10,13]
    next x
    Case 3
    for x = 0 to 640 step 128
    I2Cwrite SDA,SCL,ROM,X,[$FFFF],failw
    Serout2 portout, mode,["Erasing Address: ",#X," Data:",$FFFF,10,13]
    pause 10
    next x
    case else
    goto loop
    end select

    goto loop:

    failr:
    serout2 portout, mode,["failure to read",10,13]
    return

    failw:
    serout2 portout, mode,["failure to write",10,13]
    return

    END

  7. #7
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Tom,

    your code works as designed.

    In the FOR...NEXT loop to write the eeprom you are counting from 0 to 640.
    X is a WORD size variable and being used as EEPROM address as well as value to be written.
    This means you are writing a WORD (two BYTES) to the EEPROM.
    This requires two BYTE locations in your EEPROM.
    By incrementing the address counter by 1 the LowByte of the last WORD written will always be overwritten with the HighByte oft the current WORD.

    here is a working example:

    Code:
    for X = 0 to 640 
    I2Cwrite SDA, SCL,ROM,X*2,[x]
    pause 10
    Serout2 portout, mode, ["Write Address: ",#x*2," Data:",#x,10,13]
    dta=dta+1
    next x
    
    for x = 0 to 640 
    I2Cread SDA,SCL,ROM,X*2,[dta]
    Serout2 portout, mode, ["Address: ",#x*2," Data:",#dta,10,13]
    pause 100
    next x
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



Similar Threads

  1. Can't read sequential addresses in external EEPROM
    By tjkelly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th February 2010, 14:46
  2. Problem with I2C EEPROM addressing
    By Atom058 in forum General
    Replies: 14
    Last Post: - 3rd November 2009, 03:17
  3. How to write/read strings EEPROM/LCD
    By g-hoot in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 11th February 2007, 06:26
  4. eeprom how many data can store
    By DragonBall_6 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th December 2006, 11:39
  5. word variable to 25lc640
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th July 2004, 19:59

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