*********************************************
* Simple 68HC05 Program Example             *
* Read state of switch connected to bit-7   *
* of port B.  When sw. closes, light LED    *
* connected to port C bit-6 for about 1 sec *
* Wait for sw release, then repeat.         *
*                                           *
* Sw closure causes bit-7 port B to go high *
* LED on when bit-6 port C is low (0)       *
* Provides sw debounce time of about 50 mS  *
* for sw closure and/or release             *
*********************************************

PORTB	EQU	$01	Direct address of port B (sw)
PORTC	EQU	$02	Direct address of port C (LED)
DDRB	EQU	$05	Data direction control, port B
DDRC	EQU	$06	Data direction control, port C
TEMP1	EQU	$9F	One byte temp storage location

	ORG	$A0	Program will start at $00A0
* $00A0 is in '705C8 RAM so program could be loaded & run
	
INIT	LDA	#$FF	Begin initialization
	STA	DDRC	Set port C to act as outputs
* Port B is configured as inputs by default from reset.
	LDA	#$E0	Red & green LEDs and beeper off
	STA	PORTC	Turn off red LED
* Some pins of port C (of my board) happen to be connected
* to devices which don't apply to this example program.
* The $E0 pattern turns off my stuff & turns off red LED

TOP	LDA	PORTB	Read sw at MSB of Port B
	BPL	TOP	Loop till MSB=1 (Neg trick)
	JSR	DLY50	Delay about 50 mS to debounce
	BCLR	6,PORTC	Turn on LED (bit-6 to zero)
	LDA	#20	Decimal 20 assembles to $14
DLYLP	JSR	DLY50	Delay 50 mS
	DECA		Loop counter for 20 loops
	BNE	DLYLP	20 times (20-19,19-18,.1-0)
	BSET	6,PORTC	Turn LED back off
OFFLP	BRSET	7,PORTB,OFFLP	Loop here till sw off
	JSR	DLY50	Debounce release
	BRA	TOP	Look for next sw closure

***
* DLY50 - Subroutine to delay ~50mS
* Save original accumulator value
* but X will always be zero on return
***

DLY50	STA	TEMP1	Save accumulator in RAM
	LDA	#32	Do outer loop 32 times
OUTLP	CLRX		X used as inner loop count
INNRLP	DECX		0-FF, FF-FE,...1-0 256 loops
	BNE	INNRLP	6cyc*256*1S/cyc = 1.536mS
	DECA		32-31, 31-30,...1-0
	BNE	OUTLP	1545cyc*32*1S/cyc=49.440mS
	LDA	TEMP1	Recover saved Accumulator val
	RTS		** Return **
