PDA

View Full Version : Going back to basics, some advice on memory storage



Scampy
- 23rd March 2016, 20:04
Guys, I need some advice on basic storage locations as I'm running round in circles...

Pic: 18F4620

Task: To load a set of pre-set values for a project that has 16 channels when first programmed. Each channel has an on time, an off time and a maximum value for the level. All of these are word variables can then be set by a user via menu options, so for argument sake all the on times can be pre-set to 14:00 hrs and then the user can set channel one to 15:00 hrs, then channel two to 16:00 hrs etc. Then in the event of a power fail these user values are saved and then read back in.

At the moment I can have used the normal READ and WRITE statements to keep track of the user set variables, but its been suggested that I need to use the DATA statement to write the initial data to the EEPROM. The only thing is I'm not that clear on how to do this correctly.

My current read write statements start at location 8



write 8,CH1_Max.lowbyte
write 9,CH1_MAX.highbyte
write 10,CH2_Max.lowbyte
write 11,CH2_Max.highbyte
write 12,CH3_Max.lowbyte
.....
write 102,CH16_off_Time.lowbyte
write 103,Ch16_off_Time.highbyte


No I know that the DATA statement uses a similar arrangement for location, so would I simply substitute the WRITE command for DATA ?



DATA 8,CH1_Max.lowbyte
DATA 9,CH1_MAX.highbyte
DATA 10,CH2_Max.lowbyte


And does the READ statement work with DATA statement, or do I need to use EEPROM statement is some way ?

Sorry if this seems a noob question, but in my previous projects I've never bothered with worrying about saving the settings as it was often quick enough to re-enter them manually as there were not so many values to set..but 96 entries is just a tad to much...

richard
- 23rd March 2016, 22:09
horses an water

I try again this seems to have gone through to the keeper
http://www.picbasic.co.uk/forum/showthread.php?t=20966


arrays and the ext modifier don't exist for no reason


data_block var byte[96]
i var byte
ch_max var word ext ;accessable as ch_max[0] to ch_max[15]
ch_on var word ext ;accessable as ch_on[0] to ch_on[15]
ch_off var word ext ;accessable as ch_off[0] to ch_off[15]

asm
ch_max = data_block
ch_on = data_block+32
ch_off = data_block+64
endasm
save_data:
for i = 0 to 95
write 8+i ,data_block[i]
next
return
read_data:
for i = 0 to 95
read 8+i ,data_block[i]
next
return

set_defaults:
for i=0 to 15
ch_max[i]= 3000
ch_on[i}= 1800
ch_off[i]=600
next
return

Scampy
- 23rd March 2016, 22:54
Richard, your previous posts hadn't been overlooked... I was trying to keep my explanation as simple as possible with regards to the locations in order to help explain what I was looking for, which was info on the correct way to use the DATA and EEPROM statements... which you appear to of overlooked in your reply whilst you were hell bent on being sarcastic with your



horses an water

I try again this seems to have gone through to the keeper"

richard
- 23rd March 2016, 23:16
sorry about the sarcasm ,but doing it your way that's a lot of typing

still

DATA 8,CH1_Max.lowbyte
DATA 9,CH1_MAX.highbyte
DATA 10,CH2_Max.lowbyte

won't work data can only store constants

data @8, 123,34,23,15 ........................ for the other 92 values

and it can be readback with read

HenrikOlsson
- 25th March 2016, 10:14
DATA is used to store the value(s) in the EEPROM when the device is programmed. Ie. to get a "default" value in there. It's embedded in the .hex file image which get programmed into the device.
WRITE is used to store values in EEPROM during runtime.
It doesn't matter HOW the values got there, READ will access them.

You don't really have to keep track of the adresses etc, you can do
Ch1_On_Time DATA WORD 1234
Ch1_Of_Time DATA WORD 2345

This way you have created a label for each entry in the EEPROM which you can then use when your READ or WRITE from/to it in your code. Please note that Ch1_On_Time in this case is NOT your variable - it's ONLY a label. To actually USE it in your program you need to READ it from EEPROM and Place it an ordinary variable - but you're already doing that.

Using arrays, the EXT modifer, and indexing thru the data as in Richards example is probably more efficient code wise but may not be the easiest way to debug and/or understand. YMMV.

/Henrik.

Scampy
- 25th March 2016, 11:11
Henrik,

Thank you for your very informative post. I agree that Richards guidance in using the array and modifier makes for tighter compact code. I just thought that in this case it might of muddied the water a little when I was trying to explain my question.

Scampy
- 25th March 2016, 12:07
Henrik,

If I follow your post correctly I can do the following



DATA @8,0

EE_CH1_Max DATA WORD 4095
EE_CH2_Max DATA WORD 4095

etc


and then




read EE_CH1_Max, WORD CH1_Max
read EE_CH2_Max, WORD CH2_Max



The part I can't get my head round is assuming you place the data statement at the start of the code so this happens when the code is run, what distinguishes it from a fresh install and a reboot as surely the code is executed from the start when powered up and will use the data command when it reaches that line of code ?

Scampy
- 25th March 2016, 14:38
Oh it's so much easier with labels than using all that location, lowbyte hughtbyte stuff !!! :)

I now have a set of preset values loaded first time after the chip is programmed, and user variables saved once changed by the user, which are retained in the event of a power fail...

Now to do a few more bits before following Richards examples to tidy up the code and make ot a bit more compact...