Storing long negative numbers in EEPROM

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Storing long negative numbers in EEPROM

Post by Creative25 »

Hi there.
A few days ago upgraded to Flowcode 5 Professional.
This gives me the opportunity to play around with EEPROM
Now I would like to know how to store and retrieve long negative numbers. For example the variable Temp containing the number -900 etc.
Is there a special function to do that, or do I have to use calculations?
Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by Benj »

Hello Uli,

You can do this using calculations.

Here I have 4 byte variables and a long variable.

byte0 = long & 0xFF
byte1 = (long >> 8 ) & 0xFF
byte2 = (long >> 16) & 0xFF
byte3 = (long >> 24) & 0xFF

You can now save your bytes to EEPROM.

To get back from bytes to a long you reverse the process.

long = byte0
long = long | (byte1 << 8 )
long = long | (byte2 << 16)
long = long | (byte3 << 24)

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by Creative25 »

Hi Benj
Thanks for the reply.
Sorry for asking such basic Questions
Are these the formulas to brake it down into different variables and combine it again?
Does it mean (long) is the variable containing the long number?
are byte0 to byte3 variables that i would have to create?
What is the longest number that could be stored in this way?

Best Regards:
Uli

User avatar
DavidA
Matrix Staff
Posts: 1076
Joined: Fri Apr 23, 2010 2:18 pm
Location: Matrix Multimedia Ltd
Has thanked: 58 times
Been thanked: 258 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by DavidA »

Hi,

Yes essentially you are breaking down a 32bit number into four 8bit sections which can be stored into the EEPROM, then putting them back together when you need them.

We do have a simple article explaining the process a little better if you want to take a look at it: http://www.matrixmultimedia.com/resourc ... php?id=382

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by Creative25 »

Thanks I understand now.
Maybe this might be a bit out of topic.
At first startup after programming the chip the EEPROM has a value of 255
Is there a way to put initial values into EEPROM while programming the chip?
Or is it better to program the chip in such a way that it puts them on at the first sartup?
Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by Benj »

Hi Uli,

You sure can. On a PIC it is done like this.

Code: Select all

#pragma DATA _EEPROM, 12, 34, 56, 23
Where the four numbers are the decimal values you want to store into the first four bytes of the EEPROM.

To add this to your Flowcode project goto the project options window, click enable supplementary code and then click edit supplementary code and place the above line of code into the top defines window section.

Here is the line of code from the BoostC manual showing how different data types can be pre-programmed into EE memory.

Code: Select all

#pragma DATA _EEPROM, 12, 34, 56, "HELLO", 0xFE, 0b10011001

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by Creative25 »

That sounds cool,
I will try it when I get the time for it.
Best Regards:
Uli

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by AbhijitR »

Hello! Ben
good morning
Benj wrote:
Fri Mar 16, 2012 1:00 pm

You can do this using calculations.

Here I have 4 byte variables and a long variable.

byte0 = long & 0xFF
byte1 = (long >> 8 ) & 0xFF
byte2 = (long >> 16) & 0xFF
byte3 = (long >> 24) & 0xFF

You can now save your bytes to EEPROM.

To get back from bytes to a long you reverse the process.

long = byte0
long = long | (byte1 << 8 )
long = long | (byte2 << 16)
long = long | (byte3 << 24)
I am trying to use the above to store LONG numbers in EEPROM, i am able to break/split the LONG number to 4 Bytes and store too, but when i do the reverse, the action serve the joining and getting the LONG number but not the same number which i split and also negative (-) sign is added.

Does that mean the process is different to split and join the positive numbers.

Thank you, hear you soon.

Abhi

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by mnf »

Hi Abhi,

Can you post your code - I would guess the bytes are getting reversed in the process because the technique looks ok..

Martin

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by AbhijitR »

Hello! Martin
good morning

Thank you for your reply, i am trying to use exactly the same like Ben has mentioned in his post, attached is the chart for your reference. I would like to mention one thing this works very well in simulation but when i run this at actual then i saw the mistake of displaying different number and with (-) negative sign.

