;-------------------------------------------------------------------------;
;
;		Project:	count down timer with 4 display 7-segment
;    	Filename:	timer-7-s.asm
;    	Date:		30/07/2011                             
;    	Author:		Giuseppe Aquilani
;    	Web:		http://www.ik0jre.net/
;       email:		ik0jre "at" ik0jre.net - giuseppe.aquilani "at" virgilio.it
;
;		History:	0.0 2011/07/30 develop 
;					0.1 2011/08/30 remove some bug
;					0.2	2011/09/24 do modify for web
;					0.3 2011/10/16 solve 1s delay relays bug
;-------------------------------------------------------------------------;
;		Processor:	16F84A
;		Pin used:	RB2-RB7	a-f cathode segment
;					RA0-RA3	0-4 anode display control
;					RB0		interrupt for menu control
;					RA4		output for rele control
;
;		Schema:	 see my web site 
;    
;		NOTE: on common anode digit light on 0 level
;			  relais is on if RB4 is Off 
;-------------------------------------------------------------------------;
;		COPYRIGHT
;		This software are free. You can freely use at only condition 
;		write the author's name on source code and schema description
;-------------------------------------------------------------------------;
;			PROGRAM 
;-------------------------------------------------------------------------;
        PROCESSOR       16F84A
        RADIX           DEC
        INCLUDE         <P16F84A.INC>
        ERRORLEVEL      -302	; Register in operand not in bank 0.
		ERRORLEVEL 		-207 	; Found label after column 1.
	
        ;Setup of PIC configuration flags
        ;HS oscillator
        ;Disable watch dog timer
        ;Enable power up timer
        ;Disable code protect

        __CONFIG        3FF2H

;-------------------------------------------------------------------------;
; 				Definitions and constants
;-------------------------------------------------------------------------;
; uncomment next line for 4194304 Hz Crystal
;#define 	OSC4194MHZ	1
#define	bank0	bcf	STATUS,RP0 ;bank 0 select
#define	bank1	bsf	STATUS,RP0 ;bank 1 select
;-------------------------------------------------------------------------;
; 			PORTA and PORTB REGISTER BITS 
;-------------------------------------------------------------------------;
	constant BDG0 =	0	; seconds display
	constant BDG1 =	1	; seconds x 10 display
	constant BDG2 =	2	; seconds x 100 display
	constant BDG3 =	3	; status display
	constant BDG4 =	4	; relais port
;-------------------------------------------------------------------------;
; Tick Count = H_TIMRV 
; See follow IK0JRE's equation
; Fosc/4/fxprescaler/256/"tick count" = 1 Sec
;	where
;	Fosc = frequency oscillator in Hz
; 	4	= Tcy Timing (four clock periods)
;	fxprescaler = prescaler division factor 1:256 
;	256 (0xFF + 1)	= TMR0 counting overflow
;	"tick count" = software cycle for counting 1 second but...
;	the value should be 16 for 4194304 xtal and 15,25 for 4000000 xtal but ...
;   for 15,25 you must adjust again for remove 0,25
;-------------------------------------------------------------------------;
#ifndef OSC4194MHZ
	H_TIMRV equ 14   ; oscillator  4000000 Hz theory 15,25
	DLY1S	equ 14
	VXTALADJ equ 252 ; theory 254 but really, for operation delay is 252
#else  				
	H_TIMRV equ 15 ; oscillator  4194304 Hz	theory 16
	DLY1S	equ 15 
#endif
;-------------------------------------------------------------------------;
; 			RSTATUS_COUNT REGISTER BITS 
;-------------------------------------------------------------------------;
BTND 	equ 6 ; control button down 1 = down 
RMENU 	equ 5 ; setup status 0 = off 1 = on
RPAUSE 	equ 4 ; count pause  0 = off 1 = pause on
RONOFF 	equ 3 ; count finish 0 = off 1 = ready for start
;-------------------------------------------------------------------------;
; 				Status defines
;-------------------------------------------------------------------------;
#define	SetFinishCount 	bcf RSTATUS_COUNT,RONOFF
#define SetStartCount	bsf RSTATUS_COUNT,RONOFF
#define SetPauseon 		bsf RSTATUS_COUNT,RPAUSE
#define	SetPauseOff		bcf RSTATUS_COUNT,RPAUSE
#define SetSetupOn		bsf RSTATUS_COUNT,RMENU
#define SetSetupOff		bcf RSTATUS_COUNT,RMENU	 

#define BCountingStatus RSTATUS_COUNT,RONOFF
#define BPauseStatus	RSTATUS_COUNT,RPAUSE
#define BSetupStatus 	RSTATUS_COUNT,SETM

;-------------------------------------------------------------------------;
; 	for standard number use a table at TAB_DISP_A label
;	but for special character status use a defines
;	NOTE: the following defines are used for status
;-------------------------------------------------------------------------;
#define DSSTOP		10	 	; now is equal to ready for counting
#define DSREADY		10	 	; - ready counting 
#define DBHOLD		11		; H button is down
#define DSPAUSE		12	 	; P	counting in pause status
#define DSMENU		13		; F button is down
#define DSOFFLINE	14	 	; display black
;-------------------------------------------------------------------------;
; 	My file registers
;-------------------------------------------------------------------------;
	cblock 0CH
RDIGSTV:		1   ; Display BDG3 value on every refresh main loop 
RCSEC100:		1   ; Display BDG2 value on every refresh main loop 
RCSEC10:		1   ; Display BDG1 value on every refresh main loop 
RCSEC0:			1   ; Display BDG0 value on every refresh main loop 
RTMPREG:		1	; TMP REGISTER FOR USE ONLY INTO A ROUTINE
RMDIGSTV:		1   ; Display BDG3 value on every refresh main loop 
RMCSEC100:		1   ; Display BDG2 value on every refresh main loop 
RMCSEC10:		1   ; Display BDG1 value on every refresh main loop 
RMCSEC0:		1   ; Display BDG0 value on every refresh main loop 
RH_CONT: 		1	; tmp HI var for delayd routine
RL_CONT: 		1	; tmp LO var for delayd routine
RH_TIMR: 		1	; tmp var for swcycle count
RW_TEMP: 		1	; register for WREG backup on IRQ routine
RSTATUS_TEMP:	1	; register for FREG backup on IRQ routine
RSTATUS_COUNT: 	1 	; see next section
RDLY1SLOOP:		1	; tmp var for delay routine
RTMPVAL:		1	; use in setup menu for temporary set 
RXTALADJ:		1	; tmp var for xtal 4Mhz Adj
   endc 


;-------------------------------------------------------------------------;
; 				Macro 
;-------------------------------------------------------------------------;
RelaisOn macro	
		bank0
		bcf PORTA,BDG4
		endm
RelaisOff macro
		bank0
		bsf PORTA,BDG4
		endm
;-------------------------------------------------------------------------;
	ORG H'2100'
	DE 5,4,0,0 ; 45 sec value - default for eeprom data
;	DE 0,6,9,0 ; 240 sec value - default for eeprom data in testing
;-------------------------------------------------------------------------;
	;start routine on reset
	org 00H			;reset vector
	goto INITMAIN

    ;interrupt routine for pic 16f84a
	org 04h			;interrupt vector 
	goto IRVECT
;-------------------------------------------------------------------------;
INITMAIN

	call SettingsPorts	
	clrf RSTATUS_COUNT
    call InitTimer 				; not enable GIE ... it must be in startcount
   	call InitCountVars 			;vars setting but wait press button for start
	RelaisOff

MAIN
    btfss PORTB,7				; test button
	call CountMenu	    		; yes pression start/stop routine
 
	btfsc RSTATUS_COUNT,RMENU 	; menu mode ?	
	call SetupMenu				; yes goto menu mode 

writedispmx
	call WRITE_DISP		 
							 
    goto MAIN

