; TUTA26.ASM 27SEP07
; Use of Timer with BCD counting on 7-segment (single)

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
; Configuration data 
; PICmicro MCU type: 16F1937 
; Oscillator: XTAL mode, FAST, VR1 your choice of setting
; LCD display: off 
; 7-segment display: on 
; Version 3 board settings: J14 links: Digital 
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;
; The following lines embed configuration data into the PICmicro
    list		p=16f1937      ; list directive to define processor
	__CONFIG H'8007', H'FE2'       ; HS mode
	__CONFIG H'8008', H'1EFF' 
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

#DEFINE	PAGE0	MOVLB 0
#DEFINE	PAGE1	MOVLB 1
				
OPSHUN	EQU	H'95'		;
PCL	EQU	H'02'		;
STATUS	EQU	H'03'		;
PORTA	EQU	H'0C'		;
TRISA	EQU	H'8C'		;
PORTB	EQU	H'0D'		;
TRISB	EQU	H'8D'		;
INTCON	EQU	H'0B'		;

W	EQU	0		;
F	EQU	1		;
C	EQU	0		;
DC	EQU	1		;
				
CLKCNT	EQU	H'20'		;clock division counter
CLKSEC	EQU	H'21'		;seconds counter
				
	ORG	0		; Reset vector
	GOTO	5		; Goto start of program
	ORG	4		; Interrupt vector
	GOTO	5		; Goto start of program
	ORG	5		; Start of program memory

	CLRF	PORTA		;
	CLRF	PORTB		;
	PAGE1			; PAGE1
	MOVLW	0x07
	MOVWF	H'9E'		; Setup the ADCON1 register
    BCF	    TRISA,0
	CLRF	TRISB		;Port B0-B7 as output
	MOVLW	B'00000110'	;set timer ratio 1:128 (TMRO rate)
	MOVWF	OPSHUN		
	PAGE0			; PAGE0		
	BSF	PORTA,0		;turn on TR1 (see text)
	MOVLW	D'150'		
	MOVWF	CLKCNT		
	CLRF	CLKSEC		
	BCF	INTCON,2		
	GOTO	INTRPT		

TABLE	ADDWF	PCL,F		;add to PCL
	RETLW	B'11000000'	;0	common anode codes
	RETLW	B'11111001'	;1
	RETLW	B'10100100'	;2
	RETLW	B'10110000'	;3
	RETLW	B'10011001'	;4
	RETLW	B'10010010'	;5
	RETLW	B'10000011'	;6
	RETLW	B'11111000'	;7
	RETLW	B'10000000'	;8
	RETLW	B'10011000'	;9

INTRPT	BTFSS	INTCON,2		;has a timer time-out been detected?
	GOTO	INTRPT		;no
	BCF	INTCON,2		;yes
	DECFSZ	CLKCNT,F		;dec clock division counter, is it zero?
	GOTO	INTRPT		;no
	MOVLW	D'150'		;yes, reset CLKCNT
	MOVWF	CLKCNT		
	INCF	CLKSEC,F		;inc count
	MOVF	CLKSEC,W		;check units >9
	ADDLW	H'06'		
	BTFSC	STATUS,DC		;is there a digit carry?
	CLRF	CLKSEC		;yes, clear counter
OUTPUT	MOVF	CLKSEC,W		;get count val
	ANDLW	H'0F'		;AND for LS nibble
	CALL	TABLE		;get 7-seg code for val
	MOVWF	PORTB		;output to display
	GOTO	INTRPT		;repeat for next

	END			;final line
