News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Filling EEPROM string with null values

Started by shantanu@india, Jul 16, 2022, 11:48 AM

Previous topic - Next topic

shantanu@india

Hi,
If I want to initialize a 32-byte EStr as follows
MyData Edata "                                ",0it gets filled up with spaces which I do not want.
I need a null-initialized addressable EEPROM string.
How do I do it?
If I put
MyData Edata Byte 0and then iterate as MyData+1 , MyData+2 etc. to fill up the 32 subsequent places by 0(null) will it do the job?
Seems to be a bad way of doing it.
Regards
Shantanu

tumbleweed

If you want all 33 bytes to be initialized to 0 (32 byte string + null), can't you just do this?
MyData EData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       EData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       EData 0

That'll put 33 0x00 bytes into the hex file for EEPROM initialization when it gets programmed
(it actually does 34 since 33 is an odd number)

shantanu@india

Not a bad idea ;)
I was just hopeful for a magic command!!
Regards
Shantanu

Yasin

#3
No different than what @tumbleweed suggested. But it could be like this.

MyData Edata Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Byte 0

shantanu@india

Regards
Shantanu

shantanu@india

My problems persist.
How do I read back the null terminated EEPROM string into an array?

MyData Edata Dword 0 , Dword 0 , Dword 0 , Dword 0 , Dword 0,Dword 0 , Dword 0,Dword 0,Byte 0
Dim MyArray[32] as Byte

StrN MyArray = Eread MyData
This gives rise to unrecognised , illegal or unresolvable characters found.
In my program I have to use Ewrite to write fresh data to MyData.How do I achieve that?
Any pointers would be greatly helpful.
Regards
Shantanu

trastikata

#6
Hello shantanu,

MyArray is declared as a byte array and not as string, so it is being threaded as such by the compiler.

MyData is actually 33 bytes (8 Dwords + Byte) and I suspect while your array is 32 bytes, the 1st member of the byte array gets overwritten by the last byte in MyData i.e. 0

Les' compiler has a lot of check-ups but I noticed mixing strings and byte arrays sometimes causes unexpected behavior. That's why when I have to transfer data between them, I always use loops and address individual array members. Here are some examples:

Dim MyByteArray[32] As Byte
Dim MyStringArray As String * 32
Dim i As Byte

Main:
    For i = 0 UpTo 31
        MyByteArray[i] = ERead i
    Next
   
    For i = 0 UpTo 31
        MyStringArray[i] = ERead i
    Next
   
    For i = 0 UpTo 31
        MyStringArray[i] = MyByteArray[i]
    Next
   
    MyData EData Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Byte 0

 

shantanu@india

#7
Quote from: trastikata on Jul 17, 2022, 07:59 AMHello shantanu,

MyArray is declared as a byte array and not as string, so it is being threaded as such by the compiler.

MyData is actually 33 bytes (8 Dwords + Byte) and I suspect while your array is 32 bytes, the 1st member of the byte array gets overwritten by the last byte in MyData i.e. 0

Les' compiler has a lot of check-ups but I noticed mixing strings and byte arrays sometimes causes unexpected behavior. That's why when I have to transfer data between them, I always use loops and address individual array members. Here are some examples:

Dim MyByteArray[32] As Byte
Dim MyStringArray As String * 32
Dim i As Byte

Main:
    For i = 0 UpTo 31
        MyByteArray[i] = ERead i
    Next
   
    For i = 0 UpTo 31
        MyStringArray[i] = ERead i
    Next
   
    For i = 0 UpTo 31
        MyStringArray[i] = MyByteArray[i]
    Next
   
    MyData EData Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Dword 0, Byte 0

 
You are absolutely right Trastikata.
I'm reading and writing byte by byte which has solved all my problems.

MyData Edata 0,0,0,0,0,0,0,0
Dim MyOldArray[8] as Byte
Dim MyNewArray[8] as Byte
Dim position as Byte
'EEPROM Read routine
For position=0 to 7
   MyOldArray[position]=Eread [MyData+position]
Next
'EEPROM write routine
INTCON. 7=0
For position=0 to 7
 Ewrite (MyData+position), [MyNewArray[position]]
INTCON.7=1
Next

Regards
Shantanu

tumbleweed

#8
I would have thought you could do this...

' eeprom init (32 chars + null)
MyData EData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       EData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       EData 0