;-------------------------------------------------------------------------;
;	EepromDataWrite
;	write in eeprom that is in RMCSEC0 RMCSEC10 RMCSEC100
; 	it must be call next sets these vars
;-------------------------------------------------------------------------;
EepromDataWrite
	; *** right sequence should be: ***;
	; backup interrupt status		
	; disable interrupt
	; restore interrupt status
	bcf INTCON,GIE		;disable interrupt for write data eeprom
	; write csec0	
	bank0
    movlw 0				;eeprom addr Csec0
	movwf EEADR			;address to read/write
	movf  RMCSEC0,W
    movwf EEDATA   		
   	bank1
	bsf EECON1,WREN		;enable write
	;start write sequence
	movlw	0x55
	movwf 	EECON2
	movlw	0xaa
	movwf	EECON2
	bsf		EECON1,WR 	;write
	nop
	call DELAYD

	; write csec10	
	bank0
    movlw 1				;eeprom addr Csec10
	movwf EEADR			;address to read/write
	movf RMCSEC10,W
    movwf EEDATA   		
	bank1
	bsf EECON1,WREN		;enable write
	;start write sequence
	movlw	0x55
	movwf 	EECON2
	movlw	0xaa
	movwf	EECON2
	bsf		EECON1,WR	;write 
	nop
	call DELAYD

	; write csec100	
	bank0
    movlw 2				;eeprom addr Csec100
	movwf EEADR			;address to read/write
	movf RMCSEC100,W
	movwf EEDATA   
	bank1
	bsf EECON1,WREN		;enable write
	;start write sequence
	movlw	0x55
	movwf 	EECON2
	movlw	0xaa
	movwf	EECON2
	bsf		EECON1,WR	;write 
	nop
	call DELAYD
; end eeprom data write
;verify data next single bye write ?????????????

	return
;-------------------------------------------------------------------------;
; conversion table commons anode whit value. 
; NOTE: PIC can use max 16 values in table
;		BIT 7 is ignored because is an INPUT PORT
;		segment table commons anode 0 = segment on
;-------------------------------------------------------------------------;
TAB_DISP_A	
	andlw 0x0F			; mask don't accept value over 0x0F 
	addwf	PCL,F		; increment PCL register
 	retlw	11000000b 	;0
	retlw	11111001b 	;1				
	retlw 	10100100b 	;2		-0-
	retlw	10110000b 	;3	 5|		|1
	retlw	10011001b 	;4		-6-
	retlw	10010010b 	;5	 4|		|2
	retlw	10000010b 	;6		-3-  .7 (not used in this project)
	retlw	11111000b 	;7
	retlw	10000000b 	;8
	retlw	10010000b 	;9
	retlw	10111111b 	;-  idx 10 counting status or display offline
	retlw	10001001b 	;H 	idx 11 button is hold
	retlw	10001100b 	;P	idx 12 counting in pause status
	retlw	10001110b 	;F	idx 13 for menu
	retlw	11111111b 	;	idx 14 for display off
;-------------------------------------------------------------------------;
; CountMenu ex WaitStatus
; can modify only pause or menu status bits 
;-------------------------------------------------------------------------;
CountMenu
    call DELAY1S 		;wait about 1S

    btfsc PORTB,7     	; the botton is hold ?
    return 			  	; no exit  note: 0 =  btdown
    					; yes verify short or long pressure
	call DELAY10S		; wait about 10S
    
    btfsc PORTB,7     	; the botton is still down ?
    goto shortpush      ; no goto a short pressure routine
						; yes is till down
;	movlw DBHOLD		; print 
;	movwf RDIGSTV       ; character H
;	call WRITE_DISP_ST	; on status display
    
	btfss PORTB,7
	goto $-1			;wait for botton release

    ; here is a long press goto in menu or in reset count mode?
	btfss RSTATUS_COUNT,RONOFF 	; is in counting mode ?
	goto $+4					; no goto setup routine
	call StopCount 				; yes stop count and disable interrupt
	call InitCountVars			; reset count
	return
	SetSetupOn					; use main loop for call setupmenu routine
   	goto lexitwait
    ; ********* short pression ********************* 
shortpush
     ; verify if the system is in running mode    
	btfsc RSTATUS_COUNT,RONOFF	; is running ?
	goto $+3					; yes test pause mode
	call StartCount				; no start the count
    goto lexitwait
   	btfsc RSTATUS_COUNT,RPAUSE  ; running - the system is in PAUSE status ?
	goto  lunsetpause  			; yes unset pause status 
								; no set pause mode
lsetpause
	movlw DSPAUSE				;character P
	movwf RDIGSTV
	RelaisOff
	bsf RSTATUS_COUNT,RPAUSE	; set the pause flag
	bcf INTCON,GIE				;disable interrupt
	goto lexitwait
lunsetpause
	RelaisOn
    bsf INTCON,GIE    		 	; yes restart counting, re-enable interrupt 
    bcf RSTATUS_COUNT,RPAUSE	; and clear the pause flag
lexitwait
	return 
