;Software License Agreement                                         
;                                                                    
;The software supplied herewith by Microchip Technology             
;Incorporated (the "Company") is intended and supplied to you, the  
;Company’s customer, for use solely and exclusively on Microchip    
;products. The software is owned by the Company and/or its supplier,
;and is protected under applicable copyright laws. All rights are   
;reserved. Any use in violation of the foregoing restrictions may   
;subject the user to criminal sanctions under applicable laws, as   
;well as to civil liability for the breach of the terms and         
;conditions of this license.                                        
;                                                                    
;THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,  
;WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED  
;TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       
;PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,  
;IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR         
;CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.   
;**********************************************************************
;
;	Filename:		Bipolar16F684.asm
;	Date:			3/23/04
;	File Version:	2.00
;
;	Author:			Reston Condit
;	Company:		Microchip Technology Inc.
;
;
;**********************************************************************
;
;	Files required:
;   	bipolar.inc
;		p16F684.inc
;
;**********************************************************************
;
;   Description:
;		
;	This file implements a bipolar stepping motor microstepping 
;	algorithm for the PIC16F684.  Details about this algorithm are 
;	found in AN906.
;
;**********************************************************************

	#include <p16F684.inc>	; processor specific variable definitions
	#include bipolar.inc	; include variable definitions and defines

	errorlevel  -302		; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;******************************************************************************
; Initialize
;	Initialize all special function registers and variables.
;
;******************************************************************************
Initialize
	bsf		STATUS,RP0		; Bank 1
	bsf		OSCCON,4		; Internal oscillator frequeny = 8 MHz
	movlw	B'00001111'		; inputs: RA0,RA1,RA2,RA3
	movwf	TRISA
	movlw	B'00000011'		; inputs: RC0,RC1
	movwf	TRISC
	movlw	B'00000000'		; Disable weak pullups
	movwf	WPUA
	movlw	B'00100000'		; ADC Fosc/32
	movwf	ADCON1
	movlw	B'00000100'		; analog pin: AN2
	movwf	ANSEL
	movlw	0x3F			; TMR2 period = 0xFF
	movwf	PR2	
	movlw	B'00001000'		; Interrupt on pin change enabled for pin RA3
	movwf	IOCA
	bcf		STATUS,RP0		; Bank 0	
	
	movlw	0x04			; Turn on comparators	
	movwf	CMCON0
	movlw	B'00001001'		; AN2 selected, A/D module turned on
	movwf	ADCON0
	movlw	B'01111100'		; TMR2 on; 1:1 prescaler; 1:16 postscaler
	movwf	T2CON
	movlw	B'00001000'		; RAIE interrupt
	movwf	INTCON	

	clrf	Mode			; Start in Mode 1

ModeSelect
	bcf		State,0			; initialize motor state
	clrf	Index			; initialize duty cycle lookup index
	movlw	0x01	
	movwf	Delay			; initialize delay

	movlw	high ModeSelectTable  ; jump to the current mode
	movwf	PCLATH
	movf	Mode,w
	andlw	0x07
	addlw	low ModeSelectTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL
	
ModeSelectTable
	goto	Mode1		; motor off
	goto	Mode2		; single-step mode
	goto	Mode3		; half-step mode
	goto	Mode4		; microstep mode
	goto	Mode5		; position control mode
	nop					
	nop		
	nop	
	clrf	Mode
	goto	Mode1		; start back at mode 1

;******************************************************************************
; Mode1 - Motor off
;	Wait here with the motor off until switch 1 is pressed.
;
;******************************************************************************
Mode1
	btfss	SW1			; if button is pressed the goto debounce routine
	goto	Debounce
	goto	Mode1

;******************************************************************************
; Mode2 - Single Step Mode
;	Single step the motor.
;
;******************************************************************************
Mode2
	clrf	CCP1CON		; Stop ECCP module
	clrf	CCPR1L
	bsf		STATUS,RP0	; Bank 1
	movlw	0x33		; Disable P1A and P1B
	movwf	TRISC
	movlw	B'00000110'	; 1:128 prescaler for TMR0, enable weak pullups
	movwf	OPTION_REG
	bcf		STATUS,RP0	; Bank 0	

