EEPROM Variables (EE_Vars.pbp)


Closed Thread
Results 1 to 40 of 80

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default EEPROM Variables (EE_Vars.pbp)

    Hi everybody,

    First, I'd like to say that, I've gone overboard on the documentation here. It might look like a monstrous program that you'll never figure out, but really, it's very easy to use. I just tried to answer all the questions ahead of time, and I'm hoping it doesn't cause confusion instead.

    This program is intended to be a demonstration of what you can do with Macro's and Include file's.
    It was not meant to be the most efficient way of doing things..., just the easiest.

    Hope you can use it.
    Last edited by Darrel Taylor; - 22nd September 2005 at 07:59.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <a name="EE_index"></a>
    EEPROM Variables (EE_Vars.pbp)
    1. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Intro">Introduction</a>
    2. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Linked">Linked Variables</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Var">EE_var</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_read_var">EE_read_var</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write_var">EE_write_var</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write_default">EE_write_d efault</a>
    3. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_UnLinked">Unlinked Variables</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_byte">EE_byte</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_word">EE_word</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_read">EE_read</a>
      • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write">EE_write</a>
    4. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_start">EE_start</a>
      <!--
    5. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Res">Resources Used by EE_Var</a> -->
    6. <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Samples">Example Programs</a>
    Attached Files Attached Files
    Last edited by Darrel Taylor; - 22nd September 2005 at 07:23.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <a name="EE_Intro"></a>
    &nbsp;Introduction

    EE_Vars.pbp is an INCLUDE file that takes a lot of the "Hassle" out of working with EEPROM data. It has several advantages over PicBasic's EEPROM, DATA, READ and WRITE statements.
    EEPROM and DATA are ok for setting default values in EEPROM, but it only does it when the chip is programmed. After that, all those default values are gone. They aren't stored anywhere in the program. So there's no way to go back to the default values without re-programming the chip again. Plus, it's all just a bunch of numbers, one after another. You can't give them variable names, or sizes. All of that has to be handled manually (by you).

    Read and write can be difficult to work with as well. You have to do everything 1 byte at a time. You have to keep track of what EEPROM locations have what data in them. And then, figure out how to get the data from the right place when you need it. And, with the *2 problem on the 18F EEPROM locations, sometimes it's just not worth the "hassle".
    Enter EE_Vars.pbp

    This program does all of that for you, and much more.&nbsp; It assigns locations in EEPROM for "Persistant" variables.&nbsp; Maintains "default" values for each EE_Var.&nbsp; And, creates easy to use commands for accessing those variables in EEPROM.&nbsp; It can "Link" the EE variables with your PBP variables, so that they automatically have the correct value on Power-up", or it can work with "Un-Linked" EE variables that can be easily Read or Written to/from any PBP variable.

    It also protects against WRITE errors caused by an interrupt in the middle of a write sequence by verifying the data, after a WRITE. If it doesn't match, it goes back and tries again. After a predetermined number of tries (10 default), if it's still unsuccessfull, it will Disable Global interrupts and WRITE it one more time.

    The program will work on any PIC chip that has Internal EEPROM space (NOT internal I2C EEPROM). But, it requires MPASM as the assembler. It won't work with PM.


    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <!-- --------------------------------- -->
    <a name="EE_Linked"></a>
    &nbsp;Linked Variables

    Here is an example of a "Linked" EE/PBP variable.
    Code:
    <font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;EE_Vars.PBP&quot;
    
    </font><b>Days   </b><font color="#008000"><b>VAR  WORD             </b></font><font color="#0000FF"><b><i>; Create your variable like normal
    </i></b></font><font color="#000080">@  EE_var  _Days, WORD, 365  </font><font color="#0000FF"><b><i>; Link an EE variable to it.</i></b></font>
    With 3 simple lines, you now have a PBP variable that is initialized from EEPROM on Power-UP. It's has a "default" value of 365 that will be initialized in EEPROM on the first "RUN" after programming, and can be restored to the default value at any time you need. The PBP variable can be used just like normal throughout your program. Then, anytime you want to permenantly save the value to EEPROM, you simply call the EE_write_var routine. On the next Reset of the processor, the PBP variable will automatically have the new value.

    These are the commands for "Linked" variables.
    • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_Var">EE_var</a>&nbsp;&nbsp;-&nbsp;&nbsp;Associate(link) PBP/EE_Vars
    • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_read_var">EE_read_var</a>&nbsp;&nbsp;-&nbsp;&nbsp;Read a Linked variable from EEPROM
    • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write_var">EE_write_var</a>&nbsp;&nbsp;-&nbsp;&nbsp;Write a Linked variable to EEPROM
    • <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write_default">EE_write_d efault</a>&nbsp;&nbsp;-&nbsp;&nbsp;Write the default value to EEPROM

    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_Var"></a>
    &nbsp;EE_Var&nbsp;&nbsp;&nbsp;(EE_variable)

    <pre>@ EE_var Variable, Size, Value</pre>
    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> that will be Linked to an EEPROM location. The variable name must be preceeded by an Underscore "_", unless the variable was declared as a SYSTEM variable. The "case" must match exatcly with the original declaration of the PBP variable.

    Size - The size of the variable. Either BYTE or WORD. (case sensitive)

    Value - The "default" value associated with this variable. This constant will be initialized in EEPROM on the first "RUN" after programming. It can also be restored at any time with the <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_write_default">EE_write_d efault</a> command.
    EE_Var will probably be the most used macro in this collection. In fact, many programs will only need this, and the EE_write_var macro to do everything required to maintain all your values in EEPROM.

    It's purpose is to create a location in EEPROM that is "Linked" with a previously declared PBP variable. It also handles the "First RUN" initialization.

    Example:
    <pre>Setpoint VAR BYTE<br>@ EE_var _Setpoint, BYTE, 150
    RunTime VAR WORD<br>@ EE_var _RunTime, WORD, 1200</pre>
    For easier readability, both statements can be placed on the same line, with a colon ":" in-between.
    Code:
    Setpoint  VAR  BYTE  :  @  EE_var  _Setpoint, BYTE, 150
    RunTime   VAR  WORD  :  @  EE_var  _RunTime, WORD, 1200
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_read_var"></a>
    &nbsp;EE_read_var

    <pre>@ EE_read_var Variable </pre>
    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> name that you want to read from EEPROM.
    EE_read_var will read the EEPROM value that has been Linked to a PBP variable with the EE_var command. &nbsp; It can only be used with Linked variables.

    On Power-up the variable will already be set to the value in EEPROM. So, the only time you would need to use this command is if you have used the variable for other puposes, and want to restore it to the "Power-up" state.

    Example:
    <pre>@ EE_read_var _Setpoint
    @ EE_read_var _RunTime</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_write_var"></a>
    &nbsp;EE_write_var

    <pre>@ EE_write_var Variable </pre>
    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> name that you want to write to EEPROM.
    After you have changed the value of a variable in your program, you can save it to EEPROM by using the EE_write_var command. Once written, the PBP variable will automatically contain the new value on the next "Power-up".

    Example:
    <pre>@ EE_write_var _Setpoint
    @ EE_write_var _RunTime</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_write_default"></a>
    &nbsp;EE_write_default

    <pre>@ EE_write_default Variable </pre>
    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> name that you want to restore the default for.
    EE_write_default will restore the default value that was defined with an EE_var statement. That value will be written to EEPROM, and it will also be placed in the Linked PBP variable.

    Example:
    <pre>@ EE_write_default _Setpoint
    @ EE_write_default _RunTime</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    Last edited by Darrel Taylor; - 22nd September 2005 at 06:48.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <!-- --------------------------------- -->
    <a name="EE_UnLinked"></a>
    &nbsp;UnLinked Variables

    Unlinked EE variables, are not linked to any PBP variables, but can allow greater freedom in how you handle them.

    They allow you to create meaningfull names to represent EEPROM locations. &nbsp;And, those locations can have default values, just like the Linked EE_Vars.

    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_byte"></a>&nbsp;EE_byte

    <pre>@ EE_byte Name, Value</pre>Define a BYTE sized location in EEPROM, with a Unique name, and default value.
    Name - A unique Name to identify this EE variable.

    Value - The "default" value associated with this variable. This constant will be initialized in EEPROM on the first "RUN" after programming. It can also be restored at any time with the <pre>@ EE_write Name, default</pre> command.
    Example:
    <pre>@ EE_byte Version, 1<br>@ EE_byte VersionMinor, 0</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_word"></a>&nbsp;EE_word

    <pre>@ EE_word Name, Value</pre>Define a WORD sized location in EEPROM, with a Unique name, and default value.
    Name - A unique Name to identify this EE variable.

    Value - The "default" value associated with this variable. This constant will be initialized in EEPROM on the first "RUN" after programming. It can also be restored at any time with the "@&nbsp; EE_write&nbsp;&nbsp; Name, default" command.
    Example:
    <pre>@ EE_word Speed, 1100<br>@ EE_word PWMfreq, 20000</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_read"></a>&nbsp;EE_read

    <pre>@ EE_read Name, Variable</pre>
    Name - A Name that was previously defined using either EE_byte or EE_word.

    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> that will receive the value from EEPROM.

    Note: The variable must be the same size as the EE Variable you are using. If it was defined with EE_word, and you supply EE_read with a byte sized variable, another variable will be overwritten with the High byte of the word.
    Example:
    <pre>Mspeed VAR WORD
    @ EE_read Speed, _Mspeed

    Version VAR BYTE
    @ EE_read Version, _Version</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>
    _________________________________
    <a name="EE_write"></a>&nbsp;EE_write

    <pre>@ EE_write Name, Variable</pre>
    Name - A Name that was previously defined using either EE_byte or EE_word.

    Variable - The <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#notes">PBP variable</a> that holds the value to be written to EEPROM. OR, the word "default"

    Note: The variable must be the same size as the EE Variable you are using. If it was defined with EE_word, and you supply EE_write with a byte sized variable, 1 garbage byte from another variable will be written to EEPROM.
    Example:
    <pre>Mspeed = 2000<br>@ EE_write Speed, _Mspeed
    Version = 2<br>@ EE_write Version, _Version</pre>
    <a href="http://www.picbasic.co.uk/forum/showthread.php?t=2444#EE_index">up to index</a>

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <a name="notes"></a>Notes:

    PBP variables - The PBP variable name must be preceeded by an Underscore "_", unless the variable was declared as a SYSTEM variable. The "case" must match exatcly with the original declaration of the PBP variable.
    <br>
    <br>
    Last edited by Darrel Taylor; - 22nd September 2005 at 08:22.

  7. #7
    Join Date
    Feb 2006
    Posts
    20


    Did you find this post helpful? Yes | No

    Default EE_Vars starting address

    Hello Darrel, I see this line in the include file.

    EE_start CON 0 ; Starting address for EE_Vars

    Is this the starting address for your EEprom storage? I use the beginning addresses for specific things that may not be able to use your EE_Vars method. I actually want that area blocked out.

    Can I mix and match your method with generic PBP read and write if I set the EE_Start address up higher. Say for an 18F part if I set it to 512 that would leave the bottom half for me to do what I want with and the top to experiment with your method.

    Is this correct?

    Thanks, Bob

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by brittons View Post
    Is this the starting address for your EEprom storage? I use the beginning addresses for specific things that may not be able to use your EE_Vars method. I actually want that area blocked out.
    That's the way I read it.

    Can I mix and match your method with generic PBP read and write if I set the EE_Start address up higher. Say for an 18F part if I set it to 512 that would leave the bottom half for me to do what I want with and the top to experiment with your method.
    I don't see why not. Just be careful you don't start overwriting what you don't want to start overwriting...

  9. #9
    Join Date
    Feb 2006
    Posts
    20


    Did you find this post helpful? Yes | No

    Default How can I reinitialize the 'First Run'

    Yes, I tried setting the starting address to 511 and the variables get stored there.

    I haven't been able to get the variables to start out in the 'First Run' or default value unless I execute the EE_write_default command. Is there a way to create the 'First Run' sate of the vars without doing this? It seems when you create the var using EE_var the first time it creates the default value but I was only able to do it for the first var I experimented with. Subsequent attempts seemed to come up with 'FFFF' as the default value.

    I have been reading through the EE_Var.pbp code but I have to admint my knowledge of macros is limited. Any recomendations on where I can get up to speed on macros?

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Reinitialize the first run? You mean program a set of initial values into the eeprom?

  11. #11
    Join Date
    Feb 2006
    Posts
    20


    Did you find this post helpful? Yes | No

    Default

    Yes, I've been trying this:

    Setpoint VAR BYTE
    @ EE_var _Setpoint, BYTE, 150

    RunTime VAR WORD
    @ EE_var _RunTime, WORD, 1200

    As shown in the docs but I look at Setpoint and Runtime using MicroCode studio ICD I get 'FF' for Setpoint and 'FFFF' for RunTime. The 'default value is not created when I execute the above.

    If I then do this:
    @ EE_write_default _Setpoint
    @ EE_write_default _RunTime

    The values become 150 and 1200 but only after I perform EE_write_default

    I must be doing something wrong. Note as I said before I have set the start address to 511 but I do see the new values 150 an 1200 stored in EEprom up above 511 so I think this is working properly

  12. #12


    Did you find this post helpful? Yes | No

    Question Having difficulty with EEPROM variables

    Hi Darrel,
    I am working on a piece of code that uses the Timer 1 to create a timed interupt for turning on and off LEDs. The PIC is a 16F887. I wish use the EEPROM for saving information. All of the code seems to work well until I include and try to use the EEPROM Variables (EE_Vars.pbp). The EE_Vars seem to operate as expected - however my Timer 1 interupt runs erratically and sometimes halts. The time base is rather brief (1 mS) and I am using an external resonator as clock source (20MHz). I am using your "DT_INTS-14.bas" and "ReEnterPBP.bas" to support the Timer 1 interupt.
    I am curious as to what I am doing wrong. The best diagnostic indication I have was sending the OSCCON variable to the port D. The values indicated that the PIC was going to internal LF oscillator when it should have been using the external resonator. Can you give me some wise counsel???

    Thanks in advance, Mitch
    Attached Files Attached Files

  13. #13
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    It's difficult to say with "large chunk of code left out .." in the routine that's having a problem.

    I don't see any usage of the EE_Var routines, other than the declarations.
    Are you trying to use them inside the interrupt handler? In the missing code?
    <br>
    DT

  14. #14
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    After a closer inspection, it looks like you are probably overflowing the Stack.

    When the Timer triggers an interrupt, that uses 1 level.
    When it gets to the handler, it GOSUB's to TimerIO. That's level 2.
    An Array operation uses 3 levels internally for a total of 5 levels.

    Then in the Main loop, you also GOSUB MonitorIO, so you are already 4 levels deep when it gets interrupted.

    16F's only have 8 stack levels.
    Last edited by Darrel Taylor; - 22nd January 2009 at 00:00. Reason: MonitorIO/TimerIO
    DT

  15. #15


    Did you find this post helpful? Yes | No

    Default Big OOPs

    I can provide the chunk of code if you would like to review it, however your point about the stack is well taken and "me thinks" you are on target. I will see if I can simplify. Thanks

  16. #16
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    I'm sure everyone on the planet (except me) already knows about this, but since I just blew a couple hours trying to figure this out I figured I'd post it here incase it trips up someone else...

    I've been playing with Darrel's EE_Vars.pbp, trying to make it work. (I'm using PBP/MCS)

    The sample file "EE_1_Example.bas" worked PERFECT, after I changed the LCD formatting for my hardware.

    But then I tried it with one of my existing programs, and it wouldn't compile... MCS kept whining about "Symbol not previously defined (Byte)"

    At one point I ended up with 2 apparently identical lines of code, and 1 worked and the other didn't. ???

    As it turns out, it was because of incorrect case in my variable definition. But what threw me was that part of the declaration wants UPPER case and part wants LOWER case.


    This works fine:
    Code:
    wastedtime       var  byte : @  EE_var  _wastedtime, BYTE, 139
    This does not work:
    Code:
    wastedtime       var  byte : @  EE_var  _wastedtime, byte, 139
    This does not work:
    Code:
    wastedtime       var  BYTE : @  EE_var  _wastedtime, BYTE, 139
    This does not work:
    Code:
    wastedtime       var  BYTE : @  EE_var  _wastedtime, byte, 139
    Of course it would have been much easier to figure out if the "Uppercase All" option for the reserved word formatting hadn't been on. But of course since it makes all the reserved words BOLD uppercase it all looked the same. I couldn't figure out why with 2 apparently identical lines, one would work and the other not. Once I turned off the Uppercase All option, then it became clear that BYTE was not in the same case...

    So... why does it demand lowercase on one side of the definition and uppercase on the other?


    Steve

Similar Threads

  1. Can't read sequential addresses in external EEPROM
    By tjkelly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th February 2010, 14:46
  2. Problem with I2C EEPROM addressing
    By Atom058 in forum General
    Replies: 14
    Last Post: - 3rd November 2009, 03:17
  3. PIC16F684 + LCD to use the 256bytes of EEPROM - HELP
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th March 2008, 14:19
  4. How to write/read strings EEPROM/LCD
    By g-hoot in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 11th February 2007, 06:26
  5. word variable to 25lc640
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th July 2004, 19:59

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts