;for 4MHz crystal, internal would be 1MHz with 1us each instruction

;written in MPASM MICROCHIP assembler
;****set up constans and variables****
	INCLUDE "P16F877.INC" 			;look up file for MPASM
	LIST	P=16F877
	ERRORLEVEL      -302    		;SUPPRESS BANK SELECTION MESSAGES
	__config  _CP_OFF & _HS_OSC & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _BODEN_OFF
						;RC oscillator, power on timer 
						;and watchdog timer off

posi	equ	h'20'		;temporary storage after reading input
serv	equ	h'21'

serv0	equ	h'22'		;temporary storage for each servo last position
serv1	equ	h'23'
serv2	equ	h'24'
serv3	equ	h'25'
serv4	equ	h'26'
serv5	equ	h'27'
serv6	equ	h'28'
serv7	equ	h'29'
serv8	equ	h'2a'
serv9	equ	h'2b'

count1	equ	h'2e'		;general purpose register
count2	equ	h'2f'		;general purpose register

r1	equ	h'30'		;registers used to store pulse length
r2	equ	h'31'
r3	equ	h'32'
r4	equ	h'33'

temp	equ	h'34'

;****set up ports****
	bsf	STATUS,RP0	;switch to bank1 to set up ports
	movlw	h'00'
	movwf	TRISB		;portB is all output, servo 1 to 8
	movwf	TRISD		;portD is all output, servo 9 and 10
	movwf	TRISE		;portE is all output
	movlw	0x06 		; Configure all pins
	movwf	ADCON1 		; as digital inputs
	movlw	h'ff'
	movwf	TRISA		;portA is all input, for servo position
	movwf	TRISC		;portC is all input, to select servo
	bcf	STATUS,RP0	;switch back to bank0 to use ports

;********************************************************************
;******main program**************************************************
init	movlw	d'12'	;15 is mid position, max is 31, min is 0
	movwf	serv1	;angle is control by 5bit input
	movlw	d'13'
	movwf	serv3	;so the 180 degree is divied into 32 smaller angles
	movwf	serv7
	movwf	serv9	;set servos to mid pos
	movlw	d'14'
	movwf	serv4
	movwf	serv8
	movlw	d'15'
	movwf	serv0
	movwf	serv5
	movlw	d'16'
	movwf	serv2
	movlw	d'17'
	movwf	serv6

