PDA

View Full Version : writing word variables to data memory



malc-c
- 4th August 2010, 22:01
Guys (and gals)

Following on from This Thread (http://www.picbasic.co.uk/forum/showthread.php?t=13540), I've spent best part of this evening searching the forum and web for examples of how to save a word variable to the PICs memory so that it reads them back when the PIC should the power fail.

Can anyone put me out of my misery :(

rsocor01
- 4th August 2010, 23:25
Guys (and gals)

Following on from This Thread (http://www.picbasic.co.uk/forum/showthread.php?t=13540), I've spent best part of this evening searching the forum and web for examples of how to save a word variable to the PICs memory so that it reads them back when the PIC should the power fail.

Can anyone put me out of my misery :(

What exactly are you trying to write to memory? From your other thread, you said it is a combination of words and bytes. What variables are bytes and which ones are words?

Robert

malc-c
- 5th August 2010, 09:07
Sorry I might not of got the point over

lightsetHR[fn]
lightsetMN[fn]
lightoffHR[fn]
lightoffMN[fn]
droptemp[fn]
normtemp[fn]
StartHour[fn]
StartMin[fn]
StopHour[fn]
StopMin[fn]
alarmhigh[fn]
alarmlow[fn]

They are all WORD variables, but as all those in blue have values that are typically two digits (eg Hours will be between 00 and 23, with minutes being 00 to 59). they get saved and read OK for some reason. All those in red will be three digit values, eg 327, 150 and it's these that don't get saved. Each Word variable has 4 elements for example ,

alarmhigh[0]
alarmhigh[1]
alarmhigh[2]
alarmhigh[3]

These will have different values depending on what has been set in a previous section
eg:

alarmhigh[0]=340
alarmhigh[1]=360
alarmhigh[2]=400
alarmhigh[3]=370

Hope that helps clarify my issue

Acetronics2
- 5th August 2010, 11:03
Hi, Malc

Did you simply tried to Open the Holy Manual ??? :rolleyes:

supposing you use the PBP 2.60a Version ... it is at page 175 ...

supposing you use previous version ( 2.50b ) ... it is at page 162 ...

examples for WORDs given.

You also should have a look to $ 4.4 p 25 " Aliases " ... where it is shown how to " slice " your values ...

Alain

malc-c
- 5th August 2010, 11:41
Hi, Malc

Did you simply tried to Open the Holy Manual ??? :rolleyes:



Alain

http://www.picbasic.co.uk/forum/showthread.php?t=13540&p=92176#post92176 ;)

Just don't quite understand it... (sorry I'm thick ! - care to actually explain how I would do this " Aliases " ... where it is shown how to " slice " your values thing ?)

Acetronics2
- 5th August 2010, 12:30
http://www.picbasic.co.uk/forum/showthread.php?t=13540&p=92176#post92176 ;)

Just don't quite understand it... (sorry I'm thick ! - care to actually explain how I would do this " Aliases " ... where it is shown how to " slice " your values thing ?)

Ok Malc ...

let's make it easy :

The last PBP2.60 Version has introduced a Qualifier that permits you NOT to care with slicing your variables ...
so, if you own a v2.60 ( and You SHOULD ) ... the link you gave me becomes ... obsolete.

as you, NOW, just have to write:

WRITE Location, Word AlarmHigh[1] , Word AlarmHigh[2] , Word AlarmHigh[3] ,...

READ Location, Word AlarmHigh[1], Word AlarmHigh[2] , Word AlarmHigh[3], WA[1],WA[2],WA[3]

BUT remember a word need 2 EEPROM locations ... so,

WRITE Location +6 , Word AlarmHigh[4]
READ Location +6 , Word AlarmHigh[4] ,WA4

Alain

PS: of course, you can keep on using the " old method "

WRITE Location, AlarmHigh[1].Lowbyte
WRITE Location+1, AlarmHigh[1].Highbyte
WRITE Location+2, AlarmHigh[2].Lowbyte
WRITE Location+3, AlarmHigh[2].Highbyte
WRITE Location+4, AlarmHigh[3].Lowbyte
WRITE Location+5, AlarmHigh[3].Highbyte
WRITE Location+6, AlarmHigh[4].Lowbyte
WRITE Location+7, AlarmHigh[4].Highbyte

... but, is it really reasonnable ???

malc-c
- 5th August 2010, 17:49
The part I really find it hard to grasp is the WA[1], WA[2] bit and how to format the two eprom locations

So assuming the start address is 80

Could this then be changed to


For x = 0 to 3
For fn = 0 to 8
Write Word (80+fn) AlarmHigh[fx] highbyte
If fn >= 4 then
x = 0
Write Word (80+fn) AlarmHigh[fx] lowbyte
Next fn
Next x


I just can't seem to make this sink in !

Acetronics2
- 5th August 2010, 18:18
[QUOTE=malc-c;92222]

So assuming the start address is 80

Could this then be changed to


For x = 0 to 3
For fn = 0 to 8
Write Word (80+fn) AlarmHigh[fx] highbyte
If fn >= 4 then
x = 0
Write Word (80+fn) AlarmHigh[fx] lowbyte
Next fn
Next x


Oh my god ... you really thick ... ( YOU told it, not me ... ;) )

following your explanation:

[code]

For fn = 0 to 3
Write (80+2*fn), Word AlarmHigh[fn+1]
next fn

[code]

LOCATION MUST BE PLACED BEFORE THE DATA !!!

( sorry for other users , but it's the 4 th time I repeat ... ;) )


Business looks to have turned you deaf and blind ... :o

Alain

malc-c
- 5th August 2010, 20:34
This seems to compile



for fn = 0 to 3
write (10*fn + 80),lightsetHR[fn]
write (10*fn + 82),lightsetMN[fn]
write (10*fn + 84),lightoffHR[fn]
write (10*fn + 86),lightoffMN[fn]
write (10*fn + 88),word droptemp[fn]
write (10*fn + 90),word normtemp[fn]
write (10*fn + 92),StartHour[fn]
write (10*fn + 94),StartMin[fn]
write (10*fn + 96),StopHour[fn]
write (10*fn + 98),StopMin[fn]
write (10*fn + 100),word alarmhigh[fn]
Write (10*fn + 102),word alarmlow[fn]
next fn

Acetronics2
- 5th August 2010, 21:06
This seems to compile



for fn = 0 to 3
write (10*fn + 80),lightsetHR[fn]
write (10*fn + 82),lightsetMN[fn]
write (10*fn + 84),lightoffHR[fn]
write (10*fn + 86),lightoffMN[fn]
write (10*fn + 88),word droptemp[fn]
write (10*fn + 90),word normtemp[fn]
write (10*fn + 92),StartHour[fn]
write (10*fn + 94),StartMin[fn]
write (10*fn + 96),StopHour[fn]
write (10*fn + 98),StopMin[fn]
write (10*fn + 100),word alarmhigh[fn]
Write (10*fn + 102),word alarmlow[fn]
next fn


Malc,

Yes, It compiles :o BUT:

IF fn = 1 ...

@ location 110 & 111 you write AlarmHigh[1]

IF fn = 2

location 110 & 111 are overwritten by NormTemp[2]

IF fn = 3

location 110 is overwritten by lighsetHR[3]

and I only give you 3 examples ...

so, ... try




write 80,lightsetHR[1]),lightsetMN[1],lightoffHR[1],lightoffMN[1],word droptemp[1] ,word normtemp[1],StartMin[1],StopHour[1],StopMin[1],word alarmhigh[1],StartHour[1],word alarmlow[1] ; need 16 locations

write 100,lightsetHR[2]),lightsetMN[2],lightoffHR[2],lightoffMN[2],word droptemp[2] ,word normtemp[2],StartMin[2],StopHour[2],StopMin[2],word alarmhigh[2],StartHour[2],word alarmlow[2]

