;**********************************************************************
;                 MANDO ALTERNATIVO PARA GP - 30 GE                   *
;**********************************************************************
;   If interrupts are not used all code presented between the ORG     *
;   0x004 directive and the label main can be removed. In addition    *
;   the variable assignments for 'w_temp' and 'status_temp' can       *
;   be removed.
;**********************************************************************
;    Date:          15/05/2007                                        *
;    File Version:  1.0                                               *
;                                                                     *
;    Author:      JOSE LUIS LAFUENTE CARRASCO                         *
;    Company:     LAFUENTE MATEOS                                     *
;                                                                     *
;**********************************************************************
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************

	list      p=16f676           ; list directive to define processor
	#include <p16F676.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

;	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x20        ; variable used for context saving 
status_temp   EQU     0x21        ; variable used for context saving
MODE		  EQU	  0X22		  ; variable para ram_mode


; initialize eeprom locations

		ORG	0x2100
;		DE	0x00, ;0x01, 0x02, 0x03

;**********************************************************************
		ORG     0x000             ; PROCESSOR RESET VECTOR
		goto    INIC              ; go to beginning of program
;**********************************************************************
		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    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



INIC
; these first 4 instructions are not required if the internal oscillator is not used
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
;
; sigue aqui el programa principal configurando
		CALL	INIC_PORTA
		CALL	INIC_PORTC
;
;Comprueba el valor de la direccion 00 de la eeprom
;
		CALL	LEE_MODE			;LEE LA POSICION MODE

		BTFSS	MODE,7				;SALTA SI EL BIT ES 1,
									;SI BIT7 ES UNO LO PROBABLE ES QUE TENGA UN FFH
		GOTO	MAIN2				;NO HACE NADA, TIENE UN MODE  PREFIJADO
		MOVLW	01H					;ESCRIBE EL OO PARA PONER EN AUTO EN MODE Y EEPROM
		MOVWF	MODE
		CALL	ESCRIBE_MODE

	

; sigue aqui el programa principal

MAIN2
		CALL	LEE_MODE
		movf	MODE,0

		MOVWF	PORTC
		GOTO	MAIN2


;
;SUBRUTINAS
;

;INICIALIZACION DEL PORTA

INIC_PORTA

		bcf 	STATUS,RP0 			;Bank 0
		clrf 	PORTA 				;Init PORTA
		movlw 	05h 				;Set RA<5:0> to
		movwf 	CMCON 				;digital I/O
		bsf 	STATUS,RP0 			;Bank 1
		clrf 	ANSEL 				;digital I/O
		movlw 	3Fh 				;Set RA<5:0> as inputs
		movwf 	TRISA 				
		bcf 	STATUS,RP0 			;Bank 0

		RETURN

;INICIALIZACION DEL PORTC

INIC_PORTC

		bcf 	STATUS,RP0 			;Bank 0
		clrf 	PORTC 				;Init PORTC
		bsf 	STATUS,RP0 			;Bank 1
		clrf 	ANSEL 				;digital I/O
		movlw 	00h 				;Set RC<5:0> as OUTPUTS
		movwf 	TRISC 				
		bcf 	STATUS,RP0 			;Bank 0

		RETURN

; LEER DE LA EEPROM

LEE_MODE

		bsf 	STATUS,RP0 			;Bank 1
		movlw 	00H			 		;ES LA DIRECCION A LEER
		movwf 	EEADR 				;Address to read
		bsf 	EECON1,RD 			;EE Read
		movf 	EEDATA,W			;Move data to MODE
		movwf	MODE
		bCf 	STATUS,RP0 			;Bank 0
		RETURN

; ESCRIBE EN LA EEPROM

ESCRIBE_MODE
									;contenido de mode a eeprom
		bsf 	STATUS,RP0 			;Bank 1
		bcf 	STATUS,RP0 			;Bank 0
		MOVLW	00h					;CARGA DIRECCION DE EEPROM
		bsf 	STATUS,RP0 			;Bank 1
		MOVWF	EEADR
		bcf 	STATUS,RP0 			;Bank 0
		MOVF	MODE,0				;CARGA EL CONTENIDO DE MODE EN W
		bsf 	STATUS,RP0 			;Bank 1
		MOVWF	EEDAT				;LO PONE EN EEDAT
		bsf 	EECON1,WREN 		;Enable write
		bcf 	INTCON,GIE 			;Disable INTs
		bcf 	STATUS,RP0 			;Bank 0
		movlw 	0x55				;Unlock write	;REQUIERED
		bsf 	STATUS,RP0 			;Bank 1
		movwf 	EECON2 				;				;REQUIERED
		bcf 	STATUS,RP0 			;Bank 0
		movlw   0xAA				;b'10101010'			;carga aah		;REQUIERED
		bsf 	STATUS,RP0 			;Bank 1
		movwf 	EECON2 				;				;REQUIERED
		bsf 	EECON1,WR 			;Start the write;REQUIERED
		NOP
		NOP
		NOP
		bsf 	INTCON,GIE 			;Enable INTS
		bcf 	EECON1,WREN 
		bcf 	STATUS,RP0 			;Bank 0

		RETURN

; VERIFICAR ESCRITURA DE EEPROM

		bcf 	STATUS,RP0 			;Bank 0

 ;Any code

		bsf 	STATUS,RP0 			;Bank 1 READ
		movf 	EEDATA,W 			;EEDATA not changed
;from previous write
		bsf 	EECON1,RD 			;YES, Read the
;value written
		xorwf 	EEDATA,W
		btfss 	STATUS,Z 			;Is data the same
;		goto 	WRITE_ERR 			;No, handle error

 ;Yes, continue



		END                       ; directive 'end of program'

