About writing word variables into the eeprom


Closed Thread
Results 1 to 12 of 12
  1. #1
    Ted's's Avatar
    Ted's Guest

    Default About writing word variables into the eeprom

    I had the challenge to write a word-array into the eeprom of a 16f628a. And if I am well informed only values from 0 to 255 fit into one "cell" of the 128 cells this pic offers.

    I did it this way:

    Code:
    counter = 0
    arraysize = 20
    FOR ArrayPosition = 0 TO arraysize-2 STEP 2
        Word_Dummy = Array[counter]
        WRITE ArrayPosition,Word_Dummy.Byte1
        counter = counter + 1    
    Next ArrayPosition
    
    counter = 0
    FOR ArrayPosition = 1 TO arraysize-1 STEP 2
        Word_Dummy = Array[counter]
        WRITE ArrayPosition,Word_Dummy.Byte0
        counter = counter + 1    
    Next ArrayPosition
    Is there a more efficient way?
    Last edited by Ted's; - 29th June 2008 at 12:36.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Since you're dealing with word variables, the size is known to be 2bytes on the PIC. So, something like this will work

    Code:
    SaveArray:
    FOR ArrayPosition = 0 TO 20-1
        WRITE ArrayPosition*2,Array[ArrayPosition].Byte0,Array[ArrayPosition].Byte1
    Next
    return
    
    ReadArray:
    FOR ArrayPosition = 0 TO 20-1
        READ ArrayPosition*2,Array[ArrayPosition].Byte0,Array[ArrayPosition].Byte1
    Next
    return

  3. #3
    Ted's's Avatar
    Ted's Guest


    Did you find this post helpful? Yes | No

    Default

    I disagree here.

    WRITE ArrayPosition*2,Array[ArrayPosition].Byte0,Array[ArrayPosition].Byte1

    is for ArrayPosition = 0

    WRITE 0,Array[0].Byte0,Array[0].Byte1

    ...

    is for ArrayPosition = 19

    WRITE 38,Array[19].Byte0,Array[19].Byte1


    According to the manual, to write a word, each of the 2 bytes that make up the word must be written separately:
    w Var Word
    WRITE 0,w.BYTE0
    WRITE 1,w.BYTE1

    in other words, WRITE does not accept 3 arguments.

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Ted, you're right on this. I goofed. But you got the idea though

  5. #5
    Ted's's Avatar
    Ted's Guest


    Did you find this post helpful? Yes | No

    Default

    Yes. But it does not seem to be efficient enough.

    A one or two-liner should be enough. Maybe using the remainder // ?

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    When you say efficient, do you mean speed or code size?
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    If you alias the Word array as a Byte array, you can just write 1 byte at a time in the loop.
    Code:
    arraysize  CON 20
    Array      VAR WORD[arraysize]
    
    @ByteArray = _Array
    ByteArray  VAR BYTE EXT
    
    FOR ArrayPosition = 0 TO (arraysize*2-1)
        WRITE ArrayPosition, ByteArray(ArrayPosition)
    NEXT ArrayPosition
    DT

  8. #8
    Ted's's Avatar
    Ted's Guest


    Did you find this post helpful? Yes | No

    Default

    When you say efficient, do you mean speed or code size?
    The latter.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    I've only done this once a while back but all I did in my program at WRITE time is:

    WRITE 0,X.BYTE0 : WRITE 1,X.BYTE1

    At READ time:

    READ 0,X.BYTE0 : READ 1,X.BYTE1

    I don't remember having any problems. Hope it works.

  10. #10
    Ted's's Avatar
    Ted's Guest


    Did you find this post helpful? Yes | No

    Default

    It does.

    You know, the issue here is filling the eeprom with data from a word array. Your line is only a part of it.

  11. #11
    Ted's's Avatar
    Ted's Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    If you alias the Word array as a Byte array, you can just write 1 byte at a time in the loop.
    Code:
    arraysize  CON 20
    Array      VAR WORD[arraysize]
    
    @ByteArray = _Array
    ByteArray  VAR BYTE EXT
    
    FOR ArrayPosition = 0 TO (arraysize*2-1)
        WRITE ArrayPosition, ByteArray(ArrayPosition)
    NEXT ArrayPosition
    I do not understand your code. Please comment.

  12. #12
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    This should help some.
    Once you know what the EXT modifier does, it should make more sense.

    The EXT (external) modifier.
    http://www.picbasic.co.uk/forum/showthread.php?t=3891
    <br>
    DT

Similar Threads

  1. Read/Write Problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th February 2010, 01:51
  2. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  3. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th July 2008, 23:19
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  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 : 2

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