Storing long negative numbers in EEPROM
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Storing long negative numbers in EEPROM
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
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
- 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
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)
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)
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Re: Storing long negative numbers in EEPROM
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
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
- 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
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
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
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Re: Storing long negative numbers in EEPROM
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
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
- 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
Hi Uli,
You sure can. On a PIC it is done like this.
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.
You sure can. On a PIC it is done like this.
Code: Select all
#pragma DATA _EEPROM, 12, 34, 56, 23
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Re: Storing long negative numbers in EEPROM
That sounds cool,
I will try it when I get the time for it.
Best Regards:
Uli
I will try it when I get the time for it.
Best Regards:
Uli
- AbhijitR
- Posts: 300
- Joined: Fri Nov 07, 2014 12:48 pm
- Location: Pune, India
- Has thanked: 284 times
- Been thanked: 80 times
Re: Storing long negative numbers in EEPROM
Hello! Ben
good morning
Does that mean the process is different to split and join the positive numbers.
Thank you, hear you soon.
Abhi
good morning
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.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)
Does that mean the process is different to split and join the positive numbers.
Thank you, hear you soon.
Abhi
- AbhijitR
- Posts: 300
- Joined: Fri Nov 07, 2014 12:48 pm
- Location: Pune, India
- Has thanked: 284 times
- Been thanked: 80 times
Re: Storing long negative numbers in EEPROM
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
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 222 times
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: Storing long negative numbers in EEPROM
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:
The addr variable allows writing to other than 0...
Reading back is slightly more involved:
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
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
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
- 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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Storing long negative numbers in EEPROM
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.
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 200 times
Martin
- AbhijitR
- Posts: 300
- Joined: Fri Nov 07, 2014 12:48 pm
- Location: Pune, India
- Has thanked: 284 times
- Been thanked: 80 times
Re: Storing long negative numbers in EEPROM
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
Hello! Martin (mnf)
Thanks for your efforts.
Abhi
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
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.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)
Hello! Martin (mnf)
Thanks for your efforts.
Abhi
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Storing long negative numbers in EEPROM
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
However, the or 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.
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)
Code: Select all
undefined shift
Code: Select all
stack corruption
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
- AbhijitR
- Posts: 300
- Joined: Fri Nov 07, 2014 12:48 pm
- Location: Pune, India
- Has thanked: 284 times
- Been thanked: 80 times
Re: Storing long negative numbers in EEPROM
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Storing long negative numbers in EEPROM
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)
Martin