;-------------------------------------------------------------------------;
; Setup time Menu actually is calling from main loop
; if appropriate bit is set in status_count register
;RMCSEC100  Display BDG2 value on every refresh main loop 
;RMCSEC10:  Display BDG1 value on every refresh main loop 
;RMCSEC0:   Display BDG0 value on every refresh main loop 
;-------------------------------------------------------------------------;
SetupMenu
 ; must disable interrupt
	SetMenuOn		;redundant call but can to be right for a logic operation
	call StopCount
	call InitCountVars
	; now must read eeprom data for setting default display
	; set the data
	movlw DSMENU     ;status and display ready for start
   	movwf RMDIGSTV

    movlw DSREADY	;DSOFFLINE
	movwf RMCSEC10 ; blank
	movwf RMCSEC100 ; blank

setdisplay0    
 	movf RCSEC0,W
	movwf RMCSEC0 

waitdisp0
	call WRITE_DISPM
	btfsc PORTB,7		; attendo la pressione di un tasto
    goto $-2			;attendo una qualsiasi pressione
    
    call DELAY1S 		;è premuto quindi attendo per un rimbalzo
	btfsc PORTB,7     	; the botton is hold ?
	goto waitdisp0 	    ; no goto in wait
	call DELAY10S		; wait about 10S
    btfsc PORTB,7     	; the botton is still down ?
	goto decdisp0      	; no goto a short pressure routine
memdisp0				; long pressure
	btfss PORTB,7
	goto $-1			; wait for botton release
    goto setdisplay10 	; yes exit
decdisp0
    movf RMCSEC0,W
    btfss STATUS,Z     	
    goto decrmcsec1
	movlw 9
	movwf RMCSEC0
	goto waitdisp0
decrmcsec1    
	decf RMCSEC0,F
	goto waitdisp0
	;*********** next display

setdisplay10    
 	movf RCSEC10,W
	movwf RMCSEC10 
waitdisp10
	call WRITE_DISPM
	btfsc PORTB,7		; attendo la pressione di un tasto
    goto $-2			; attendo una qualsiasi pressione

    call DELAY1S 		;è premuto quindi attendo per un rimbalzo
	btfsc PORTB,7     	; the botton is hold ?
	goto waitdisp10 	    ; no goto in wait
	call DELAY10S		; wait about 10S
    btfsc PORTB,7     	; the botton is still down ?
	goto decdisp10      	; no goto a short pressure routine
memdisp1				; long pressure
	btfss PORTB,7
	goto $-1			;wait for botton release
    goto setdisplay100	; si esci
decdisp10
    movf RMCSEC10,W
    btfss STATUS,Z     	
    goto decrmcsec10
	movlw 9
	movwf RMCSEC10
	goto waitdisp10
decrmcsec10    
	decf RMCSEC10,F
	goto waitdisp10

;*********** next display 100
setdisplay100    
 	movf RCSEC100,W
	movwf RMCSEC100 
waitdisp100
	call WRITE_DISPM
	btfsc PORTB,7		; attendo la pressione di un tasto
    goto $-2			;attendo una qualsiasi pressione

    call DELAY1S 		;è premuto quindi attendo per un rimbalzo
	btfsc PORTB,7     	; the botton is hold ?
	goto waitdisp100 	    ; no goto in wait
	call DELAY10S		; wait about 10S
    btfsc PORTB,7     	; the botton is still down ?
	goto decdisp100      	; no goto a short pressure routine
memdisp10				; long pressure
	btfss PORTB,7
	goto $-1			;wait for botton release
    goto ExitMenu	; si esci
decdisp100
    movf RMCSEC100,W
    btfss STATUS,Z     	
    goto decrmcsec100
	movlw 9
	movwf RMCSEC100
	goto waitdisp100
decrmcsec100    
	decf RMCSEC100,F
	goto waitdisp100

ExitMenu
;;; clrf RSTATUS_COUNT ; only for debug
;re-setting variables
	call EepromDataWrite
    call InitCountVars ;vars setting but wait press button for start
;routine menu is done
	SetSetupOff
	return
;-------------------------------------------------------------------------;
; StartCount
;-------------------------------------------------------------------------;
StartCount
	call InitCountVars		
   	bsf RSTATUS_COUNT,RONOFF	;global status in start
    clrf TMR0
	RelaisOn				;Relais on
	bsf INTCON,GIE    		;enable interrupt
	return
