*********************************************
* Simple 68HC05 SPI Program Example         *
*********************************************

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
SPCR	EQU	$0A	SPIE,SPE,-,MSTR;CPOL,CPHA,SPR1,SPR0
SPSR	EQU	$0B	SPIF,WCOL,-,MODF;-,-,-,-
SPDR	EQU	$0C	SPI Data Register

SPIVAL	EQU	$9E	One byte RAM storage location
TEMP1	EQU	$9F	One byte temp storage location

	ORG	$250	Program will start at $0250

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	#$E8	Red & grn LED & beep off, SPI EN off
	STA	PORTC	Turn off red LED
* Some pins of port C (my board) happen to be connected
* to devices which don't apply to this example program.
* The $E8 pattern turns off my stuff & turns off red LED
	CLR	SPIVAL	Start with 0
	LDA	#%01010000  SPE, MSTR, norm lo fast clk
	STA	SPCR	Initialize SPI control reg

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	3,PORTC	Drive select of 74HC595 low
	LDA	SPIVAL	Current data to send to SPI
	STA	SPDR	Send SPI data
	INC	SPIVAL	Add one to current SPI value
HERE	BRCLR	7,SPSR,HERE	Wait for SPIF to set
	BSET	3,PORTC	Drive select of 74HC595 hi

	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 save Accumulator val
	RTS		** Return **