start	movlw	b'10000000'	;***********servo1******************
	movwf	PORTB		;2
	movlw	b'00000000'	;3
	movwf	PORTD		;4
	call	loads		;5	
	call	loads		;6	45+3=48 instructions
	call	delay1
	movf	serv0,W		;7
	movwf	temp		;8
	call	loadr		;9	129+9=138 instructions

	btfss	r1,7		;10
	bcf	PORTB,7
	call	delay2
	btfss	r1,6		;13
	bcf	PORTB,7
	call	delay2
	btfss	r1,5		;16
	bcf	PORTB,7
	call	delay2
	btfss	r1,4		;19
	bcf	PORTB,7
	call	delay2
	btfss	r1,3		;22
	bcf	PORTB,7
	call	delay2
	btfss	r1,2		;25
	bcf	PORTB,7
	call	delay2
	btfss	r1,1		;28
	bcf	PORTB,7
	call	delay2
	btfss	r1,0		;31
	bcf	PORTB,7
	call	delay2
	btfss	r2,7		;34
	bcf	PORTB,7
	call	delay2
	btfss	r2,6		;37
	bcf	PORTB,7
	call	delay2
	btfss	r2,5		;40
	bcf	PORTB,7
	call	delay2
	btfss	r2,4		;43
	bcf	PORTB,7
	call	delay2
	btfss	r2,3		;46
	bcf	PORTB,7
	call	delay2
	btfss	r2,2		;49
	bcf	PORTB,7
	call	delay2
	btfss	r2,1		;52
	bcf	PORTB,7
	call	delay2
	btfss	r2,0		;55
	bcf	PORTB,7
	call	delay2
	btfss	r3,7		;58
	bcf	PORTB,7
	call	delay2
	btfss	r3,6		;61
	bcf	PORTB,7
	call	delay2
	btfss	r3,5		;64
	bcf	PORTB,7
	call	delay2
	btfss	r3,4		;67
	bcf	PORTB,7
	call	delay2
	btfss	r3,3		;70
	bcf	PORTB,7
	call	delay2
	btfss	r3,2		;73
	bcf	PORTB,7
	call	delay2
	btfss	r3,1		;76
	bcf	PORTB,7
	call	delay2
	btfss	r3,0		;79
	bcf	PORTB,7
	call	delay2
	btfss	r4,7		;82
	bcf	PORTB,7
	call	delay2
	btfss	r4,6		;85
	bcf	PORTB,7
	call	delay2
	btfss	r4,5		;88
	bcf	PORTB,7
	call	delay2
	btfss	r4,4		;91
	bcf	PORTB,7
	call	delay2
	btfss	r4,3		;94
	bcf	PORTB,7
	call	delay2
	btfss	r4,2		;97
	bcf	PORTB,7
	call	delay2
	btfss	r4,1		;100
	bcf	PORTB,7
	call	delay2
	btfss	r4,0		;103
	bcf	PORTB,7
	call	delay2		;105
	
	movlw	b'01000000'	;***********servo2******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv1,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,6
	call	delay2
	btfss	r1,6
	bcf	PORTB,6
	call	delay2
	btfss	r1,5
	bcf	PORTB,6
	call	delay2
	btfss	r1,4
	bcf	PORTB,6
	call	delay2
	btfss	r1,3
	bcf	PORTB,6
	call	delay2
	btfss	r1,2
	bcf	PORTB,6
	call	delay2
	btfss	r1,1
	bcf	PORTB,6
	call	delay2
	btfss	r1,0
	bcf	PORTB,6
	call	delay2
	btfss	r2,7
	bcf	PORTB,6
	call	delay2
	btfss	r2,6
	bcf	PORTB,6
	call	delay2
	btfss	r2,5
	bcf	PORTB,6
	call	delay2
	btfss	r2,4
	bcf	PORTB,6
	call	delay2
	btfss	r2,3
	bcf	PORTB,6
	call	delay2
	btfss	r2,2
	bcf	PORTB,6
	call	delay2
	btfss	r2,1
	bcf	PORTB,6
	call	delay2
	btfss	r2,0
	bcf	PORTB,6
	call	delay2
	btfss	r3,7
	bcf	PORTB,6
	call	delay2
	btfss	r3,6
	bcf	PORTB,6
	call	delay2
	btfss	r3,5
	bcf	PORTB,6
	call	delay2
	btfss	r3,4
	bcf	PORTB,6
	call	delay2
	btfss	r3,3
	bcf	PORTB,6
	call	delay2
	btfss	r3,2
	bcf	PORTB,6
	call	delay2
	btfss	r3,1
	bcf	PORTB,6
	call	delay2
	btfss	r3,0
	bcf	PORTB,6
	call	delay2
	btfss	r4,7
	bcf	PORTB,6
	call	delay2
	btfss	r4,6
	bcf	PORTB,6
	call	delay2
	btfss	r4,5
	bcf	PORTB,6
	call	delay2
	btfss	r4,4
	bcf	PORTB,6
	call	delay2
	btfss	r4,3
	bcf	PORTB,6
	call	delay2
	btfss	r4,2
	bcf	PORTB,6
	call	delay2
	btfss	r4,1
	bcf	PORTB,6
	call	delay2
	btfss	r4,0
	bcf	PORTB,6
	call	delay2

	movlw	b'00100000'	;***********servo3******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv2,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,5
	call	delay2
	btfss	r1,6
	bcf	PORTB,5
	call	delay2
	btfss	r1,5
	bcf	PORTB,5
	call	delay2
	btfss	r1,4
	bcf	PORTB,5
	call	delay2
	btfss	r1,3
	bcf	PORTB,5
	call	delay2
	btfss	r1,2
	bcf	PORTB,5
	call	delay2
	btfss	r1,1
	bcf	PORTB,5
	call	delay2
	btfss	r1,0
	bcf	PORTB,5
	call	delay2
	btfss	r2,7
	bcf	PORTB,5
	call	delay2
	btfss	r2,6
	bcf	PORTB,5
	call	delay2
	btfss	r2,5
	bcf	PORTB,5
	call	delay2
	btfss	r2,4
	bcf	PORTB,5
	call	delay2
	btfss	r2,3
	bcf	PORTB,5
	call	delay2
	btfss	r2,2
	bcf	PORTB,5
	call	delay2
	btfss	r2,1
	bcf	PORTB,5
	call	delay2
	btfss	r2,0
	bcf	PORTB,5
	call	delay2
	btfss	r3,7
	bcf	PORTB,5
	call	delay2
	btfss	r3,6
	bcf	PORTB,5
	call	delay2
	btfss	r3,5
	bcf	PORTB,5
	call	delay2
	btfss	r3,4
	bcf	PORTB,5
	call	delay2
	btfss	r3,3
	bcf	PORTB,5
	call	delay2
	btfss	r3,2
	bcf	PORTB,5
	call	delay2
	btfss	r3,1
	bcf	PORTB,5
	call	delay2
	btfss	r3,0
	bcf	PORTB,5
	call	delay2
	btfss	r4,7
	bcf	PORTB,5
	call	delay2
	btfss	r4,6
	bcf	PORTB,5
	call	delay2
	btfss	r4,5
	bcf	PORTB,5
	call	delay2
	btfss	r4,4
	bcf	PORTB,5
	call	delay2
	btfss	r4,3
	bcf	PORTB,5
	call	delay2
	btfss	r4,2
	bcf	PORTB,5
	call	delay2
	btfss	r4,1
	bcf	PORTB,5
	call	delay2
	btfss	r4,0
	bcf	PORTB,5
	call	delay2

	movlw	b'00010000'	;***********servo4******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv3,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,4
	call	delay2
	btfss	r1,6
	bcf	PORTB,4
	call	delay2
	btfss	r1,5
	bcf	PORTB,4
	call	delay2
	btfss	r1,4
	bcf	PORTB,4
	call	delay2
	btfss	r1,3
	bcf	PORTB,4
	call	delay2
	btfss	r1,2
	bcf	PORTB,4
	call	delay2
	btfss	r1,1
	bcf	PORTB,4
	call	delay2
	btfss	r1,0
	bcf	PORTB,4
	call	delay2
	btfss	r2,7
	bcf	PORTB,4
	call	delay2
	btfss	r2,6
	bcf	PORTB,4
	call	delay2
	btfss	r2,5
	bcf	PORTB,4
	call	delay2
	btfss	r2,4
	bcf	PORTB,4
	call	delay2
	btfss	r2,3
	bcf	PORTB,4
	call	delay2
	btfss	r2,2
	bcf	PORTB,4
	call	delay2
	btfss	r2,1
	bcf	PORTB,4
	call	delay2
	btfss	r2,0
	bcf	PORTB,4
	call	delay2
	btfss	r3,7
	bcf	PORTB,4
	call	delay2
	btfss	r3,6
	bcf	PORTB,4
	call	delay2
	btfss	r3,5
	bcf	PORTB,4
	call	delay2
	btfss	r3,4
	bcf	PORTB,4
	call	delay2
	btfss	r3,3
	bcf	PORTB,4
	call	delay2
	btfss	r3,2
	bcf	PORTB,4
	call	delay2
	btfss	r3,1
	bcf	PORTB,4
	call	delay2
	btfss	r3,0
	bcf	PORTB,4
	call	delay2
	btfss	r4,7
	bcf	PORTB,4
	call	delay2
	btfss	r4,6
	bcf	PORTB,4
	call	delay2
	btfss	r4,5
	bcf	PORTB,4
	call	delay2
	btfss	r4,4
	bcf	PORTB,4
	call	delay2
	btfss	r4,3
	bcf	PORTB,4
	call	delay2
	btfss	r4,2
	bcf	PORTB,4
	call	delay2
	btfss	r4,1
	bcf	PORTB,4
	call	delay2
	btfss	r4,0
	bcf	PORTB,4
	call	delay2

	movlw	b'00001000'	;***********servo5******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv4,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,3
	call	delay2
	btfss	r1,6
	bcf	PORTB,3
	call	delay2
	btfss	r1,5
	bcf	PORTB,3
	call	delay2
	btfss	r1,4
	bcf	PORTB,3
	call	delay2
	btfss	r1,3
	bcf	PORTB,3
	call	delay2
	btfss	r1,2
	bcf	PORTB,3
	call	delay2
	btfss	r1,1
	bcf	PORTB,3
	call	delay2
	btfss	r1,0
	bcf	PORTB,3
	call	delay2
	btfss	r2,7
	bcf	PORTB,3
	call	delay2
	btfss	r2,6
	bcf	PORTB,3
	call	delay2
	btfss	r2,5
	bcf	PORTB,3
	call	delay2
	btfss	r2,4
	bcf	PORTB,3
	call	delay2
	btfss	r2,3
	bcf	PORTB,3
	call	delay2
	btfss	r2,2
	bcf	PORTB,3
	call	delay2
	btfss	r2,1
	bcf	PORTB,3
	call	delay2
	btfss	r2,0
	bcf	PORTB,3
	call	delay2
	btfss	r3,7
	bcf	PORTB,3
	call	delay2
	btfss	r3,6
	bcf	PORTB,3
	call	delay2
	btfss	r3,5
	bcf	PORTB,3
	call	delay2
	btfss	r3,4
	bcf	PORTB,3
	call	delay2
	btfss	r3,3
	bcf	PORTB,3
	call	delay2
	btfss	r3,2
	bcf	PORTB,3
	call	delay2
	btfss	r3,1
	bcf	PORTB,3
	call	delay2
	btfss	r3,0
	bcf	PORTB,3
	call	delay2
	btfss	r4,7
	bcf	PORTB,3
	call	delay2
	btfss	r4,6
	bcf	PORTB,3
	call	delay2
	btfss	r4,5
	bcf	PORTB,3
	call	delay2
	btfss	r4,4
	bcf	PORTB,3
	call	delay2
	btfss	r4,3
	bcf	PORTB,3
	call	delay2
	btfss	r4,2
	bcf	PORTB,3
	call	delay2
	btfss	r4,1
	bcf	PORTB,3
	call	delay2
	btfss	r4,0
	bcf	PORTB,3
	call	delay2

	movlw	b'00000100'	;***********servo6******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv5,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,2
	call	delay2
	btfss	r1,6
	bcf	PORTB,2
	call	delay2
	btfss	r1,5
	bcf	PORTB,2
	call	delay2
	btfss	r1,4
	bcf	PORTB,2
	call	delay2
	btfss	r1,3
	bcf	PORTB,2
	call	delay2
	btfss	r1,2
	bcf	PORTB,2
	call	delay2
	btfss	r1,1
	bcf	PORTB,2
	call	delay2
	btfss	r1,0
	bcf	PORTB,2
	call	delay2
	btfss	r2,7
	bcf	PORTB,2
	call	delay2
	btfss	r2,6
	bcf	PORTB,2
	call	delay2
	btfss	r2,5
	bcf	PORTB,2
	call	delay2
	btfss	r2,4
	bcf	PORTB,2
	call	delay2
	btfss	r2,3
	bcf	PORTB,2
	call	delay2
	btfss	r2,2
	bcf	PORTB,2
	call	delay2
	btfss	r2,1
	bcf	PORTB,2
	call	delay2
	btfss	r2,0
	bcf	PORTB,2
	call	delay2
	btfss	r3,7
	bcf	PORTB,2
	call	delay2
	btfss	r3,6
	bcf	PORTB,2
	call	delay2
	btfss	r3,5
	bcf	PORTB,2
	call	delay2
	btfss	r3,4
	bcf	PORTB,2
	call	delay2
	btfss	r3,3
	bcf	PORTB,2
	call	delay2
	btfss	r3,2
	bcf	PORTB,2
	call	delay2
	btfss	r3,1
	bcf	PORTB,2
	call	delay2
	btfss	r3,0
	bcf	PORTB,2
	call	delay2
	btfss	r4,7
	bcf	PORTB,2
	call	delay2
	btfss	r4,6
	bcf	PORTB,2
	call	delay2
	btfss	r4,5
	bcf	PORTB,2
	call	delay2
	btfss	r4,4
	bcf	PORTB,2
	call	delay2
	btfss	r4,3
	bcf	PORTB,2
	call	delay2
	btfss	r4,2
	bcf	PORTB,2
	call	delay2
	btfss	r4,1
	bcf	PORTB,2
	call	delay2
	btfss	r4,0
	bcf	PORTB,2
	call	delay2

	movlw	b'00000010'	;***********servo7******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv6,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,1
	call	delay2
	btfss	r1,6
	bcf	PORTB,1
	call	delay2
	btfss	r1,5
	bcf	PORTB,1
	call	delay2
	btfss	r1,4
	bcf	PORTB,1
	call	delay2
	btfss	r1,3
	bcf	PORTB,1
	call	delay2
	btfss	r1,2
	bcf	PORTB,1
	call	delay2
	btfss	r1,1
	bcf	PORTB,1
	call	delay2
	btfss	r1,0
	bcf	PORTB,1
	call	delay2
	btfss	r2,7
	bcf	PORTB,1
	call	delay2
	btfss	r2,6
	bcf	PORTB,1
	call	delay2
	btfss	r2,5
	bcf	PORTB,1
	call	delay2
	btfss	r2,4
	bcf	PORTB,1
	call	delay2
	btfss	r2,3
	bcf	PORTB,1
	call	delay2
	btfss	r2,2
	bcf	PORTB,1
	call	delay2
	btfss	r2,1
	bcf	PORTB,1
	call	delay2
	btfss	r2,0
	bcf	PORTB,1
	call	delay2
	btfss	r3,7
	bcf	PORTB,1
	call	delay2
	btfss	r3,6
	bcf	PORTB,1
	call	delay2
	btfss	r3,5
	bcf	PORTB,1
	call	delay2
	btfss	r3,4
	bcf	PORTB,1
	call	delay2
	btfss	r3,3
	bcf	PORTB,1
	call	delay2
	btfss	r3,2
	bcf	PORTB,1
	call	delay2
	btfss	r3,1
	bcf	PORTB,1
	call	delay2
	btfss	r3,0
	bcf	PORTB,1
	call	delay2
	btfss	r4,7
	bcf	PORTB,1
	call	delay2
	btfss	r4,6
	bcf	PORTB,1
	call	delay2
	btfss	r4,5
	bcf	PORTB,1
	call	delay2
	btfss	r4,4
	bcf	PORTB,1
	call	delay2
	btfss	r4,3
	bcf	PORTB,1
	call	delay2
	btfss	r4,2
	bcf	PORTB,1
	call	delay2
	btfss	r4,1
	bcf	PORTB,1
	call	delay2
	btfss	r4,0
	bcf	PORTB,1
	call	delay2

	movlw	b'00000001'	;***********servo8******************
	movwf	PORTB
	movlw	b'00000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv7,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTB,0
	call	delay2
	btfss	r1,6
	bcf	PORTB,0
	call	delay2
	btfss	r1,5
	bcf	PORTB,0
	call	delay2
	btfss	r1,4
	bcf	PORTB,0
	call	delay2
	btfss	r1,3
	bcf	PORTB,0
	call	delay2
	btfss	r1,2
	bcf	PORTB,0
	call	delay2
	btfss	r1,1
	bcf	PORTB,0
	call	delay2
	btfss	r1,0
	bcf	PORTB,0
	call	delay2
	btfss	r2,7
	bcf	PORTB,0
	call	delay2
	btfss	r2,6
	bcf	PORTB,0
	call	delay2
	btfss	r2,5
	bcf	PORTB,0
	call	delay2
	btfss	r2,4
	bcf	PORTB,0
	call	delay2
	btfss	r2,3
	bcf	PORTB,0
	call	delay2
	btfss	r2,2
	bcf	PORTB,0
	call	delay2
	btfss	r2,1
	bcf	PORTB,0
	call	delay2
	btfss	r2,0
	bcf	PORTB,0
	call	delay2
	btfss	r3,7
	bcf	PORTB,0
	call	delay2
	btfss	r3,6
	bcf	PORTB,0
	call	delay2
	btfss	r3,5
	bcf	PORTB,0
	call	delay2
	btfss	r3,4
	bcf	PORTB,0
	call	delay2
	btfss	r3,3
	bcf	PORTB,0
	call	delay2
	btfss	r3,2
	bcf	PORTB,0
	call	delay2
	btfss	r3,1
	bcf	PORTB,0
	call	delay2
	btfss	r3,0
	bcf	PORTB,0
	call	delay2
	btfss	r4,7
	bcf	PORTB,0
	call	delay2
	btfss	r4,6
	bcf	PORTB,0
	call	delay2
	btfss	r4,5
	bcf	PORTB,0
	call	delay2
	btfss	r4,4
	bcf	PORTB,0
	call	delay2
	btfss	r4,3
	bcf	PORTB,0
	call	delay2
	btfss	r4,2
	bcf	PORTB,0
	call	delay2
	btfss	r4,1
	bcf	PORTB,0
	call	delay2
	btfss	r4,0
	bcf	PORTB,0
	call	delay2

	movlw	b'00000000'	;***********servo9******************
	movwf	PORTB
	movlw	b'10000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv8,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTD,7
	call	delay2
	btfss	r1,6
	bcf	PORTD,7
	call	delay2
	btfss	r1,5
	bcf	PORTD,7
	call	delay2
	btfss	r1,4
	bcf	PORTD,7
	call	delay2
	btfss	r1,3
	bcf	PORTD,7
	call	delay2
	btfss	r1,2
	bcf	PORTD,7
	call	delay2
	btfss	r1,1
	bcf	PORTD,7
	call	delay2
	btfss	r1,0
	bcf	PORTD,7
	call	delay2
	btfss	r2,7
	bcf	PORTD,7
	call	delay2
	btfss	r2,6
	bcf	PORTD,7
	call	delay2
	btfss	r2,5
	bcf	PORTD,7
	call	delay2
	btfss	r2,4
	bcf	PORTD,7
	call	delay2
	btfss	r2,3
	bcf	PORTD,7
	call	delay2
	btfss	r2,2
	bcf	PORTD,7
	call	delay2
	btfss	r2,1
	bcf	PORTD,7
	call	delay2
	btfss	r2,0
	bcf	PORTD,7
	call	delay2
	btfss	r3,7
	bcf	PORTD,7
	call	delay2
	btfss	r3,6
	bcf	PORTD,7
	call	delay2
	btfss	r3,5
	bcf	PORTD,7
	call	delay2
	btfss	r3,4
	bcf	PORTD,7
	call	delay2
	btfss	r3,3
	bcf	PORTD,7
	call	delay2
	btfss	r3,2
	bcf	PORTD,7
	call	delay2
	btfss	r3,1
	bcf	PORTD,7
	call	delay2
	btfss	r3,0
	bcf	PORTD,7
	call	delay2
	btfss	r4,7
	bcf	PORTD,7
	call	delay2
	btfss	r4,6
	bcf	PORTD,7
	call	delay2
	btfss	r4,5
	bcf	PORTD,7
	call	delay2
	btfss	r4,4
	bcf	PORTD,7
	call	delay2
	btfss	r4,3
	bcf	PORTD,7
	call	delay2
	btfss	r4,2
	bcf	PORTD,7
	call	delay2
	btfss	r4,1
	bcf	PORTD,7
	call	delay2
	btfss	r4,0
	bcf	PORTD,7
	call	delay2

	movlw	b'00000000'	;***********servo10******************
	movwf	PORTB
	movlw	b'01000000'
	movwf	PORTD
	call	loads
	call	loads
	call	delay1
	movf	serv9,W
	movwf	temp
	call	loadr

	btfss	r1,7
	bcf	PORTD,6
	call	delay2
	btfss	r1,6
	bcf	PORTD,6
	call	delay2
	btfss	r1,5
	bcf	PORTD,6
	call	delay2
	btfss	r1,4
	bcf	PORTD,6
	call	delay2
	btfss	r1,3
	bcf	PORTD,6
	call	delay2
	btfss	r1,2
	bcf	PORTD,6
	call	delay2
	btfss	r1,1
	bcf	PORTD,6
	call	delay2
	btfss	r1,0
	bcf	PORTD,6
	call	delay2
	btfss	r2,7
	bcf	PORTD,6
	call	delay2
	btfss	r2,6
	bcf	PORTD,6
	call	delay2
	btfss	r2,5
	bcf	PORTD,6
	call	delay2
	btfss	r2,4
	bcf	PORTD,6
	call	delay2
	btfss	r2,3
	bcf	PORTD,6
	call	delay2
	btfss	r2,2
	bcf	PORTD,6
	call	delay2
	btfss	r2,1
	bcf	PORTD,6
	call	delay2
	btfss	r2,0
	bcf	PORTD,6
	call	delay2
	btfss	r3,7
	bcf	PORTD,6
	call	delay2
	btfss	r3,6
	bcf	PORTD,6
	call	delay2
	btfss	r3,5
	bcf	PORTD,6
	call	delay2
	btfss	r3,4
	bcf	PORTD,6
	call	delay2
	btfss	r3,3
	bcf	PORTD,6
	call	delay2
	btfss	r3,2
	bcf	PORTD,6
	call	delay2
	btfss	r3,1
	bcf	PORTD,6
	call	delay2
	btfss	r3,0
	bcf	PORTD,6
	call	delay2
	btfss	r4,7
	bcf	PORTD,6
	call	delay2
	btfss	r4,6
	bcf	PORTD,6
	call	delay2
	btfss	r4,5
	bcf	PORTD,6
	call	delay2
	btfss	r4,4
	bcf	PORTD,6
	call	delay2
	btfss	r4,3
	bcf	PORTD,6
	call	delay2
	btfss	r4,2
	bcf	PORTD,6
	call	delay2
	btfss	r4,1
	bcf	PORTD,6
	call	delay2
	btfss	r4,0
	bcf	PORTD,6
	call	delay2

	goto	start		;do it all over again