Abhi
Attachments
Long_EEPROM_20-10-2021.fcfx
(12.85 KiB) Downloaded 144 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by mnf »

Hi Abhi,

Just on my phone at moment - so can't test...

I would suggest making a loop to read and write values (as separate macros)

So in pseudocode:

Code: Select all

for .i = 0..3
   Write (.i +addr, .n)	// Writes LSB
   .n = .n >> 8		       // shift next byte into LSB
end loop
The addr variable allows writing to other than 0...

Reading back is slightly more involved:

Code: Select all

.value = 0 
for i = 0 to 3
	.value = .value << 8
	.b = read(addr + (3 - .i))
	value = .value + .b 
end loop
return = .value
You might need a delay after the writes (somewhere on here I posted an eeprom component that handled multi-bytes which made life easier - but was for a specific eeprom)

- but your code looks okay. Have you tried printing the bytes out to see if written / read correctly? It might be a signing trick from C (byte is unsigned in FC) so shifting might give an anomaly when converted to signed long (try unsigned long see if that works ok?)

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by medelec35 »

Hi Abhi,
The attached flowchart works for me.
It converts ulong to bytes.
saves the bytes within EEPROM.
Clears the byte variables then retrieves the bytes from EEPROM.
Converts retrieved bytes back to EEPROM, then displays the converted value.
If the two values on the display matched then you know all is working as it should.
Attachments
Store & Retrieve Long within EEPROM v1.fcfx
(15.31 KiB) Downloaded 132 times
Martin

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by AbhijitR »

Hello! Martin
good evening

Your trick worked like a piece of cake, million thanks again, as usual SAVIOUR.

For knowledge one of the warnings after compiling the chart disappeared, it was as below
Total_16BADC_SDC_RTC_OLED_20-10-2021.c: 16714: (753) undefined shift (16 bits) (warning)
Total_16BADC_SDC_RTC_OLED_20-10-2021.c: 16715: (753) undefined shift (24 bits) (warning)
that was causing the problem i think, i ignored that because did not understand. On this note how serious are the warnings, and how to learn which one is serious or could be ignored, sounds tough to me.

Hello! Martin (mnf)
Thanks for your efforts.

Abhi

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by medelec35 »

Excellent, I'm glad the issue is resolved for you.
Conversions can be hit and miss especially with Longs/Ulongs
For that reason, Ben created an excellent component to help with that.
Within Flowcode search for conv/At the top of the list will be Type conversions.
There is a really good example flowchart within the wiki.
Of course, there are warnings that can be ignored e.g

Code: Select all

Warning unreferenced functions removed

Code: Select all

variable "NameOfVaraible" is not used (warning)
However, the

Code: Select all

undefined shift
or

Code: Select all

stack corruption
cant be ignored.
I know there are plenty more examples but listed a couple of the most common warnings for both can and can't be ignored.
Martin

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by AbhijitR »

Hello! Martin
good morning

Yes, Ben has worked on almost every probable doubt and question one can have, thank you to everyone to make Flowcode a wonderful option to program micro, someone like me was helpless few years back when i did not had Flowcode and "C" was not my cup of tea.

Cheers to the thought of making Flowcode.

Abhi

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Storing long negative numbers in EEPROM

Post by medelec35 »

AbhijitR wrote:
Wed Oct 20, 2021 10:08 am
but when i do the reverse, the action serve the joining and getting the LONG number but not the same number which i split and also negative (-) sign is added.
Just thought I would mention that Ben's post was in 2012 and the OP was asking about Flowcode V5
That did not use the compiler that is used in Flowcode V7 and above.
It used BoostC compiler, so it would have worked with

Code: Select all

long = byte0
long = long | (byte1 << 8 )
long = long | (byte2 << 16)
long = long | (byte3 << 24)
As now using a diferrerent compiler, converstion needs to work differently.
Martin

Post Reply