Mode2Start
	btfss	SW1				; switch 1 pressed?
	goto	Debounce		;  yes, then goto debounce routine and next mode
	btfss	INTCON,T0IF		; timer 0 interrupt?
	goto	Mode2StateMachine
	bcf		INTCON,T0IF		; yes, then clear interrupt flag
	decfsz	Delay,f			; if delay is zero then move pot value into delay
	goto	Mode2StateMachine
	swapf	ADRESH,w		; move pot value into Delay
	andlw	0x0F
	movwf	Delay
	incf	Delay,f
	bsf		ADCON0,GO		; enable another analog read
	incf	State,f			; increment motor state

Mode2StateMachine
	movlw	high Mode2JumpTable
	movwf	PCLATH
	movf	State,w
	andlw	0x03
	addlw	low Mode2JumpTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL
	
Mode2JumpTable
	goto	Mode2State0
	goto	Mode2State1
	goto	Mode2State2
	goto	Mode2State3

Mode2State0				; Winding A -> (current flow direction)
	bcf		CTRLB1
	bcf		CTRLA2
	bcf		CTRLB2
	bsf	    CTRLA1
	goto	Mode2Start	

Mode2State1				; Winding B ->
	bcf		CTRLA1
	bcf		CTRLB2
	bcf		CTRLA2
	bsf	    CTRLB1
	goto	Mode2Start

Mode2State2				; Winding A <-
	bcf		CTRLB1
	bcf		CTRLA1
	bcf		CTRLB2
	bsf	    CTRLA2
	goto	Mode2Start

Mode2State3				; Winding B <-
	bcf		CTRLA1
	bcf		CTRLB1
	bcf		CTRLA2
	bsf	    CTRLB2
	goto	Mode2Start	

;******************************************************************************
; Mode3 - Half-Step Mode
;	Half step the motor.
;
;******************************************************************************
Mode3
	clrf	CCP1CON		; Stop ECCP module
	clrf	CCPR1L
	bsf		STATUS,RP0	; Bank 1
	movlw	0x33		; Disable P1A and P1B
	movwf	TRISC
	movlw	B'00000101'	; 1:64 prescaler for TMR0, enable weak pullups
	movwf	OPTION_REG
	bcf		STATUS,RP0	; Bank 0	

Mode3Start
	btfss	SW1			; switch 1 pressed?
	goto	Debounce	;  yes, then goto bebounce routine and onto next mode
	btfss	INTCON,T0IF		; timer 0 interrupt?
	goto	Mode3StateMachine
	bcf		INTCON,T0IF		;  yes, then clear interrupt flag
	decfsz	Delay,f			; if delay is zero reload delay with ADC value
	goto	Mode3StateMachine
	swapf	ADRESH,w		; move pot value into Delay
	andlw	0x0F
	movwf	Delay
	incf	Delay,f
	bsf		ADCON0,GO		; enable another analog read
	incf	State,f			; increment motor state

Mode3StateMachine
	movlw	high Mode3JumpTable
	movwf	PCLATH
	movf	State,w
	andlw	0x07
	addlw	low Mode3JumpTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL
	
Mode3JumpTable
	goto	Mode3State0
	goto	Mode3State1
	goto	Mode3State2
	goto	Mode3State3
	goto	Mode3State4
	goto	Mode3State5
	goto	Mode3State6
	goto	Mode3State7	

Mode3State0				; Winding A -> (current flow direction)
	bcf		CTRLB1
	bcf		CTRLA2
	bcf		CTRLB2
	bsf	    CTRLA1
	goto	Mode3Start	

Mode3State1				; Winding A ->, Winding B ->
	bcf		CTRLA2
	bcf		CTRLB2
	bsf	    CTRLA1
	bsf	    CTRLB1
	goto	Mode3Start

Mode3State2				; Winding B ->
	bcf		CTRLA1
	bcf		CTRLB2
	bcf		CTRLA2
	bsf	    CTRLB1
	goto	Mode3Start

Mode3State3				; Winding A <-, Winding B ->
	bcf		CTRLA1
	bcf		CTRLB2
	bsf	    CTRLB1
	bsf	    CTRLA2
	goto	Mode3Start

Mode3State4				; Winding A <-
	bcf		CTRLB1
	bcf		CTRLA1
	bcf		CTRLB2
	bsf	    CTRLA2
	goto	Mode3Start

