I have a piece of working code using an Interrupt, whereby each time the button is pushed a variable is increased. This routine is going to be the first when the PIC is powered up and the rest of the code, relies on what the variable is set at.
I have also included an LED routine using PWM to visually indicate which stage the Variable is currently at (Variable is between 1 and 9).
How can i add a routine, where if the button is not pushed for 5 seconds, the code continues to the main routines etc. However, during this initial stage, if the button is pushed, the variable counts up (as already catered for) and 5 seconds starts all over again.
I'm sure it has to be added using a WHILE routine within the Interrupt Handlet routine, but i have tried to do it and failed.
Can anyone help please.
Here is the code:
If anyone can also suggest a better way of visually representing at which stage the variable is set at (1-9) using 3 LEDs, then please do.Code:' I/O definition ' TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS ' Variable Definition ' Switch VAR PORTB.0 Red VAR PORTB.1 Green VAR PORTB.2 Blue VAR PORTB.3 PushHowManyTimes VAR Byte PWMVariable VAR Byte ' PWM Variable 1-9 PWMVariableTimeOut VAR Byte ' 5 second Timeout (100 loops of 50mS) storedloop VAR Byte onled VAR Byte ' register and interrupt definition ' CMCON = 7 ' Disable analog comparator on PORTA... not needed as now OPTION_REG=0 ' enable pull-up resistor on PORTb ' Interrupt on falling edge of RB0 INTCON = %10010000 ' Enable RB0 interrupt On Interrupt Goto ProcedureSwitcher ' Variable initialisation ' 'PORTB = 0 ' Reset all outputs on PORTB PushHowManyTimes = 0 'Main Procedure ' MainProcedure: Select Case PushHowManyTimes case 1 Gosub resetleds PWMVariable = 1 PWM BLue,1,100 Case 2 Gosub resetleds PWMVariable = 2 PWM blue,25,100 Case 3 Gosub resetleds PWMVariable = 3 PWM blue,255,100 Case 4 Gosub resetleds PWMVariable = 4 PWM green,1,100 Case 5 Gosub resetleds PWMVariable = 5 PWM green,25,100 Case 6 Gosub resetleds PWMVariable = 6 PWM green,255,100 Case 7 Gosub resetleds PWMVariable = 7 PWM red,1,100 Case 8 Gosub resetleds PWMVariable = 8 PWM red,25,100 Case 9 Gosub resetleds PWMVariable = 9 PWM red,255,100 End Select Goto Mainprocedure ResetLEDs: Low PortB.1 ' reset output to PORTB Low PortB.2 Low PortB.3 Low PortB.4 Low PortB.5 Return ' Interrupt handler stuff here ' Disable ' Disable interrupts in handler ProcedureSwitcher: PushHowManytimes = PushHowManytimes + 1 ' Changing task If PushHowManytimes = 10 Then PushHowManytimes=1 Here: While SWITCH = 0 ' waiting until wend ' push-button is release pause 100 ' debounce time If switch = 0 then here INTCON.1=0 ' reset RB0 interrupt flag Resume ' Return to main program Enable ' Enable interrupts after ' handler
Many thanks,
Steve




Bookmarks