Saving Variables


Closed Thread
Results 1 to 5 of 5
  1. #1
    Tear's Avatar
    Tear Guest

    Default Saving Variables

    Hi,

    I am working on a simple trigger audio application. In my code I have it so that I can adjust delay with one up (longer) and one down (shorter) delay button. What I want to do is when both buttons are pushed have it save the value of the delay variable to the microcontroller, so that when the microcontroller is restarted it will load this value for the delay rather than having to reset it every time. If this is possible how would I write the code for it and include it in my project.

    Thanks for the help,
    Michael

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Michael,

    M>What I want to do is when both buttons are pushed have it save the value of the delay variable to the microcontroller, so that when the microcontroller is restarted it will load this value for the delay rather than having to reset it every time. If this is possible how would I write the code for it and include it in my project.<<

    Well, you can save it in Eprom and load it from beginning. Here is a Psuedo code:

    HoldVar Var byte
    Button1 Var Port A.0
    Button2 Var Port A.1

    Read 0, HoldVar ;put EEprom in HoldVar

    Loop:
    if Button1=1 then
    pauseus 100
    If Button2=1 then
    Write 0, HoldVar
    HoldVar=HoldVar -1
    Pause 100
    endif
    HoldVar=HoldVar+1
    endif

    if Button2=1 then
    pauseus 100
    If Button1=1 then
    Write 0, HoldVar
    HoldVar=HoldVar +1
    Pause 100
    endif
    HoldVar=HoldVar-1
    endif

    Do whatever you want....
    goto Loop

    The Pauseus and Pause can be varied to a value that works for you...
    I know there are other ways to do this *=} But have fun.

    Dwayne
    Last edited by Dwayne; - 15th June 2005 at 22:01.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Tear's Avatar
    Tear Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the help Dwayne,

    I have a new question concerning your idea of writin to the eprom. Can you save over the same space in eprom? Also, can you write to the eprom inside your program?

    Looking at your code you have the write command I assume to write to the eprom, however, is that writing over the same memory location in the eprom to prevent from filling all the memory with variables delay times?

    I need to be able to save the holdvar variable periodically in the program at different values. If I can simply write and rewrite to the same memory spot in the eprom from the program and then read the variable on startup then this will take care of all my problems.

    Thanks,
    Michael
    Last edited by Tear; - 16th June 2005 at 15:22.

  4. #4
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Tear,

    Tear>>I have a new question concerning your idea of writin to the eprom. Can you save over the same space in eprom? Also, can you write to the eprom inside your program?<<

    Yes to both...You can overwrite your Eprom time and time again... and you can also write to your Eprom inside your program. The simple "Write" command does it.

    Tear>>Looking at your code you have the write command I assume to write to the eprom, however, is that writing over the same memory location in the eprom to prevent from filling all the memory with variables delay times?

    Yes, it is...

    Write 0, Holdvar... it is writing over location "Zero" of the Eprom... You can change this to 1, and write over Eprom position #1, without affecting #0.


    Tear>>I need to be able to save the holdvar variable periodically in the program at different values. If I can simply write and rewrite to the same memory spot in the eprom from the program and then read the variable on startup then this will take care of all my problems.<<

    Then your problems are solved <smile>... This is exactly what is happening. I will send you a "Bill" for one handshake and one smile the next time I get to see you in person;=}

    Remember the Pauseus and Pause are "timing" pauses. That computer chip is moving at a scorching speed. So you need a small amount of time for a human being to have both buttons pressed "exactly" at the same time. The Pauseus allows for this human error. Then you need a pause, so that the counter doesn't count from 1 to 256 in less time it takes to blink a eye. Thus the Pause is used to vary that. Maybe count by "1"'s every 1/10 of a second? Or you can put a "check" in there and do the following:

    if Button1=1
    HoldVar=HoldVar + 1
    pauseus 100 (debouce that I forgot to add <g>)
    endif.
    while Button1=1 wend.

    What this does, is only count 1 at a time, until you "release" button1. The chip will be in a infinite Loop until you release the button. That way, you can press the button really fast to make the HoldVar increase or decrease.

    Dwayne
    Last edited by Dwayne; - 16th June 2005 at 16:22.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  5. #5
    Tear's Avatar
    Tear Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Dwayne,

    This information has solved my program perfectly! I am more than happy to extend that handshake next time we meet!

Similar Threads

  1. Saving variable(s) contents before power is removed
    By emavil in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th July 2007, 17:26
  2. 16F to 18F context saving differences
    By brittons in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st April 2007, 03:40
  3. Variables not appearing in Watch Window MPLabv7.3
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th June 2006, 14:36
  4. Problems with variables
    By egberttheone in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th December 2004, 18:37
  5. saving program variables
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 2nd November 2004, 22:25

Members who have read this thread : 1

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