Page 1 of 1

Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 12:00 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 1:00 pm
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)

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 2:40 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 3:55 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 4:16 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 4:28 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Fri Mar 16, 2012 6:00 pm
by Creative25
That sounds cool,
I will try it when I get the time for it.
Best Regards:
Uli

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 10:08 am
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

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 11:00 am
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

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 11:31 am
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

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 11:54 am
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

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 3:19 pm
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.

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 5:07 pm
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

Re: Storing long negative numbers in EEPROM

Posted: Wed Oct 20, 2021 5:53 pm
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.

Re: Storing long negative numbers in EEPROM

Posted: Thu Oct 21, 2021 7:24 am
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

Re: Storing long negative numbers in EEPROM

Posted: Sun Oct 24, 2021 10:20 am
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.