	list      p=16F690           ; list directive to define processor
	#include <p16F690.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file


	__CONFIG   _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF 


	
	include "..\..\modules\base.inc"

	__IDLOCS 0100h			; version

	EXPAND_MACROS	EXP_COND	; macro expansion mode

;***** DEFINES ************************************************************
;#define DEBUG				; for some debug message
;#define NO_DELAY			; for fast simulation
;#define NO_RS232			; for fast simulation

xtal_freq	EQU	4 MHz  		; crystal frequency
core_freq	EQU	xtal_freq/4	; core operating frequency


; ----- porta -------------------------------------------------------------

#define		PORTA_0		PORTA,0		; free
#define		PORTA_1		PORTA,1		; free
#define		PORTA_2		PORTA,2		; free
#define		PORTA_3		PORTA,3		; free
#define		PORTA_4		PORTA,4		; free
#define		PORTA_5		PORTA,5		; free
;#define		led		PORTA,2		; out
;#define		PORTA_3		PORTA,3		; free
;#define		PORTA_4		PORTA,4		; free (open drain!)

; ----- portb -------------------------------------------------------------
#define		RS232_TxD	PORTB,5		; out
#define		RS232_RxD	PORTB,7		; in

#define		PORTB_0		PORTB,0		; free
#define		PORTB_1		PORTB,1		; free
#define		PORTB_2		PORTB,2		; free
#define		PORTB_3		PORTB,3		; free
#define		PORTB_4		PORTB,4		; free
;#define		PORTB_5		PORTB,5		; free
#define		PORTB_6		PORTB,6		; free
;#define		PORTB_7		PORTB,7		; free

; ----- portc -------------------------------------------------------------

#define		PORTC_0		PORTC,0		; free
#define		PORTC_1		PORTC,1		; free
#define		PORTC_2		PORTC,2		; free
#define		PORTC_3		PORTC,3		; free
#define		PORTC_4		PORTC,4		; free
#define		PORTC_5		PORTC,5		; free
#define		PORTC_6		PORTC,6		; free
#define		PORTC_7		PORTC,7		; free


;***** MACROS *************************************************************

	include	"..\..\modules\rs232.mac"


;***** CONSTANT DEFINITIONS ***********************************************
option_val	EQU	B'00000010'	; Timer source : intclk / 8
trisb_val	EQU	B'10110000'	; TRISA value
trisa_val	EQU	B'00011111'	; TRISB value (all input)
;intcon_val	EQU	00100000b	; Interrupt control


;***** VARIABLE DEFINITIONS ***********************************************
	cblock	0Ch
		w_temp			; variable used for context saving
		status_temp		; variable used for context saving
;		fsr_temp		; variable used for context saving

		TEMP			; dummy register



	endc




;***** ENTRY POINT ********************************************************
		ORG	000h		; processor reset vector
		clrwdt
		clrf	PORTA		; clear latch
		clrf	PORTB		; clear latch
		goto	start


;***** IT HANDLER *********************************************************
;* Interrupt handler
;* <comment>
;**************************************************************************
;		ORG	004h		; interrupt vector location
;		movwf	w_temp		; save off current W register contents
;		swapf	STATUS,w	; move STATUS register into W register
;		movwf	status_temp	; save off contents of STATUS register
;
;		movf	FSR,w		; save FSR
;		movwf	fsr_temp
;--------------------------------------------------------------------------

		; <IT Code>

;--------------------------------------------------------------------------
;		movf	fsr_temp,w	; restore FSR
;		movwf	FSR

;		swapf	status_temp,w	; retrieve copy of STATUS register
;		movwf	STATUS		; restore pre-isr STATUS register contents
;		swapf	w_temp,f
;		swapf	w_temp,w	; restore pre-isr W register contents
;		retfie			; return from interrupt




;***** REAL PROGRAM START *************************************************
start
;----- set configuration --------------------------------------------------
		bank1
;		ERRORLEVEL -302
;		movlw	option_val
;		movwf	OPTION_REG	; set OPTION_REG

		BSF      STATUS,RP0 ;BANK1
       ; MOVLW    B'00011111'
       ; MOVWF    TRISA
        ;MOVLW    B'00000000'
        ;MOVWF    TRISB
        

		movlw	trisa_val
		movwf	TRISA		; set TRISA
		movlw	trisb_val
		movwf	TRISB		; set TRISB

		BCF      STATUS,RP0 ;BANK0 
;		bank0
;		ERRORLEVEL +302
;		movlw	intcon_val	
;		movwf	INTCON		; set INTCON

;----- inits --------------------------------------------------------------


		RS232_INIT	9600, 8, 1


;----- main cycle  --------------------------------------------------------


		movlw	4
		movwf	TEMP
main_cycle
		movlw  'P' 
		RS232_SENDW
		movlw  'I' 
		RS232_SENDW
		movlw  'C' 
		RS232_SENDW
		movlw  ':' 
		RS232_SENDW
		movlw  '\n' 
		RS232_SENDW
		movlw  '\r' 
		RS232_SENDW



;		WAIT	500 Millisec, 0

	;	movlw	00100b		; togle led
	;	xorwf	PORTA,f

		decfsz	TEMP,f
		goto	main_cycle


run	
		RS232_RECEIVE


		sublw	'\r'
		btfss	STATUS,Z
		goto	cont_1
		
		
		movlw	'\r'		; convert: 0x0d -> 0x0d, 0x0a
		RS232_SENDW
		movlw	'\n'
		RS232_SENDW
		goto	run

cont_1		movf	RS232_RecvBuffer,w
		sublw	'A' -1		; convert: 'A-Z' -> 'a-z'
		btfsc	STATUS,C
		goto	send_1

		movf	RS232_RecvBuffer,w
		sublw	'Z'
		btfss	STATUS,C
		goto	send_1

		movf	RS232_RecvBuffer,w
		addlw	32
		goto	send_2

send_1		movf	RS232_RecvBuffer,w
send_2		RS232_SENDW
		goto	run









;**************************************************************************

	include	"..\..\modules\rs232.inc"

	end				  ; end of program