Dim MyString As string*32

' read eeprom data into MyString
' terminates upon reading null char from MyData
' (does not put it into MyString, so MyString may not be properly terminated)
MyString = EStr MyData

'modify string (adds null term @ [4])
MyString = "1234"
' modify individual chars in the string (null term not changed)
MyString[1] = "A"
MyString[3] = "B"

' write string to eeprom
'(stops at null char in MyString... does not write it to eeprom so MyData length does not change)
EWrite MyData, [MyString]


but it would appear that some of the operations don't update the null termination char. In this particular case some of this would work because the EEPROM array is set to all 0 to start, but you would have mixed results when using strings and other data.

I don't know if that's intentional or an oversight.

shantanu@india

I remember Les giving an explanation about null terminated strings which was conceptualized from the Basic Stamps that were in vogue.
I tried MyArray = Estr MyData.
Didn't work.
Estr is to be used in conjunction with some serial I/O, display etc. as per the manual. Cannot be used to load an array from EEPROM.
Regards
Shantanu

tumbleweed

Quote from: shantanu@india on Jul 17, 2022, 12:12 PMEstr is to be used in conjunction with some serial I/O, display etc. as per the manual. Cannot be used to load an array from EEPROM.

Right, but it can be used to load a string type (manual pg 58)...
QuoteA null terminated string of characters held in Data (on-board EEPROM) can also be loaded or
concatenated to a String by using the Estr function: -

The null term issues I mentioned could just be the result of the syntax I used, but I'm not sure what the "proper" syntax would be.

top204

A Byte array and a String variable are very different beasts. Because they look the same in RAM, and because the compiler allows a String variable to be sliced with sqaure brackets as is performed with arrays, does not mean they operate the same.

Remember, Byte arrays in the compiler are not char arrays as in C or C++, they are a collection of variables that are the same size and can contain any value that the variable size will allow, whereas a String cannot, and its comparisons are very different and so are its assignments. A String variable is, essentially, similar to a char array, because it is null terminated, but easier to use because it does not require specialised functions for comparisons and assignments, such as strcpy, strcat, strcmp etc...

Also remember, an EData table takes no flash memory to operate, because it is a compiler directive at "compile time", so the data is placed in directly in the EEPROM address with an org directive. So you could have 1024 values (max EEPROM size) in several Edata tables and it will not use any flash memory, so will not bulk up the code at all.

In order to load an array with EEPROM data, the simple procedure demonstrated below can be used:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Read from on-board EEPROM and fill a byte array with its values
' Written for the Positron8 compiler by Les Johnson
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at
'
' Create a variable for the demo
'
    Dim bMyArray[20] As Byte Heap                   ' Holds the integer values from the ASCII hex characters 
'
' Create some EEPROM data
'
EEPROM_Data EData 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,_
                  10, 11, 12, 13, 14, 15, 16, 17, 18, 19
                 
'--------------------------------------------------------------------
' The main program starts here
'
Main:
    EEPROM_To_Array(bMyArray, 10, EEPROM_Data + 10) ' Read 10 bytes from EEPROM and load them into array "bMyArray"
   
 
'--------------------------------------------------------------------
' Load a byte array with EEPROM data
' Input     : pArAddr holds the address of the Byte array to load into
'           : pAmount holds the amount of bytes to read from EEPROM and load into the array
'           : pOffset holds the offset address of EEPROM to start reading from
' Output    : The array held in pArAddr will be loaded with the values read from EEPROM
' Notes     : None
'
Proc EEPROM_To_Array(ByRef pArAddr As Word, pAmount As Word, pOffset As Word)
    Dim wFSR0 As FSR0L.Word                 ' Create a 16-bit SFR from FSR0L and FSR0H
   
    wFSR0 = pArAddr                         ' Load FSR0L\H with the address of the array to load into
    Repeat                                  ' \ Create a loop for the amount of EEPROM reads required
        Dec pAmount                         ' /
        POSTINC0 = ERead pOffset            ' Load the array with the data read and increment it
        Inc pOffset                         ' Increment the EEPROM offset value
    Until pAmount = 0                       ' Loop until the amount fo bytes to read/write is reached
EndProc

If the amount of data to read is less that 256 bytes, the pAmount and pOffset parameter variables can be changed to Byte types.


shantanu@india

Thanks for the detailed clarification Les.
Regards
Shantanu