; Mike Nitchie
; Bharat Kuncharavelu
; USART Presentation - Transmit code
; 3/31/2014

	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"	;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings (oscillator type etc.)

	cblock 	0x20 			;start of general purpose registers
		d1
		d2
		toXmit
	endc
	
	org	0x0000	

	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)


	bsf		STATUS,RP0
	movlw	b'10000110'		; Enable TX and RX pins
	movwf	TRISB			
	movlw	0x00
	movwf	TRISA			

setBaudRate
	bsf		TXSTA,BRGH		; Select high baud rate
	movlw	d'25'
	movwf	SPBRG			; 9600 baud

configureModes
	bcf		TXSTA,SYNC		; selects Async mode
	bcf		STATUS,RP0		; return to bank 0
	bsf		RCSTA,SPEN		; Configures PORTB<1,2> as serial ports

	bsf		STATUS, RP0
	bsf		TXSTA,TXEN		; enable transmission (TX pin goes high)
	bcf		STATUS, RP0

waitForTrigger
	btfss	PORTB,7
	goto	waitForTrigger

setData
	movlw	0x00			; set output to the
	movwf	toXmit			; PORTA inputs

	btfsc	PORTA,1			; A2 goes to
	bsf		toXmit,2		; bit 2
	btfsc	PORTA,0			; A0 goes to 
	bsf		toXmit,1		; bit 1
	btfsc	PORTA,7			; A7 goes to
	bsf		toXmit,0		; bit 0

	movf	toXmit,w
	movwf	TXREG
	bsf		STATUS,RP0

waitUntilDone
	
	btfss	TXSTA,TRMT     	; TRMT is set when TSR is empty
	goto	waitUntilDone

transmitAgain
	bcf		STATUS,RP0
	goto	waitForTrigger

	end