Mode3State5				; Winding A <-, Winding B <-
	bcf		CTRLB1
	bcf		CTRLA1
	bsf	    CTRLA2
	bsf	    CTRLB2
	goto	Mode3Start

Mode3State6				; Winding B <-
	bcf		CTRLA1
	bcf		CTRLB1
	bcf		CTRLA2
	bsf	    CTRLB2
	goto	Mode3Start

Mode3State7				; Winding A ->, Winding B <-
	bcf		CTRLB1
	bcf		CTRLA2
	bsf	    CTRLB2
	bsf	    CTRLA1
	goto	Mode3Start	

;******************************************************************************
; Mode4 - High Torque Microstepping Mode
;	High Torque Microstepping is achieved by pulse width modulating one winding 
;	using the ECCP module.  The duty cycle of the module is varied according to 
;	a cosine lookup table.
;
;******************************************************************************
Mode4
	bsf		STATUS,RP0	; Bank 1
	movlw	B'00000010'	; 1:8 prescaler for TMR0, enable weak pullups
	movwf	OPTION_REG
	bcf		STATUS,RP0	; Bank 0
	movlw	B'10001101'	; Half-bridge,PWM mode
	movwf	CCP1CON

Mode4Start
	btfss	SW1				; switch 1 pressed?
	goto	Debounce		;  yes, then goto debounce routine and onto next mode
	btfsc	INTCON,T0IF		; Timer 0 interrupt flay set?
	call	Mode4IncrementState	;  yes, then goto state advance routine

Mode4StateMachine
	movlw	high Mode4JumpTable
	movwf	PCLATH
	movf	State,w
	andlw	0x07
	addlw	low Mode4JumpTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL
	
Mode4JumpTable
	goto	Mode4State0
	goto	Mode4State1
	goto	Mode4State2
	goto	Mode4State3
	goto	Mode4State4			
	goto	Mode4State5
	goto	Mode4State6
	goto	Mode4State7
	
Mode4State0				; Winding A _-_->, Winding B -> (current direction and winding modulation)
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB2
	bsf		CTRLA1
	bsf	    CTRLB1
	goto	Mode4Start

Mode4State1				; Winding A <-_-_, Winding B ->
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB2
	bsf		CTRLA2
	bsf	    CTRLB1
	goto	Mode4Start
	
Mode4State2				; Winding A <-, Winding B _-_->
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB2
	bsf	    CTRLA2
	bsf		CTRLB1
	goto	Mode4Start
	
Mode4State3				; Winding A <-, Winding B <-_-_
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB1
	bsf	    CTRLA2
	bsf		CTRLB2
	goto	Mode4Start
	
Mode4State4				; Winding A <-_-_, Windng B <-
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB1
	bsf		CTRLA2
	bsf	    CTRLB2
	goto	Mode4Start

Mode4State5				; Winding A _-_->, Winding B <-
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB1
	bsf		CTRLA1
	bsf	    CTRLB2
	goto	Mode4Start

Mode4State6				; Winding A ->, Winding B <-_-_
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB1
	bsf	    CTRLA1
	bsf		CTRLB2
	goto	Mode4Start
	
Mode4State7				; Winding A ->, Winding B _-_->
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB2
	bsf	    CTRLA1
	bsf		CTRLB1
	goto	Mode4Start

;******************************************************************************
; Mode5 - Position control with the potentiomenter
;	Microstep to a position indicated by the potentiometer.
;
;******************************************************************************
Mode5
	bsf		STATUS,RP0		; Bank 1
	movlw	B'00000001'		; 1:4 prescaler for TMR0, enable weak pullups
	movwf	OPTION_REG
	bcf		STATUS,RP0		; Bank 0
	movlw	B'10001101'		; Half-bridge,PWM mode
	movwf	CCP1CON
	movf	ADRESH,w
	movwf	PositionMotor