;********************************************************************
;****delay subroutines***********************************************

;****delay = wait (delay count value 1) cycles****	
delay1	movlw	d'185'
	movwf	count1
dec1	decfsz	count1,1
	goto	dec1
	return

;****delay = wait (delay count value 1) cycles****	
delay2	movlw	d'8'
	movwf	count1
dec2	decfsz	count1,1
	goto	dec2
	return

;********************************************************************
;******portion that read the input decode it into registers**********
;********************************************************************
loads	movf	PORTA,W		;1
	movwf	posi		;2
	movf	PORTC,W		;3
	movwf	serv		;4

	movlw	h'00'		;5
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill0
	movlw	h'01'		;9
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill1
	movlw	h'02'		;13
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill2
	movlw	h'03'		;17
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill3
	movlw	h'04'		;21
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill4
	movlw	h'05'		;25
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill5
	movlw	h'06'		;29
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill6
	movlw	h'07'		;33
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill7
	movlw	h'08'		;37
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill8
	movlw	h'09'		;41
	xorwf	serv,W
	btfsc	STATUS,Z
	call	fill9
	return			;45

fill0	movf	posi,W		;1
	movwf	serv0		;2
	return			;3
fill1	movf	posi,W
	movwf	serv1
	return
fill2	movf	posi,W
	movwf	serv2
	return
