	LIST p=18f452		;list directive to define processor
		#include <p18f452.inc>	;processor specific definitions

	CONFIG OSC = HS	;uso xtal 20mhz
	CONFIG OSCS = OFF
	CONFIG BOR = ON
	CONFIG BORV = 25
	CONFIG WDT = OFF
	CONFIG CCP2MUX = OFF
	CONFIG STVR = OFF
	CONFIG LVP = OFF
	CONFIG DEBUG = OFF


;***** VARIABLE DEFINITIONS

contador             EQU    0x01   


;**********************************************************************
        ORG     0x0000             ; processor reset vector
        goto    main              ; go to beginning of program
        ORG     0x0008             ; vector de interrupciones
        
highINT


        btfsc PIR1,RCIF            ;CAUSANTE DE LA INTERRUPCIÓN?
        call IntUSART            ;solo esperamos interrupcion a la recepcion.


IntReturn
       
        BSF PIE1,RCIE	;rehabilitamos interrupciones
        BSF INTCON,PEIE
	    bsf INTCON,GIE        
        retfie  FAST               ; return de interrupciones y guardamos STATUS,WREG y BSR

main

; remaining code goes here

    CLRF     STATUS
   	movlw 	07h					;puerto A como entradas/salidas digitales
	movwf 	ADCON1
	bsf 	INTCON2,7 					;pullups inhabilitadas
    MOVLW    b'00111111'            ;PORTA como entrada
    MOVWF    TRISA
    MOVLW    b'00000000'
    MOVWF     TRISB               ;Portb como salida
    MOVLW     b'11000000'
    MOVWF    TRISC
    CLRF    INTCON                ;limpiamos intcon, gie=0
    MOVLW d'15'                ;seleccionamos la tasa de baudios a 9600
    MOVWF SPBRG
    MOVLW b'10000000'			;BRGH a 0 con xtal 20mhz
    MOVWF TXSTA                ;habilitamos la transmisión asincrona
    BSF PIE1,RCIE
    BSF INTCON,PEIE
  
   CLRF    LATB                ;limpiamos el portb
    CLRF    LATA                ;limpiamos el porta
    MOVLW    b'10010000'            ;habilitamos recepción asincrona
    MOVWF RCSTA            
    bsf     INTCON,GIE               ;interrupciones globales habilitadas

Label_0 
    goto Label_0


IntUSART    
            BCF INTCON,RCIE            ;borramos flag interrución por recepcion
            
            MOVF RCREG,0            ;transferimos el byte recibido al PORTB
            MOVWF PORTB
            
            BSF TXSTA, TXEN        ;preparamos la transferencia
            
            MOVF PORTA,0
            MOVWF TXREG            ;cargamos buffer de transmisión con PORTA
            return

        END                       ; directive 'end of program'
