;ledsrun.asm 
;example program with PIC16F84 µcontroller
;connect four leds with a 470 ohm series resistor to RB0 TO 3
;and a 10k / 27pF RC-oscillator
;the LEDS will light up as a running light
;written in MPASM MICROCHIP assembler
;****set up constans and variables****
	INCLUDE "P16F84.INC" 			;look up file for MPASM
	LIST	P=16F84
	ERRORLEVEL      -302    		;SUPPRESS BANK SELECTION MESSAGES
	__config  _RC_OSC&_PWRTE_ON & _WDT_OFF	;RC oscillator, power on timer 
						;and watchdog timer off

status	equ	h'03'		;status register
trisb	equ	h'86'		;set up register for port B
trisa	equ	h'85'		;set up register for port B
count1	equ	h'0e'		;general purpose register
count2	equ	h'0f'		;general purpose register
leds	equ	h'06'		;portB is 7seg display content
porta	equ	h'05'		;portA is not used

;****set up portA and portB****
	bsf	status,5	;switch to bank1 to set up ports
	movlw	h'00'
	movwf	trisb		;portB is all output
	movlw	h'00'
	movwf	trisa		;portA is all output
	bcf	status,5	;switch back to bank0 to use ports

;****main program****
start	movlw	b'00000001'	;load pattern into W
	movwf	leds		;load W into portB (switch on rightmost LED)
	call	delay		;wait for some time 
	movlw	b'00000010'	;new pattern
	movwf	leds		
	call	delay		;wait
	movlw	b'00000100'	;new pattern
	movwf	leds		;
	call	delay		;wait
	movlw	b'00001000'	;new pattern
	movwf	leds		;
	call	delay		;wait
	goto	start		;do it all over again
;****delay subroutine****
;****delay = wait (delay count value 1) x (delay count value 2) cycles****
delay	movlw	h'EE'		;setup delay count value 1
	movwf	count1		;count1 is slow counter	
loop1	decfsz	count1,1	;
	goto	label		;still > 0
	return			;count1 < 0 so get out of the delay loop
label	movlw	h'EE'		;setup delay count value 2
	movwf	count2		;count2 is fast counter inside count1 loop
loop2	decfsz	count2,1	;
	goto	loop2		;still > 0 so stay in loop2
	goto	loop1		;count1 < 0 go to loop1
	end;	

