Page 1 of 1

Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 1:47 am
by Bobw
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

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 6:54 am
by medelec35
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

Code: Select all

ByteLow = IntValue & 0xFF 
ByteHigh = (IntValue >> 8) & 0xFF 

Code: Select all

IntValue = ByteLow+(ByteHigh << 8) 
Or

Code: Select all

ByteHigh = (IntValue/ 256) 
ByteLow = IntValue - ByteHigh* 256 

Code: Select all

IntValue = ByteLow+(ByteHigh * 256) 
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.

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 3:19 pm
by Bobw
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.

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 3:43 pm
by medelec35
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..
Yes that sounds about right.
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.
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.
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.

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 5:55 pm
by Bobw
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

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 6:17 pm
by medelec35
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
Your welcome.
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.

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 6:24 pm
by Bobw
Nope,
That helps very much.
When I did BASIC programming many years ago use to call them GOSUBs.

Re: Store and read from EPROM <Pic16F1827>

Posted: Wed Feb 09, 2011 10:36 pm
by medelec35
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 :P

Re: Store and read from EPROM <Pic16F1827>

Posted: Thu Feb 10, 2011 12:33 am
by Bobw
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. 8)

Bob