PDA

View Full Version : About writing word variables into the eeprom



Ted's
- 29th June 2008, 12:29
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:



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?

Jerson
- 29th June 2008, 12:41
Since you're dealing with word variables, the size is known to be 2bytes on the PIC. So, something like this will work



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

Ted's
- 29th June 2008, 14:07
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.

Jerson
- 29th June 2008, 16:02
Ted, you're right on this. I goofed. But you got the idea though ;)

Ted's
- 30th June 2008, 17:32
Yes. But it does not seem to be efficient enough.

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

mackrackit
- 30th June 2008, 17:40
When you say efficient, do you mean speed or code size?

Darrel Taylor
- 30th June 2008, 18:01
If you alias the Word array as a Byte array, you can just write 1 byte at a time in the loop.

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

Ted's
- 11th August 2008, 18:45
When you say efficient, do you mean speed or code size?
The latter.

peterdeco1
- 11th August 2008, 19:16
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.

Ted's
- 30th August 2008, 21:06
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.

Ted's
- 30th August 2008, 21:07
If you alias the Word array as a Byte array, you can just write 1 byte at a time in the loop.

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.

Darrel Taylor
- 30th August 2008, 21:20
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>