Hi,
There are several possible ways depending on what it is your doing on the various "programs", here's one aproach.
Set up one of the PIC's timers, say TMR0 as a counter, have your button, with some debounce circuitry connected to the external input of the timer.
Then you have a main loop which checks the value of the TMR0 register and jumps to appropriate sub program, when the value of TMR0 is lager than the "last" sub-program you reset the TMR0 register to 0.
Something like, for a 16F628:
Code:
TRISA.4 = 1
OPTION.5 = 1 'Set TMR0 as Counter, input is on RA4.
OPTION.4 = 0 'Count on rising edge.
TMR0 = 0 'Reset TMR0 register
Main:
Select Case TMR0
Case 0
Goto ProgramZero
Case 1
Goto ProgramOne
Case 2
Goto ProgramThree
Case Else
TMR0 = 0
Goto Main
ProgramZero:
'Do This
Goto Main
ProgramOne:
'Do That
Goto Main
ProgramThree:
'Do Something else
Goto Main
Another aproach is to wire the switch to an external interrupt pin and use the interrupt request flag to increment a software pointer quite similar to the TMR0 counter above. On the 16F628 PortB.0 is the interrupt pin
Code:
TRISB.0 = 1 'PortB.0 as input.
OPTION.6 = 1 'Set interrupt flag on rising edge of PortB.0
Main:
If INTCON.1 = 1 then
INTCON.1 = 0 'Reset flag
myPointer = myPointer + 1
If myPointer = 3 then myPointer = 0 'Wrap around
Select Case myPointer
and then like in the first example...
If you want "instant" switching of programs then you'll need to actually enbale the interrupt as well, not only check the flag, and then do the "program switching" in the interrupt handler.
Well, there's a couple of ideas for you.
/Henrik.
Bookmarks