Mode5Start
	btfss	SW1				; if switch is pressed goto debounce routine
	goto	Debounce
	movf	ADRESH,w
	bsf		ADCON0,GO		; enable another analog read
	movwf	PositionPot
	subwf	PositionMotor,w	; PositionMotor - PositionPot
	movwf	CounterH
	btfsc	STATUS,Z		; if PostionMotor = PositionPot
	goto	Mode5Start		;  then do nothing
	btfss	STATUS,C		; if PositionMotor > PositionPot
	goto	Next		
	movf	CounterH,w		;  then reverse movement might be needed
	sublw	0x0A			; filter: is difference in position greater than 10?
	btfsc	STATUS,C	
	goto	Mode5Start		;  no, then do nothing
	bcf		FDirection		;  yes, go into reverse mode
	goto	Mode5Start2		; go make the motor move
Next					; PostionMotor < PositionPot
	comf	CounterH,w
	sublw	0x0A			; filter: is difference in position greater than 10?
	btfsc	STATUS,C	
	goto	Mode5Start		;  no, then do nothing  
	bsf		FDirection		;  yes, go into forward mode
	goto	Mode5Start2		; go make the motor move

Mode5Start2
	btfss	SW1				; if switch is pressed goto debounce routine
	goto	Debounce
	btfsc	INTCON,T0IF
	goto	ChangeMode5State

Mode5StateMachine
	movlw	high Mode5JumpTable
	movwf	PCLATH
	movf	State,w
	andlw	0x07
	addlw	low Mode5JumpTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL
	
Mode5JumpTable
	goto	Mode5State0
	goto	Mode5State1
	goto	Mode5State2
	goto	Mode5State3
	goto	Mode5State4			
	goto	Mode5State5
	goto	Mode5State6
	goto	Mode5State7
	
Mode5State0
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB2
	bsf		CTRLA1
	bsf	    CTRLB1
	goto	Mode5Start2

Mode5State1
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB2
	bsf		CTRLA2
	bsf	    CTRLB1
	goto	Mode5Start2
	
Mode5State2
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB2
	bsf	    CTRLA2
	bsf		CTRLB1
	goto	Mode5Start2
	
Mode5State3
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB1
	bsf	    CTRLA2
	bsf		CTRLB2
	goto	Mode5Start2
	
Mode5State4
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA1
	bcf		CTRLB1
	bsf		CTRLA2
	bsf	    CTRLB2
	goto	Mode5Start2

Mode5State5
	bsf		STATUS,RP0	; Bank 1
	movlw	0x13		; Disable P1B, Enable P1A
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB1
	bsf		CTRLA1
	bsf	    CTRLB2
	goto	Mode5Start2

Mode5State6
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB1
	bsf	    CTRLA1
	bsf		CTRLB2
	goto	Mode5Start2
	
Mode5State7
	bsf		STATUS,RP0	; Bank 1
	movlw	0x23		; Disable P1A, Enable P1B
	movwf	TRISC
	bcf		STATUS,RP0	; Bank 0
	bcf		CTRLA2
	bcf		CTRLB2
	bsf	    CTRLA1
	bsf		CTRLB1
	goto	Mode5Start2

;******************************************************************************
; Mode4IncrementState
;	Determine if microstepping state should be incremented for Mode 4.
;
;******************************************************************************
Mode4IncrementState
	bcf		INTCON,T0IF		; clear interrupt flag
	decfsz	Delay,f			; is Delay 0?
	return					;  no, then return
	swapf	ADRESH,w		;  yes, reload with high 4-bits of ADC value
	andlw	0x0F
	movwf	Delay
	incf	Delay,f			; increment to make sure it is not-zero
	bsf		ADCON0,GO		; enable another analog read
	incf	Index,f			; increment duty cycle lookup register
	call	DutyCycleLookup	; look up duty cycle value and move
	movwf	CounterL		; move duty cycle into CCP1CON and CCPR1L
	swapf	CounterL,w
	andlw	0x30
	iorlw	0x8D			; keep integrity of CCP1CON, half bridge mode
	movwf	CCP1CON			;  P1A active high, P1B active low
	rrf		CounterL,w
	movwf	CounterH
	rrf		CounterH,w
	andlw	0x30	
	movwf	CCPR1L		
	movf	Index,w			; is it time to change stepper state? 
	andlw	0x07
	btfss	STATUS,Z	
	return					;  no, then return
	incf	State,f			;  yes, then increment state
	return

