;
; www.RoboHobby.com
; Assember code for PIC processor PIC16F628A.
; 
; Motor driver for small hand-made robot.
; (c) 2004-2006, Oleg Lioubtchenko, Anton Lioubtchenko
;
;
		list p=16F628A, r=DEC

		#include	"P16F628A.INC"

		__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _MCLRE_OFF & _LVP_OFF

		cblock	0x20			;variables
		offset,char,loop1,loop2
		endc

		org		0x000			;program start

;******************
; initialization:

;------------------------
Start		movlw	0x07
		    movwf	CMCON			;turn comparators off (make it like a 16F84)
;------------------------


		bsf		STATUS,RP0		;bank 1

		movlw	b'00000010'		;set PORTB data directions
		movwf	TRISB

		movlw	b'00000000'		;set PORTA data directions
		movwf	TRISA


		bcf		STATUS,RP0		;bank 0

		movlw	b'11110000'		;shut off leds
		movwf	PORTB

		movlw	b'10101010'		;shut off leds of Port A
		movwf	PORTA

		bsf		STATUS,RP0		;bank 1

		clrf	offset			;offset=0
		decf	offset,1		;offset=$ff (-1)

;************************
;* init RS232 at 9600,8,N,1
		movlw	0x19			;0x19=9600 baud
		movwf	SPBRG
		movlw	b'00100100'		;brgh = high
		movwf	TXSTA			;enable async transmit

		bcf		STATUS,RP0		;bank 0

		movlw	b'10010000'		;enable async receive
		movwf	RCSTA

		call	delay			;startup settle time



loop:
		call	receive			;wait for a char
		movwf	PORTA
		call	transmit		;echo the char

		goto	loop


;******************************************
;* Wait for a char from RS232 - return in W
receive:
		btfss	PIR1,RCIF		;wait for a byte from RS232
		goto	receive

		movf	RCREG,W			;return received byte in W
		return

;*************************
transmit:
		movwf	TXREG			;transmit byte in W 

		bsf		STATUS,RP0		;bank 1
send1:	btfss	TXSTA,TRMT		;is the transmission done?
		goto	send1			;no, then go again

		bcf		STATUS,RP0		;bank 0
		return					;OK, done

;*******
;* Delay
delay   movlw   60 
        movwf   loop1 
outer   movlw   200 
        movwf   loop2 
inner   nop 
        nop 
        decfsz  loop2,F 
        goto    inner
        decfsz  loop1,F 
        goto    outer 
        return

		END