PDA

View Full Version : power-up question



ERM
01-25-2006, 02:31 PM
Hi Guys,
Can anyone fill me in on how to make my BS2 resume its action on power-up after the power was removed.

Explained:
I have a series of loops executing different flashing sequences of led's. The push of a button advances the program to the next loop.

Example: If I am in the fourth loop and remove the power and leave it off, when I reapply the power, it starts with loop one. How do I make it start with the last loop it was in (in this case loop four)?

Thanks in advance,
Tony

Bruce Bates
01-25-2006, 06:02 PM
Tony -

It might be a bit complex to do EXACTLY what you're asking to do, but coming close is fairly easy. The difference is in the particular iteration of a given loop, and exactly where you restart.

What you need to do is take your own checkpoints. You do this by maintaining a variable which will indicate the next routine to be executed, BUT this variable MUST also written to EEPROM, so it is safe and secure in the event of a power failure.

Here is a simple example, just using a couple of routines. We will presume that the contents of EEPROM is initially zero, which may or may not be initially true. You will need to ensure that it is at some known initial value to start. This routine has not bee tested or even run through the PBASIC Compiler.

'Pin Port Usage - None

'Variables -

RTN VAR BYTE 'Number of next routine
Iter VAR BYTE 'Loop iteration

'Constants - None

'EEPROM Usage
' Byte 0 - Address of next routine to execute

Start:

'Fetch proper value for next routine to execute on first excursion
'Default value presumed to be 0

READ 0, Rtn

Commutator:
'All routines which continue, return here

Branch Rtn, (RTN1, RTN2, RTN3, Exit_RTN) 'Locate and branch to the proper routine

'Error if we reach this point
DEBUG "Invalid Routine Number"
STOP

RTN1:

For Iter = 1 to 10
'Do something
NEXT

rtn = rtn + 1
WRITE 0, Rtn 'Set next checkpoint
GOTO Commutator

RTN2:

For Iter = 1 to 50
'Do something
NEXT

rtn = rtn + 1
WRITE 0, Rtn 'Set next checkpoint
GOTO Commutator

RTN3:

For Iter = 1 to 100
'Do something
NEXT

rtn = rtn + 1
WRITE 0, Rtn 'Set next checkpoint
GOTO Commutator

Exit_RTN:

DEBUG "Finished"
WRITE 0, 0 'Reset checkpoint reference to default
END

To get even closer to your desired result, you would need to write the value of "Iter" to EEPROM on each iteration of each loop, and restore it upon restart. The reason why this becomes a potential problem, without a bit more sophistication to the program, is that EEPROM only has a finite life. If you write to it too many times, you can wear it out.

Regards,

Bruce Bates