I need to store an integer that will be anywhere from -90 to 450 into EPROM and be able to read it on power up.
I have downloaded several of the sample files I have found but I am still having issues. Anyone have a very simple example?
Bob
Store and read from EPROM <Pic16F1827>
Moderator: Benj
-
- 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: Store and read from EPROM <Pic16F1827>
The way you can store int values in EEPROM is to split integers into high and low bytes, there store these in two separate EEPROM locations.
When both values are retrieved, you can then convert high and low byes into a single integer.
To help you are are two different ways you can convert from byte to integer and vice-versa
Or
Not tried the lower three formulas within Flowcode but have included them so you will know how conversions can be done mathematically. Both methods should work OK.
I know this method works if positive numbers as I have used it.
With negative numbers since you can't get -ve bytes, perhaps you can convert -ve integer to positive integer, then have a 3rd location in EEPROM for sign. eg 0 for positive, 1 for negative.
Read all three locations and converted 1st two locations containing bytes to int.
then have a decision branch:
if value of 3rd loc is 0 then leave result as positive.
otherwise if = 1 then result is negative. (i.e: result = 0 - result)
If you get stuck, do what you can post flowchart if it's not commercially sensitive and I can help you complete it.
When both values are retrieved, you can then convert high and low byes into a single integer.
To help you are are two different ways you can convert from byte to integer and vice-versa
Code: Select all
ByteLow = IntValue & 0xFF
ByteHigh = (IntValue >> 8) & 0xFF
Code: Select all
IntValue = ByteLow+(ByteHigh << 8)
Code: Select all
ByteHigh = (IntValue/ 256)
ByteLow = IntValue - ByteHigh* 256
Code: Select all
IntValue = ByteLow+(ByteHigh * 256)
I know this method works if positive numbers as I have used it.
With negative numbers since you can't get -ve bytes, perhaps you can convert -ve integer to positive integer, then have a 3rd location in EEPROM for sign. eg 0 for positive, 1 for negative.
Read all three locations and converted 1st two locations containing bytes to int.
then have a decision branch:
if value of 3rd loc is 0 then leave result as positive.
otherwise if = 1 then result is negative. (i.e: result = 0 - result)
If you get stuck, do what you can post flowchart if it's not commercially sensitive and I can help you complete it.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: Store and read from EPROM <Pic16F1827>
Not sure if I am going about this the most clean way.
Write to EPROM example:
NUMtoSTORE can = -90 to 450
NUMtoSTORE>255
If NUMtoSTORE= Str_High = 255 <--- put 255 in EPROM location 0
Str_low = NUMtoSTORE - 255 <----- Put 95 into EPROM location 1
If NUMtoSTORE<0 Then Pos_Neg=0 <----- Puts 0 into EPROM location 2 (puts a 1 if neg number)
It adds the 2 values together when reading the EPROM and multiples the value by -1 if location 2 =1 to give me back my neg number.
I am playing with this in FC4 and it is working, but there may be a cleaner (Faster) way.
I only need this for remembering the POSITION of the antenna for power down and up.
I guess I am going to learn how to write MACRO calls now.
Write to EPROM example:
NUMtoSTORE can = -90 to 450
NUMtoSTORE>255
If NUMtoSTORE= Str_High = 255 <--- put 255 in EPROM location 0
Str_low = NUMtoSTORE - 255 <----- Put 95 into EPROM location 1
If NUMtoSTORE<0 Then Pos_Neg=0 <----- Puts 0 into EPROM location 2 (puts a 1 if neg number)
It adds the 2 values together when reading the EPROM and multiples the value by -1 if location 2 =1 to give me back my neg number.
I am playing with this in FC4 and it is working, but there may be a cleaner (Faster) way.
I only need this for remembering the POSITION of the antenna for power down and up.
I guess I am going to learn how to write MACRO calls now.
-
- 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: Store and read from EPROM <Pic16F1827>
Yes that sounds about right.Bobw wrote:Write to EPROM example:
NUMtoSTORE can = -90 to 450
NUMtoSTORE>255
If NUMtoSTORE= Str_High = 255 <--- put 255 in EPROM location 0
Str_low = NUMtoSTORE - 255 <----- Put 95 into EPROM location 1
If NUMtoSTORE<0 Then Pos_Neg=0 <----- Puts 0 into EPROM location 2 (puts a 1 if neg number)
It adds the 2 values together when reading the EPROM and multiples the value by -1 if location 2 =1 to give me back my neg number..
When variables are assigned a value then this value is retained until power is interrupted.If variable is an integer then value can be from -32768 to 32767.Bobw wrote: but there may be a cleaner (Faster) way.
I only need this for remembering the POSITION of the antenna for power down and up.
I guess I am going to learn how to write MACRO calls now.
However EEPROM which retains its memory after power is interrupted, can only store bytes which is 0 - 255
That is why I suggested splitting integer into bytes, storing in EEPROM.
Like wise retrieving bytes from EEPROM, and converting back to integers.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: Store and read from EPROM <Pic16F1827>
Good,
I think I am getting the hang of it all. Glad FC will let you copy parts of things from one saved file to another.
I have kind of built my code in chunks and saved each under a different name as I went. Time to start putting it all together into one file.
Can't thank you guys enough for all the help.
Bob
I think I am getting the hang of it all. Glad FC will let you copy parts of things from one saved file to another.
I have kind of built my code in chunks and saved each under a different name as I went. Time to start putting it all together into one file.
Can't thank you guys enough for all the help.
Bob
-
- 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: Store and read from EPROM <Pic16F1827>
Your welcome.Bobw wrote:Good,
Glad FC will let you copy parts of things from one saved file to another.
I have kind of built my code in chunks and saved each under a different name as I went. Time to start putting it all together into one file.
Can't thank you guys enough for all the help.
Bob
If you don't already know, you can import macros (including all assigned variables) directly into flowcode.
If you have not created a macro, and only got main:
Click on macro, new. Enter a name but don't change any of the parameters.
Select all of your flowchart ,and select copy. paste flowchart in the new macro. Then go to macro menu and select export..
Finally on the flowchart you want to import, select Macro menu and import.
If variables are the same, then ignore conflict message, and just select keep existing names.
You can either keep macro and use a macro component to call macro. Or
you can cut the flowchart on the macro , then paste in main.
You are correct you can also copy and paste but that does not assign variables, if not using macro, you will need to add variable, if does not already exist.
BTW when I said that sounds about right, you dont add the two numbers together.
you could either use IntValue = ByteLow+(ByteHigh << 8)
Or IntValue = ByteLow+(ByteHigh * 256)
So your adding ByteHigh x 256 to ByteLow
Hope this helps, and not confuses.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: Store and read from EPROM <Pic16F1827>
Nope,
That helps very much.
When I did BASIC programming many years ago use to call them GOSUBs.
That helps very much.
When I did BASIC programming many years ago use to call them GOSUBs.
-
- 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: Store and read from EPROM <Pic16F1827>
I also used basic, in the commodore and spectrum days.The are many basic equivalents in flowchart. As you correctly said GOSUB = call macro
ON ( or CASE in other languages) = switch
GOTO = connection point
IF var IS GREATER THAN ETC = decision
DIM = var[array] when you add a variable.
When using LCD component: PRINT "STRING" = ..... well print string
ON ( or CASE in other languages) = switch
GOTO = connection point
IF var IS GREATER THAN ETC = decision
DIM = var[array] when you add a variable.
When using LCD component: PRINT "STRING" = ..... well print string
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: Store and read from EPROM <Pic16F1827>
Hey, got it working. Didn't use a macro call, just added it to the end of the program.
Just have to wait for my hall sensor to arrive and I can start putting this thing in a pretty box.
Although not sure how many pulses are on the magnet ring I used, I think 16, but that is just some easy math to figure out the pulse to degree of movement again. At least now I have a way to count the pulses for me.
Bob
Just have to wait for my hall sensor to arrive and I can start putting this thing in a pretty box.
Although not sure how many pulses are on the magnet ring I used, I think 16, but that is just some easy math to figure out the pulse to degree of movement again. At least now I have a way to count the pulses for me.
Bob