

	list	p=16f690, r=dec, l=0

;***** INCLUDES ***********************************************************

	include "p16f690.inc"
	include "base.inc"

;***** CONFIG ************************************************************


;	__CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _CP_OFF
	;__CONFIG   _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF 

	__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

	__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


; ----- portc -------------------------------------------------------------

#define		RS232_TxD	PORTA,0		; out
#define		RS232_RxD	PORTA,1		; in


; ----- portb -------------------------------------------------------------

#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


;***** MACROS *************************************************************

	include "wait.mac"
	include	"rs232.mac"


;***** CONSTANT DEFINITIONS ***********************************************
option_val	EQU	00000010b	; Timer source : intclk / 8
trisa_val	EQU	01111111b	; TRISC value TX - RC4=0 output
trisb_val	EQU	11111111b	; 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
		BCF STATUS,RP0 ;Bank 0
		BCF STATUS,RP1 ;
		CLRF PORTC ;Init PORTA
		BSF STATUS,RP1 ;Bank 2
		CLRF ANSEL ;digital I/O
		CLRF ANSELH
		BSF STATUS,RP0 ;Bank 1
		BCF STATUS,RP1 ;
		movlw	trisa_val
		movwf	TRISA		; set TRISC
;		bcf RS232_TxD
;		bcf RS232_RxD
		movlw	trisb_val
		movwf	TRISB		; set TRISB
		BSF STATUS,RP0 ;Bank 3
		BSF STATUS,RP1 ;
		;bank3
;		movlw	option_val
;		movwf	OPTION_REG	; set OPTION_REG
		
		BANKSEL CCP1CON ;Set Bank bits to point
;to CCP1CON
		CLRF CCP1CON ;Turn CCP module off

		BANKSEL ADCON0
		CLRF ADCON0

		bank0

	;	bsf RS232_TxD
	;	bsf RS232_RxD

	;	movlw	255
	;	movwf	PORTC
	;	sleep

;		MOVLW    b'00010000'
;		MOVWF    PORTC            


;		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  'A' 
		RS232_SENDW
		movlw  'T' 
		RS232_SENDW
		movlw  0x0d 
		RS232_SENDW
		movlw  0x0a 
		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 "wait.inc"
	include "rs232.inc"

	end				  ; end of program








