; --------------------------------------------------------
; | FILE    : mark_1.asm                                 |
; | PROJECT : Low Cost PC Based Oscilloscope             |
; | DESC    : Test Serial Communications, sends out      |
; |			: "Testing.." Contiunously                   |
; | ==================================================== |
; | DATE    : 12/02/2002                                 |
; | BY      : Colin K McCord                             |
; | VERSION : 1.1                                        |
; --------------------------------------------------------			

	#include <p16f877.inc>	; Using PIC16F877
	#define BAUD .115200	; Put baud rate here (e.g. 4800bps = 4800)
	#define XTAL .20		; Put Crystal fequency here (e.g. 8MHz = 8)	
	#define	X  ((XTAL * .1000000)/(.64 * BAUD))-.1
DATA_TX		equ 	20h	; TX Register
DATA_RX		equ 	21h	; RX Register
ERR			equ		22h	; RX Error Register


	org 0x0000				; Reset Vector
	goto MAIN	

	org 0x0020				; Main program

	; Include RS232 Subroutines (INITCOMS, PUTCHAR, GETCHAR)


MAIN:
 	call	INITCOMS		; Initialise RS232 Communications
	call	TESTCOMS

INITCOMS	

	bsf	STATUS,RP0	; Change to Bank1.
	bcf	STATUS,RP1
	bsf	TRISC,7		; RX pin is set as an input.
	bcf	TRISC,6		; TX is set as an output.
	movlw	X			; Move X to Baud Rate Generator.
	movwf	SPBRG	
 	movlw	b'00100100'	; 8 data bits, TX enabled.
	movwf	TXSTA		; Low speed SPBRG mode.
	bcf		STATUS,RP0	; Back again to Bank0.
	bcf	STATUS,RP1
	movlw	b'10010000'	; USART enabled, 8 data bits.
	movwf	RCSTA		; Receiver enabled.
	RETURN


; --------------------------------------------------------
; | FUNCTION: Transmits one char over RS232 using USART  |
; | RESOURCE: PIC USART                                  |
; | ENTRY   : 8-bit datum in DATA_TX                     |
; | EXIT    : Contents of DATA_TX unchanged, byte TXed   |
; | ==================================================== |
; | DATE    : 12/02/2002                                 |
; | BY      : Colin K McCord                             |
; | VERSION : 1.0                                        |
; --------------------------------------------------------

PUTCHAR

	btfss	PIR1,TXIF	; Check, is TX buffer full?
	goto	PUTCHAR		; IF not THEN try again.
	movf	DATA_TX,w	; ELSE get datum.
	movwf	TXREG		; and copy to USART TX register.
	return


; --------------------------------------------------------
; | FUNCTION: Receives a char form RS232 using USART     |
; | RESOURCE: PIC USART                                  |
; | ENTRY   : None                                       |
; | EXIT    : DATA_RX holds the received byte            |
; |         : ERR is 00 if no error, Framing ERR = -1,   |
; |         : ERR = -2 if overflow and ERR = -3 if both. |
; | ==================================================== |
; | DATE    : 12/02/2002                                 |
; | BY      : Colin K McCord                             |
; | VERSION : 1.0                                        |
; --------------------------------------------------------

GETCHAR

	clrf	ERR			; Zero flag byte.
	btfss	PIR1, RCIF	; Check, is there a char ready?
	goto	GETCHAR		; IF not THEN try again.

; Error return
	btfss 	RCSTA, FERR	; Was there a Framing error ?
	goto	CHECK_OERR	; IF not THEN check for OIverflow.
	movlw	-1			; ELSE record a Framing error.

CHECK_OERR

	btfsc	RCSTA,OERR	; Check for Overflow error.
	goto	GET_EXIT	; IF none THEN complete.
	decf	ERR,f		; Otherwise register error
	decf	ERR,f		
	bcf		RCSTA,CREN	; and rest the logic
	bsf		RCSTA,CREN

GET_EXIT

	movf	RCREG,w		; Get datum.
	movwf	DATA_RX		; and put away.
	return




TESTCOMS

	movlw	A'T'  		; Transmit 'T'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A'e'  		; Transmit 'e'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A's'  		; Transmit 's'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A't'  		; Transmit 't'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A'i'  		; Transmit 'i'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A'n'  		; Transmit 'n'
	movwf	DATA_TX		
	call 	PUTCHAR


	movlw	A'g'  		; Transmit 'g'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A'.'  		; Transmit '.'
	movwf	DATA_TX		
	call 	PUTCHAR

	movlw	A'.'  		; Transmit '.'
	movwf	DATA_TX		
	call 	PUTCHAR

	goto TESTCOMS

	END