;-------------------------------------------------------------------------;
; StopCount
;-------------------------------------------------------------------------;
StopCount
	RelaisOff				;Relais off
	bcf INTCON,GIE    		;disable interrupt
    movlw DSSTOP     		;status and display stop
    movwf RDIGSTV
	bcf RSTATUS_COUNT,RONOFF	;global status in stop
	bcf RSTATUS_COUNT,RPAUSE ;   
	return      
;-------------------------------------------------------------------------;
; InitTimer InIT IRQ variables and settings
; NOTE: TMR0 is used for delays button routine 
;-------------------------------------------------------------------------;
InitTimer
    bank0
	clrf TMR0
	clrf INTCON
	bank1
    clrf OPTION_REG
	;set prescaler to 1:256 111b PS2 PS1 PS0
    bsf OPTION_REG,0
	bsf OPTION_REG,1
	bsf OPTION_REG,2
    bank0
    bsf INTCON, T0IE
	;bsf INTCON, GIE
	return
;-------------------------------------------------------------------------;
; InitCountVars
;-------------------------------------------------------------------------;
InitCountVars
    clrf RH_TIMR
	 
	movlw H_TIMRV
	movwf RH_TIMR

	#ifndef OSC4194MHZ
		clrf	RXTALADJ
	#endif

	movlw DSREADY	;DSRSTART     ;status and display ready for start
	movwf RDIGSTV

    ; load data from eeprom
    bank0
	movlw 0			;eeprom addr Csec10
	movwf EEADR		;address to read
	bank1
	bsf EECON1,RD	;eeprom read
	bank0
	movf EEDATA,W   ;W = EEDATA
    movwf RCSEC0
	
	bank0
    movlw 1			;eeprom addr Csec10
	movwf EEADR		;address to read
	bank1
	bsf EECON1,RD	;eeprom read
	bank0
	movf EEDATA,W   ;W = EEDATA
    movwf RCSEC10
   
	bank0
    movlw 2			;eeprom addr Csec0
	movwf EEADR		;address to read
	bank1
	bsf EECON1,RD	;eeprom read
	bank0
	movf EEDATA,W   ;W = EEDATA
    movwf RCSEC100
	
   	return
;-------------------------------------------------------------------------;
; Settings Ports
;-------------------------------------------------------------------------;
SettingsPorts
    ;general setting
	bank1
    bsf OPTION_REG,7 	; enable pull'up
    movlw 0
	movwf TRISA			; clear TRISA register
	movwf TRISB			; clear TRISB register
    bsf TRISB,7 		; rb7 input for a button
   	RelaisOff
	return
;-------------------------------------------------------------------------;
; UPTIMES 
; update time set, here must set Stop Count calling StopCount routine
;-------------------------------------------------------------------------;
UPTIMES
    	; 1 TCY sec = freqclock/4 clock cycle 4Mhz
		; prescaler set to 256 clock division
		; about 1 Sec. = (FOSC/4/fxPrescaler/VXTALADJ/RH_TIMR) - (istructions TCY)


       ;theory: RH_TIMR cicli = 1 Sec     
        movf RH_TIMR,W
        btfss STATUS,Z    		;è zero
		goto dechtmrv			;no

        movlw H_TIMRV     		;si reassign original value
		movwf RH_TIMR
        ; one second is gone blinking display        
		movlw DSREADY 
		subwf RDIGSTV,W
        btfss STATUS,Z   	; il display acceso ?
        goto dispoff        ; no e' spento lo accendo	
		movlw DSOFFLINE  	; e' acceso lo spengo
		movwf RDIGSTV
		goto updatet 
dispoff
       	movlw DSREADY 
		movwf RDIGSTV

		;count down routine		

updatet    	
	   ; update first digit RCSEC0 var
        movf RCSEC0,F      		; 
        btfss STATUS,Z    		; is seconds var zero ?
        goto decsec0      		; is not ZERO, dec seconds 
								; yes is ZERO test next var
        movf RCSEC10,F      	; 
        btfss STATUS,Z     		; is secondsx10 var zero ?    
        goto decsec10     		; is not ZERO dec secondsx10 
						   		; yes is ZERO test next var
  		movf RCSEC100,F        	; 
        btfss STATUS,Z     		; is secondsx10 var zero ?    
        goto decsec100			; is not ZERO dec secondx100
    	return					; return
     
    dechtmrv
        decf RH_TIMR,F
		return  
    decsec0
		decf RCSEC0,F
        btfss STATUS,Z    		
        return
        movf RCSEC10,F      	 
        btfss STATUS,Z     		   
        return
        movf RCSEC100,F        	
        btfss STATUS,Z     		     
        return
        call StopCount
		call InitCountVars
		return   
    decsec10
		decf RCSEC10,F
		movlw 9 			
    	movwf RCSEC0
 		return   
    decsec100
		decf RCSEC100,F
		movlw 9 			
    	movwf RCSEC0
        movlw 9 			
    	movwf RCSEC10
	return   

