;**********************************************************************
;
;    Filename:	   PICrs232.asm
;    Date:   1-15-05 
;    File Version:    1 
;    Author: j baumeister 
;    Company: himself 
;
;
;**********************************************************************
;
;    Files required 
;                    12F675.lkr 
;
;**********************************************************************
;
;    Notes:  This is the basic RS 232 serial communication program    
;    It uses two subroutines, one for transmitting and one for        
;    receiving.  The "test" program listed under "Main" echoes back    
;    the byte that it receives                                            *
;    Pin 3 is the output. Pin 5 the input   It uses a visual basic    
;    program (vBasic_simple_Interface) running on a  the PC  to test the application 
;**********************************************************************

	list      p=12f675          	 ; list directive to define processor
	#include <p12f675.inc>      ; processor specific variable definitions

	errorlevel  -302              	; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT  

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The labels following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.
; Reset Vector
;********************************************************************
	ORG     0x000            		; processor reset vector
	nop				; Inserted For ICD2 Use
	goto    Init              		; go to beginning of program

;***** Assign Constants Value *************************************** 
#define	BANK1		banksel 0x80	;Select Bank1
#define	BANK0		banksel 0x00	;Select Bank0
#define serial_in		GPIO,2		;serial data in
#define serial_out		GPIO,4		;serial data out
#define	bit_K		h'DA'			; hex DA = 1200 bits/sec (218)
									; hex 6b = 2400 bits/sec (107)
									; hex 32 = 4800 bits/sec (50)
									; hex 17 = 9600 bits/sec (23)
									; hex 0A =19200 bits/sec (10)
#define half_bit		bit_K/2		
			
;****** Reserve Space for Variables (file registers) ********************
		org		0x20	; start at memory location Hex 20
					; assign 1, 8 bit byte of memory to each
count		res		1	; used to adjust the delay time
rcv_byte	res		1	; the received byte
delay_cntr	res		1	; counter for serial delay routine
bit_cntr	res		1	; number of transmitted bits
xmt_byte	res		1	; the transmitted byte
Display		res		1	; the variable that carries the byte to be 
					; transmitted to the RS-232 subroutine for								; transmission
;**********************************************************************
Init
	BANK1					;Switch to Bank1
	call   	0x3FF      		; retrieve factory calibration value
	movwf   OSCCAL	   		 ; update register with factory cal value 
	movlw	B'11011111'		 ;set internal clock(bit5=0)
	movwf	OPTION_REG	
	clrf	VRCON			;Vref Off (power off the comparator voltage, saves power)
	clrf	ANSEL
	movlw	B'11001101'		; pin 4,5,7 are inputs and pins 2,3,6 are outputs
	movwf	TRISIO			;Tri-State pins 4,5,7 .... 2,3,& 6 are outputs
	BANK0				;BANK 0
	clrf	GPIO			;Clear Port
	movlw	0x07		
	movwf	CMCON		;Comparator Off
;*******************************************************************
;  this is the "Main" program which in this example is set to receive and re-transmit
;  a byte sent from the computers serial port	
Main
	call	Receive232		; receive the byte sent from the computer
	movf	rcv_byte,0		; move the received byte to W register
	movwf	xmt_byte		; varable used by the xmit subroutine
					; contains the byte to be transmitted
	call	xmit232			; calls the transmit subroutine
	goto	Main			; do it again
;********************************subroutines ***********************
;*******************************************************************
xmit232					; RS-232C serial out.  the byte in file register
					; xmt_bute will transmit
	
again
	movlw	h'08'
	movwf	bit_cntr
	
	bsf	serial_out		;bsf for direct connection 
					;bcf for standard connection 
	call 	bit_delay
xmit
	rrf	xmt_byte,1
	btfsc	STATUS,0		;btfsc	STATUS,0  for direct connection			
					;btfss	STATUS,0  for standard connection
	bcf	serial_out	
	btfss	STATUS,0		;btfss	STATUS,0  for direct connection	
					;btfsc	STATUS,0  for standard connection
	bsf	serial_out		
	call	bit_delay
	decfsz	bit_cntr,1
	goto	xmit
	bcf	serial_out		;bcf for direct connection
					;bsf for standard connection
	call	bit_delay
	return
	
bit_delay
	movlw	bit_K
	movwf	delay_cntr
loop
	nop
	decfsz	delay_cntr,1
	goto	loop
	return

;******************* Receive RS-232 data *******************************
Receive232
	nop
start_bit
	btfss	serial_in			;btfss if connected via 22K resistor
					;btfsc if standard connection
	goto	start_bit
	call	start_delay
	btfss	serial_in			;btfss if connect via 22k resistor
					; btfsc if standard connection
	goto	start_bit	
	movlw	h'08'
	movwf	bit_cntr
	clrf	rcv_byte
receive
	call	bit_delay_R
	btfsc	serial_in			;btfsc if connect via 22k resistor
					;btfss if standard connection
	bcf	STATUS,0
	btfss	serial_in			;btfss if connect via 22k resistor
					;btfsc if standard connection
	bsf	STATUS,0		
	rrf	rcv_byte,1
	decfsz	bit_cntr,1
	goto	receive
	call 	bit_delay_R
	return

bit_delay_R
	movlw	bit_K
	movwf	delay_cntr
loop_R
	nop
	decfsz	delay_cntr,1
	goto	loop_R
	return

start_delay
	movlw	half_bit
	movwf	delay_cntr
loop_R2
	nop
	decfsz	delay_cntr,1
	goto	loop_R2
	return	
		
	END           ; directive 'end of program'

