; ***************************************
; ***** Assembler Code File Details *****
; ***************************************
;    Filename: 		rs-3.asm
;    Date: 		30/12/2004
;    File Version: 	v3.0
;    Written by: 	Mark Craig
;    Project :		Robosapien
;    Function: 		take control of robosapien through original PCB


; ************************************
; ***** (1) Setup PICmicro type. *****
; ************************************

	list      p=16F84A             ; define PICmicro type
        #include <p16F84A.inc>         ; variable definitions
	errorlevel -302		      ; suppress warning 302

; **************************************
; ***** (2) Setup PICmicro config. *****
; **************************************

   	__config   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

; **********************************
; ***** (3) RAM File Registers *****
; **********************************

	cblock 	h'20'
TEMP		; Temporary Scratchpad
COUNTER		; Counter for loops
WC1		; Counter for wait loop
WC2		; Counter for wait loop
WC3		; Counter for wait loop
B0		; General purpose
B1		; General purpose
B2		; General purpose
B3		; General purpose
B4		; General purpose
		; (any other registers go here)
txreg
rxreg
bitcount

d1		; bitdelay counter
d2		; bitdelay counter
d3		; bitdelay counter
	endc

#DEFINE	IRIN	PORTA,0		; connected to IR reciever in RS's head
#DEFINE	SEROUT	PORTA,3		; connected to IR-OUT on PCB
#DEFINE	LED1	PORTA,2		; connected to an LED for debug
#DEFINE LED2	PORTA,1		; connected to an LED for debug


; ********************************************
; ***** (4) Assembler code reset vectors *****
; ********************************************

; ***** Reset vector *****
	org	d'00'
	clrf	PORTA		; initialise PORTA
	bsf	SEROUT
	clrf	PORTB		; initialise PORTB
	goto	init		; jump to init

; ***** Interrupt vector *****
	org	d'04'
	return			; return

; *************************************
; ***** (5) Common sub-procedures *****
; *************************************

	#include <wait.inc>     ; wait sub-procedure
	#include <robosap.inc>
				; (any other sub-procedures go here)

echomode:		; This sub echos the recieved ir-data straight to the ir-output forever
	btfsc	IRIN		; if input set
	bsf	SEROUT		; set output
	nop
	btfss	IRIN		; if input clear
	bcf	SEROUT		; clear output
	nop
	bsf	LED2
	goto	echomode	; loop forever (reset PIC or turn off robosapien to resume normal operation)


; ***************************************
; ***** (6) Start of assembler code *****
; ***************************************

; ***** Initialisation *****
init:	bsf	STATUS,RP0
	movlw	b'10001'
	movwf	TRISA
	movlw	b'00000000'
	movwf	TRISB
	bcf	STATUS,RP0

; ***** Main program *****
main:	
	; This is a simple program to test both the readir and send routines

	bsf	LED1
	bcf	LED2
	call	readir
	call	send
	bcf	LED1
	bsf	LED2
	movlw	d'5'
	call	wait	; .5 secs
	goto	main

;	goto	echomode



; ******************************
; ***** (7) End of Program *****
; ******************************

	sleep
	end
