;-------------------------------------------------------------------------;
WRITE_DISP
	;multiplex display
    ; must call a routine return digit per digit the time
    bsf PORTA,BDG3
	movf RDIGSTV,W 
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG3

   	bsf PORTA,BDG2
	movf RCSEC100,W
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG2

   	bsf PORTA,BDG1
	movf RCSEC10,W
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG1

   	bsf PORTA,BDG0
    movf RCSEC0,W 
    call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG0

	return
;-------------------------------------------------------------------------;
;	Special function for write display on setup menu
;-------------------------------------------------------------------------;
WRITE_DISPM
	;multiplex display
    ; must call a routine return digit per digit the time
    bsf PORTA,BDG3
	movf RMDIGSTV,W 
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG3

   	bsf PORTA,BDG2
	movf RMCSEC100,W
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG2

   	bsf PORTA,BDG1
	movf RMCSEC10,W
	call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG1

   	bsf PORTA,BDG0
    movf RMCSEC0,W 
    call TAB_DISP_A
	MOVWF PORTB
    call DELAYD
   	bcf PORTA,BDG0

	return
;-------------------------------------------------------------------------;
DELAYD
	movlw	5 
	movwf	RH_CONT
	clrf	RL_CONT
	decfsz	RL_CONT,F
	goto	$-1
	decfsz	RH_CONT,F
	goto	$-3
	return
;-------------------------------------------------------------------------;
DELAY1S
	movlw	DLY1S
    movwf  	RDLY1SLOOP
LOOP1
	decfsz RDLY1SLOOP,F	;decrement
    goto LOOP2
    return
LOOP2    
    movf TMR0,W
    btfss STATUS,Z		;is ZERO
	goto $-2			;no wait
	goto LOOP1
	
  	return
;-------------------------------------------------------------------------;
DELAY10S
	movlw	DLY1S * 10
    movwf  RDLY1SLOOP
LOOP10S
	decfsz RDLY1SLOOP,F	;decrement
    goto LOOP20S
    return
LOOP20S    
    movf TMR0,W
    btfss STATUS,Z		;is ZERO
	goto $-2			;no wait
	goto LOOP10S
	
  return
;-------------------------------------------------------------------------;
;	Interrupt vector routine
;-------------------------------------------------------------------------;
IRVECT
   btfss INTCON,T0IF   		; is it overflow TMR0 interrupt?
   RETFIE 					; no exit from routine
   
	movwf	RW_TEMP			; saving Wreg
	swapf	STATUS,W 		; saving STATUS REG in 2 steps
	movwf	RSTATUS_TEMP 	; lower and hi nibble are swapped 
	; if was a stop, pause,setup status why call other routines ?
#ifndef OSC4194MHZ
    ; se settato salta
    btfsc RXTALADJ,0
	goto  noxtaladj

    nop
	nop
	nop	
	nop
	movlw VXTALADJ ;252 
	movwf TMR0
	bsf RXTALADJ,0
	goto leaveint
noxtaladj 
 
   bcf RXTALADJ,0
#endif


   ; movlw  0001000b ;0x38 	start e pause ; rivedere ? *****************
   ; andwf RSTATUS_COUNT,W	; running an masked event?
   ; btfss STATUS,Z			; yes skip uptimes
	btfsc RSTATUS_COUNT,3	; running an masked event?

    call UPTIMES			; yes masked events call uptimes routine  
							; no uptime routine is required 
leaveint
	bcf INTCON,T0IF 		; clear overflow bit

	swapf RSTATUS_TEMP,W
	movfw	STATUS			;restore status reg
	swapf	RW_TEMP,F
	swapf	RW_TEMP,W		;restore wreg

	RETFIE
;-------------------------------------------------------------------------;
	END

