; uncomment following two lines if using 16f627 or 16f628. config uses internal oscillator
	LIST	p=16F627		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings (oscillator type etc.)



;	list	p=16f84a
;	__config h'3ff1'





	radix	hex
pc			equ	0x02
porta		equ	0x05
portb		equ	0x06


cblock 	0x20 			;start of general purpose registers
		count1 			;used in delay routine
		counta 			;used in delay routine 
		countb 			;used in delay routine
		count_low
		count_hi
		temp
	endc

counter		equ	0x10
tempw		equ	0x11
tempit		equ	0x012
qqq			equ	0x013
	org	0x000

;un-comment the following two lines if using 16f627 or 16f628

	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)



	movlw	0x01
	tris	portb
	tris	porta
	clrf	count_low
	clrf	count_hi	


; simple loop here. display a number and increment
topit	
	call	display_it
	incf	count_low,f
	btfsc	STATUS,0
	incf	count_hi,f
	goto topit


display_it
	movlw	0xff
	movf	temp
more
	movf	count_low,0
	andlw	0x0f
    call	seg_display
	movwf	portb			; turn on transistor 1
	bsf		porta,0
	call	Delay5
	bcf		porta,0		; turn off transistor 1
	bsf		porta,1		; turn on transistor 2
	call	Delay5
	bcf		porta,1		; turn off transistor 2
	bsf		porta,2		; you get the idea
	call	Delay5
	bcf		porta,2
	bsf		porta,3
	call	Delay5
	bcf		porta,3
	decfsz	temp
	goto more

	ret


; modified Delay routine, direct calls for specified times
; or load W and call Delay for a custom time.Taken from some Brit named Nigel 
; http://www.winpicprog.co.uk/pic_tutorial2.htm

Delay0	retlw	0x00			;delay 0mS - return immediately
Delay1	movlw	d'1'			;delay 1mS
	goto	Delay
Delay5	movlw	d'5'			;delay 5mS
	goto	Delay
Delay10	movlw	d'10'			;delay 10mS
	goto	Delay
Delay20	movlw	d'20'			;delay 20mS
	goto	Delay
Delay50	movlw	d'50'			;delay 50mS
	goto	Delay
Delay100	movlw	d'100'			;delay 100mS
	goto	Delay
Delay250	movlw	d'250'			;delay 250 ms
Delay	movwf	count1
d1	movlw	0xC7			;delay 1mS
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00

; 7 segment display including hex digits
;  wiring problems:  interchange bit 0 and 7
;                    that is, b7<- b0 to make b0 available for interrupts
seg_display
	addwf	pc,f
	retlw	0xbf		;0x3f
	retlw	0x06
	retlw	0xdb
	retlw	0xcf
	retlw	0x66	;4
	retlw	0xed
	retlw	0xfd	;6
	retlw	0x87
	retlw	0xff
	retlw	0xef	;9
	retlw	0xf7	;A
	retlw	0x7c	;b
	retlw	0x58	;c
	retlw	0x5e	;d
	retlw	0xf9	;e
	retlw	0xf1	;f

	end