write 120,lightsetHR[3]),lightsetMN[3],lightoffHR[3],lightoffMN[3],word droptemp[3] ,word normtemp[3],StartMin[3],StopHour[3],StopMin[3],word alarmhigh[3],StartHour[3],word alarmlow[3]


write 140,lightsetHR[4]),lightsetMN[4],lightoffHR[4],lightoffMN[4],word droptemp[4] ,word normtemp[4],StartMin[4],StopHour[4],StopMin[4],word alarmhigh[4],StartHour[4],word alarmlow[4]



This Way, your EEPROM Map is neat ... and you know where to find What ... :rolleyes:
...

What did you tell me by PM ??? :D

I just call that pedagogics ...

Alain

malc-c
- 5th August 2010, 22:05
Alain,

Many thanks......

Apologies for the PM - just having a bad day.

I'll give that a try in the morning

rsocor01
- 6th August 2010, 00:08
This seems to compile



for fn = 0 to 3
write (10*fn + 80),lightsetHR[fn]
write (10*fn + 82),lightsetMN[fn]
write (10*fn + 84),lightoffHR[fn]
write (10*fn + 86),lightoffMN[fn]
write (10*fn + 88),word droptemp[fn]
write (10*fn + 90),word normtemp[fn]
write (10*fn + 92),StartHour[fn]
write (10*fn + 94),StartMin[fn]
write (10*fn + 96),StopHour[fn]
write (10*fn + 98),StopMin[fn]
write (10*fn + 100),word alarmhigh[fn]
Write (10*fn + 102),word alarmlow[fn]
next fn


