
;*******************************
;;Joy Stick.asm
;drives 2 servo motors forward and reverse
;10-11-2010 
;*******************************


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 registers

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 

;******************************
; variables - names and files
;*****************************

temp1		equ 20h	;
temp2		equ 21h	;

Sw_FlagT	equ	25h	;switch flag top
Sw_FlagB	equ	26h	;switch flag bottom
count		equ	27h	;loops of discharge time for 100n
PotValue	equ 28h ;value of pot


;***************************
;Equates
;***************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			

status		equ	03h
option_reg	equ 81h


		; bits on GPIO
				
pin7		equ	0	;GP0  left servo motor
pin6	 	equ	1	;GP1  right servo motor
pin5		equ	2	;GP2  100k speed pot 
pin4		equ	3	;GP3  Input ONLY - not used
pin3		equ	4	;GP4  input from 2 top buttons
pin2		equ	5	;GP5  input from 2 bottom buttons   
 

		;bits
				
rp0			equ	5			;bit 5 of the status register

;**********************
;Beginning of program
;**********************
		org		0x00
		nop
		nop
		nop
		nop
		nop			
SetUp	bsf		status, rp0 	;Bank 1			
       	movlw	b'11101000'		;Set TRIS  GP0,1,4 out 
		movwf	TRISIO	   	    ;		
		bcf		status, rp0		;bank 0
		movlw   07h         	;turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0 
        clrf 	GPIO       		;Clear GPIO of junk			
		goto 	Main	
			


;****************
;delays			*
;****************

_uS		movlw	08Ch
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00
		
		
		;delay for pulsing servo anticlockwise
		
acw
		movlw	60h
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00
		
	
_1mS	nop
		decfsz 	temp1,f
		goto 	_1mS
		retlw 	00
		
			

_10mS	movlw	0Ah
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00
		
_15mS	movlw	.15
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00	
		
		
_18mS	movlw	.18
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00			
			
		
		;delay to create value for pot for servo speed
		
PotDel	movlw	0A0h
		movwf	temp1
		decfsz 	temp1,f
		goto 	$-1
		retlw 	00		
	
			
;***************************
; Sub Routines 		       *
;***************************
	
	
		;position of pot creates a value in PotValue
		
Pot		bsf		status,rp0			
		bcf		trisio,2		;Make GP2 output
		bcf		status,rp0
		bcf		gpio,2			;make GP2 LOW	
		call	_1mS			;create delay to discharge 100n
		bsf		status,rp0			
		bsf		trisio,2		;Make GP2 input
		bcf		status,rp0	
		clrf	PotValue
		call	PotDel				
		incf	PotValue,f		
		btfss	gpio,2			;is input HIGH?				
		goto	$-3	
		retlw	00				;returns with a valus in PotValue
				
	;SwTop sets bit 1,2,3,4 in Sw_FlagT file		
		
SwTop	clrf	Sw_FlagT			
		clrf	count
		bsf		status,rp0			
		bcf		trisio,4		;Make GP4 output
		bcf		status,rp0
		bcf		gpio,4			;make GP4 LOW	
		call	_1mS			;create delay to discharge 100n
		bsf		status,rp0			
		bsf		trisio,4		;Make GP4 input
		bcf		status,rp0							
		call	_uS			
		incf	count,f	
		movlw	.10				;create 10 loops to definitely return
		xorwf	count,w
		btfsc	status,z
		goto	$+3				
		btfss	gpio,4			;is input HIGH?				
		goto	$-7				;count exits with 1-4			
		decfsz	count,f			
		goto	$+3
		bsf		Sw_FlagT,1		;both top buttons pressed
		retlw	00
		decfsz	count,f
		goto	$+3
		bsf		Sw_FlagT,2		;left top button pressed
		retlw	00
		decfsz	count,f
		goto	$+3
		bsf		Sw_FlagT,3		;right top button pressed
		retlw	00
		bsf		Sw_FlagT,4		;no buttons pressed
		retlw	00				
	
		;SwBott sets bit 1,2,3,4 in Sw_FlagB file		
		
SwBott	clrf	Sw_FlagB			
		clrf	count
		bsf		status,rp0			
		bcf		trisio,5		;Make GP5 output
		bcf		status,rp0
		bcf		gpio,5			;make GP5 LOW	
		call	_1mS			;create delay to discharge 100n
		bsf		status,rp0			
		bsf		trisio,5		;Make GP5 input
		bcf		status,rp0							
		call	_uS						
		incf	count,f	
		movlw	.10			;create 10 loops to definitely return
		xorwf	count,w
		btfsc	status,z		
		goto	$+3	
		btfss	gpio,5			;is input HIGH?				
		goto	$-7				;count exits with 1-4		
		decfsz	count,f			
		goto	$+3
		bsf		Sw_FlagB,1		;both lower buttons pressed
		retlw	00
		decfsz	count,f
		goto	$+3
		bsf		Sw_FlagB,2		;left lower button pressed
		retlw	00
		decfsz	count,f
		goto	$+3
		bsf		Sw_FlagB,3		;right lower button pressed
		retlw	00
		bsf		Sw_FlagB,4		;no buttons pressed
		retlw	00			
						
											
;****************************************************************
;* Main 							*
;****************************************************************

Main	call	SwBott		;to detect 2 lower buttons
		btfsc	Sw_FlagB,4  ;bit 4 set if no buttons pressed
		goto	_b			;go to detect top buttons	
		call	Pot			;determine servo speed		
		btfsc	Sw_FlagB,2	;left lower button pressed
		goto	$+9
		btfss	Sw_FlagB,3	;right lower button pressed
		goto	Main     			
		bsf		gpio,0		;creates clockwise motion
		call	_1mS		;for clockwise pulse
		call	_1mS		;for clockwise pulse
		bcf		gpio,0
		call	_18mS		
		goto	$+5				
		bsf		gpio,0		;creates anticlockwise motion
		call	acw			;anticlockwise pulse					
		bcf		gpio,0	
		call	_18mS		
		decfsz	PotValue,f
		goto	$-2
		goto	Main
		
	
_b		call	SwTop		;to detect 2 top buttons
		btfsc	Sw_FlagT,4  ;bit 4 set if no buttons pressed
		goto	Main		;
		call	Pot			;determine servo speed			
		btfsc	Sw_FlagT,2	;left top button pressed
		goto	$+9
		btfss	Sw_FlagT,3	;right top button pressed
		goto	_b			
		bsf		gpio,1		;creates clockwise motion
		call	_1mS		;for clockwise pulse
		call	_1mS		;for clockwise pulse
		bcf		gpio,1
		call	_18mS		
		goto	$+5				
		bsf		gpio,1		;creates anticlockwise motion
		call	acw			;anticlockwise pulse					
		bcf		gpio,1	
		call	_18mS		
		decfsz	PotValue,f
		goto	$-2			
		goto	Main
								
		END
		
		
		