; FILE: 		count.asm
; AUTHOR: 		JMY
; DATE: 		24th March 2005
; DESCRIPTION: 	Count to 255 at 1 second intervals and display on a seven 
; 				segment display in binary. 
; 				The pause subroutine takes apx. 0.2 sec.
; 				Pause is called 5 times

	list	p=16F84		; select processor type
	radix	hex			; define radix as hexadecimal
;	#include "p16f84.inc"
;----------------------------------------------------------------------
;	cpu equates (memory map)
portB	equ	0x06		;port B address
trisb	equ 0x06		;trisB address in register bank 1

Status	equ 0x03		;status register
RP0		equ 5			;bit 5 of status register
RP1 	equ	6			;bit 6 of status register

count	equ	0x0c		; counter 0 to 255
nCount	equ	0x0d		; pause routine inner loop counter
mCount	equ	0x0e		; pause routine outer loop counter

;----------------------------------------------------------------------

	org	0x000

start
	bcf		status,RP0	; clear bank selection bits in the status reg
	bcf		status,RP1

	bsf		status,RP0	; select bank 1
	movlw	0x00		; load w with 0x00
	movwf	trisb		; load it into trisb to make portb all outputs
	bcf		status,RP0	; reselect bank 0

	clrf	portB		; clear all lines low
	clrf	count		; initialize counter to 0

get_counnt	
	movf	count, w	; move count to W


	movwf	portB		; move W to port B to update the display

	addlw	1			; add +1 to W

	movwf	count		; save W in count

	call	pause		; delay 200ms by subroutine
	call	pause
	call	pause
	call	pause
	call	pause		; five pause executions equals ~ 1 second

	goto	get_count		; repeat forever

;--------------------------------------------------------------------
;subroutine pause: lets waste some time....
;
;inputs: nothing
;outputs: nothing
;
;does: nothing but waste some time
;corrupts: register W
;
;description: two loops using "mCount" and "nCount" register files
;			  "mCount" is the outer loop counter (255 down to zero)
;			  "nCount" is the inner loop counter (255 down to zero)
;
;--------------------------------------------------------------------
pause	
	movlw	0xfff		; set w = 255 decimal
	movwf	mCont		; mCount = w: outer loop counter
loadN
	movlw	0xff		; set w = 255 decimal
	movwf	nCount		; nCount = w: inner loop counter
decN
	decfsz	nCount, f	; nCount--
	goto	decN		; if nCount != 0 then repeat nCount--

	decfsz	mCount, f	; else decrement Count
	goto	loadN		; if mCount != 0 then 
						; reload nCount to 255 and decrement
	return				; else exit subroutine


	en d
;----------------------------------------------------------------------
; Configuration! Important!
;
; at blast time, select:
;	memory uprotected
;	watchdog timer disabled
;	standard crystal (4 MHz)
;	power-up timer on