;******************************************************************************
; ChangeMode5State
;	Increment or decrement motor state so that motor position corresponds to
;	potentiometer position
;******************************************************************************
ChangeMode5State
	bcf		INTCON,T0IF		; clear interrupt flag
	btfss	FDirection		; what direction does the motor need to turn?
	goto	Reverse			;  reverse, then goto reverse routine

Forward	; move motor clockwise
	incf	Index,f			; increment duty cycle lookup register
	call	DutyCycleLookup	; look up duty cycle value and move
	movwf	CounterL		;  it into CCP1CON and CCPR1L
	swapf	CounterL,w
	andlw	0x30
	iorlw	0x8D			; keep integrity of CCP1CON, half bridge mode
	movwf	CCP1CON			;  P1A active high, P1B active low
	rrf		CounterL,w
	movwf	CounterH
	rrf		CounterH,w
	andlw	0x30	
	movwf	CCPR1L		
	movf	Index,w			; is it time to change stepper state? 
	andlw	0x07
	btfss	STATUS,Z	
	goto	Mode5StateMachine ; no, then just go back to state machine
	incf	State,f			; yes, then increment state
	movf	Index,w			; are we at the beginning of the duty cycle lookup table?
	andlw	0x0F
	btfss	STATUS,Z
	goto	Mode5StateMachine ; no, then just go back to state machine
	incf	PositionMotor,f	; yes, then increment motor position and goto	
	goto	Mode5Start		;  position compare routine

Reverse	; move motor counter-clockwise
	movf	Index,w			; is it time to change stepper state? 
	andlw	0x07
	btfsc	STATUS,Z
	decf	State,f			;  yes, then decrement state
	incf	Index,f			; increment duty cycle lookup register
	call	DutyCycleLookup	; look up duty cycle value and move
	movwf	CounterL		;  into CCP1CON and CCPR1L
	swapf	CounterL,w
	andlw	0x30
	iorlw	0x8D			; keep integrity of CCP1CON, half bridge mode
	movwf	CCP1CON			;  P1A active high, P1B active low
	rrf		CounterL,w
	movwf	CounterH
	rrf		CounterH,w
	andlw	0x30	
	movwf	CCPR1L			; are we at the beginning of the duty cycle lookup table?
	movf	Index,w
	andlw	0x0F
	btfss	STATUS,Z
	goto	Mode5StateMachine ; no, then just go back to state machine
	decf	PositionMotor,f	; yes, then decrement motor position and goto		
	goto	Mode5Start		;  position compare routine

;******************************************************************************
; DutyCycleLookup
;	Contains a table of Cosine values. 
;
;******************************************************************************
DutyCycleLookup
	movlw	high LookupTable
	movwf	PCLATH
	movf	Index,w
	andlw	0x0F
	addlw	low LookupTable
	btfsc	STATUS,C	
	incf	PCLATH,f
	movwf	PCL

LookupTable
	retlw	.251
	retlw	.238
	retlw	.217
	retlw	.188
	retlw	.154
	retlw	.114
	retlw	.70
	retlw	.24
	retlw	.24
	retlw	.70
	retlw	.114
	retlw	.154
	retlw	.188
	retlw	.217
	retlw	.238
	retlw	.251

;******************************************************************************
; Delay10ms
;	10ms delay loop.
;
;******************************************************************************
Delay10ms
	clrf	CounterL
	movlw	30
	movwf	CounterH
Delay10msLoop
	decfsz	CounterL,f
	goto	Delay10msLoop
	decfsz	CounterH,f
	goto	Delay10msLoop
	return

;******************************************************************************
; Debounce
;	Debounce button press and turn off motor.
;
;******************************************************************************
Debounce
	clrf	CCPR1L			; clear duty cycle
	clrf	CCP1CON
	clrf	PORTA			; turn of all control pins
	clrf	PORTC
	incf	Mode,f			; next mode please
Debounce1					; wait for SW1 to be unpressed for 10ms
	btfss	SW1				; wait here until button is released
	goto	Debounce1
	bcf		INTCON,RAIF		; Once release make sure it doesn't change
	call	Delay10ms		;  for 10ms before moving to next state
	btfsc	INTCON,RAIF
	goto	Debounce1
	goto	ModeSelect		; goto mode select state machine

	END                       ; directive 'end of program'