fill3	movf	posi,W
	movwf	serv3
	return
fill4	movf	posi,W
	movwf	serv4
	return
fill5	movf	posi,W
	movwf	serv5
	return
fill6	movf	posi,W
	movwf	serv6
	return
fill7	movf	posi,W
	movwf	serv7
	return
fill8	movf	posi,W
	movwf	serv8
	return
fill9	movf	posi,W
	movwf	serv9
	return


;********************************************************************
;******read servs value and convert into r1-r4 in time domain********
;********************************************************************
loadr	movlw	d'00'		;1
	xorwf	temp,W		;2
	btfsc	STATUS,Z	;if Z is high means temp==d'00'
	call	flre00		;call the subroutine flre00
	movlw	d'01'		;if not, test with following values
	xorwf	temp,W		;6
	btfsc	STATUS,Z	;7
	call	flre01		;8
	movlw	d'02'		;9
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre02
	movlw	d'03'		;13
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre03
	movlw	d'04'		;17
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre04
	movlw	d'05'		;21
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre05
	movlw	d'06'		;25
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre06
	movlw	d'07'		;29
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre07
	movlw	d'08'		;33
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre08
	movlw	d'09'		;37
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre09
	movlw	d'10'		;41
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre10
	movlw	d'11'		;45
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre11
	movlw	d'12'		;49
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre12
	movlw	d'13'		;53
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre13
	movlw	d'14'		;57
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre14
	movlw	d'15'		;61
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre15
	movlw	d'16'		;65
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre16
	movlw	d'17'		;69
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre17
	movlw	d'18'		;73
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre18
	movlw	d'19'		;77
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre19
	movlw	d'20'		;81
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre20
	movlw	d'21'		;85
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre21
	movlw	d'22'		;89
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre22
	movlw	d'23'		;93
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre23
	movlw	d'24'		;97
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre24
	movlw	d'25'		;101
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre25
	movlw	d'26'		;105
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre26
	movlw	d'27'		;109
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre27
	movlw	d'28'		;113
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre28
	movlw	d'29'		;117
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre29
	movlw	d'30'		;121
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre30
	movlw	d'31'		;125
	xorwf	temp,W
	btfsc	STATUS,Z
	call	flre31
	return			;129