Malcolm,

This code won't work anymore like Alain already mentioned. Now, you have words instead of bytes. Also, now you have 12 variables instead of 10 :cool:.

Now, if you try to write 12*2*4=96 bytes at location 80 then you are going to overwrite all the stuff that you wrote at location 150 with your DATA command (from your other post).

I would suggest that you should try first to clear up the mess by organizing the data that you put in EEPROM. It looks like the amount of data that you are putting in memory by doing "Data @0,0 and Data @150" is 58 bytes for each DATA command. So, instead of "Data @0,0 and Data @150" try something like "Data @0,0 and Data @58". Then from location 116 and up you have no data that could get overwritten.

Next, try the same code as above but with the following changes.


for fn = 0 to 3
write (12*fn + 116),lightsetHR[fn]
write (12*fn + 118),lightsetMN[fn]
write (12*fn + 120),lightoffHR[fn]
write (12*fn + 122),lightoffMN[fn]
write (12*fn + 124),word droptemp[fn]
write (12*fn + 126),word normtemp[fn]
write (12*fn + 128),StartHour[fn]
write (12*fn + 130),StartMin[fn]
write (12*fn + 132),StopHour[fn]
write (12*fn + 134),StopMin[fn]
write (12*fn + 136),word alarmhigh[fn]
write (12*fn + 138),word alarmlow[fn]
next fn

I replaced 10 by 12 since now you have 12 variables. I hope that this helps.

Robert

malc-c
- 6th August 2010, 00:38
Robert, thanks for the input, and yes I agree that it needs tidying up. I've already moved Alain's code down by 10 (ie first line starts at 70, the second 90 etc as I thought that I might be getting close to over-writing data at the 150 location.

Guys, I appreciate the time and trouble you have gone to... sorry that it seemed an uphill struggle - it must be the medication I'm on (that's my story and I'm sticking to it :) )

Acetronics2
- 6th August 2010, 09:21
I've already moved Alain's code down by 10 (ie first line starts at 70, the second 90 etc as I thought that I might be getting close to over-writing data at the 150 location.



Hi, Malc

you also can optimize memory use a bit not to hit location 150 ...

see:

80 = $ 50 ... as you need 16 locations per fn value ...

locations $50 ...$5F for fn = 1
............ $60 ...$6F for fn = 2 ...
...
.............$80 ... $8F for fn = 4 ...

$8F = 143 ... it makes it !!!

so, simply change locations to:



write $50,lightsetHR[1]),lightsetMN[1],lightoffHR[1],lightoffMN[1],word droptemp[1] ,word normtemp[1],StartMin[1],StopHour[1],StopMin[1],word alarmhigh[1],StartHour[1],word alarmlow[1] ; need 16 locations

write $60,lightsetHR[2]),lightsetMN[2],lightoffHR[2],lightoffMN[2],word droptemp[2] ,word normtemp[2],StartMin[2],StopHour[2],StopMin[2],word alarmhigh[2],StartHour[2],word alarmlow[2]

write $70,lightsetHR[3]),lightsetMN[3],lightoffHR[3],lightoffMN[3],word droptemp[3] ,word normtemp[3],StartMin[3],StopHour[3],StopMin[3],word alarmhigh[3],StartHour[3],word alarmlow[3]

write $80,lightsetHR[4]),lightsetMN[4],lightoffHR[4],lightoffMN[4],word droptemp[4] ,word normtemp[4],StartMin[4],StopHour[4],StopMin[4],word alarmhigh[4],StartHour[4],word alarmlow[4]



and moreover, you'll see your values nicely ordered into your EEPROM Watch Window ... ;)
Have a nice day
Alain

PS: sorry , but there's a forum bug with " carriage return ", it seems ...