; 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
portA   equ 0x05		;port A address
trisa	equ 0x05		;trisA 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
	movlw   0x0f		; value used to initialize data direction
	movwf   trisa		; load it into trisa to make port a 0-3 inputs
	bcf		status,RP0	; reselect bank 0

	clrf	portB		; clear all lines low
	clrf    portA		; clear all lines low
	clrf	count		; initialize counter to 0

get_count	
	bsf     portB,4
	btfss   portA,0
	bcf     portB,4
	bsf     portB,5
	bsf     portB,1
	call	pause		; delay 200ms by subroutine
	call	pause
	call	pause		; five pause executions equals ~ 1 second
	bcf     portB,1
	call	pause		; delay 200ms by subroutine
	call	pause
	call	pause		; five pause executions equals ~ 1 second
	bsf     portB,3
	call	pause2		; delay 200ms by subroutine
	call	pause2
	call	pause2		; five pause executions equals ~ 1 second
	bcf     portB,3
	call	pause2		; delay 200ms by subroutine
	call	pause2
	call	pause2
	
	


	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	0x22		; set w = 255 decimal
	movwf	mCount		; mCount = w: outer loop counter
loadN
	movlw	0x22		; 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
pause2	
	movlw	0x28		; set w = 255 decimal
	movwf	mCount		; mCount = w: outer loop counter
load2N
	movlw	0x25 ;set w = 255 decimal
	movwf	nCount		; nCount = w: inner loop counter
dec2N
	decfsz	nCount, f	; nCount--
	goto	dec2N		; if nCount != 0 then repeat nCount--

	decfsz	mCount, f	; else decrement Count
	goto	load2N		; if mCount != 0 then 
						; reload nCount to 255 and decrement
	return				; else exit subroutine


	end
;----------------------------------------------------------------------
; Configuration! Important!
;
; at blast time, select:
;	memory uprotected
;	watchdog timer disabled
;	standard crystal (4 MHz)
;	power-up timer on