flre00	movlw	b'10000000'	;1
	movwf	r1		;2
	movlw	b'00000000'	;3
	movwf	r2		;4
	movlw	b'00000000'	;5
	movwf	r3		;6
	movlw	b'00000000'	;7
	movwf	r4		;8
	return			;9
flre01	movlw	b'11000000'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre02	movlw	b'11100000'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre03	movlw	b'11110000'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre04	movlw	b'11111000'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre05	movlw	b'11111100'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre06	movlw	b'11111110'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre07	movlw	b'11111111'
	movwf	r1
	movlw	b'00000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre08	movlw	b'11111111'
	movwf	r1
	movlw	b'10000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre09	movlw	b'11111111'
	movwf	r1
	movlw	b'11000000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre10	movlw	b'11111111'
	movwf	r1
	movlw	b'11100000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre11	movlw	b'11111111'
	movwf	r1
	movlw	b'11110000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre12	movlw	b'11111111'
	movwf	r1
	movlw	b'11111000'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre13	movlw	b'11111111'
	movwf	r1
	movlw	b'11111100'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre14	movlw	b'11111111'
	movwf	r1
	movlw	b'11111110'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre15	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'00000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre16	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'10000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre17	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11000000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre18	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11100000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre19	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11110000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre20	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111000'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre21	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111100'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre22	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111110'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre23	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'00000000'
	movwf	r4
	return
flre24	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'10000000'
	movwf	r4
	return
flre25	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11000000'
	movwf	r4
	return
flre26	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11100000'
	movwf	r4
	return
flre27	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11110000'
	movwf	r4
	return
flre28	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11111000'
	movwf	r4
	return
flre29	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11111100'
	movwf	r4
	return
flre30	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11111110'
	movwf	r4
	return
flre31	movlw	b'11111111'
	movwf	r1
	movlw	b'11111111'
	movwf	r2
	movlw	b'11111111'
	movwf	r3
	movlw	b'11111111'
	movwf	r4
	return

	end;	

