Microcontroller, 68HC11A1, 68HC11, 68HC811, HC11...
Orgler electronic
"Applications with microcontroller HC11"


sci.GIF SPI and SCI on PORTD of HC11

spisend1.gif The HC11 sends in every loop (300microsec) the charackter 'S'= 0x53
******************* SAMPLE SPI SEND PROGRAMM ********
PORTD   EQU $1008
DDRD    EQU $1009

TFLG1   EQU $1023
TOC3    EQU $101A

SPCR    EQU $1028
SPSR    EQU $1029
SPDR    EQU $102A

BIT5            EQU    00100000B

		ORG     $E000

START          	LDS	#$00FF

*=============== INIT SPI ====================
                ldaa    DDRD
		oraa    #00111000B
		staa    DDRD          set data direction

		ldaa    #01110011B    SPI ON
		staa    SPCR
**********************************************
SYSTEM_LOOP     ldaa	#'S'
                staa	SPDR

wait_spi        ldaa	SPSR         Data transfer finished ?
                bpl	wait_spi

                jsr	REALTIME
                JMP	SYSTEM_LOOP

*-----------------------------------------------------
REALTIME        LDAA 	#BIT5 		wait for Real Time
WAIT_REAL	BITA 	TFLG1
		BEQ     WAIT_REAL
	 	STAA	TFLG1  		reset TOC3
*		----------------------
		LDD	TOC3
		ADDD	#600             300 ęsec
		STD	TOC3
                rts
*-----------------------------------------------

		ORG 	$FFFE
                FDB	START
*==================================================

External EEPROM connected over SPI with 68HC11

SPI

Example program 68HC11 with SPI to external EEPROM
Usage:
set : JSR InitSpi in your INIT-Routine
Set one of the following routines in your loop
JSR LoadFromEEprom
JSR SaveToEEprom
In a Output Compare Interrupt routine with a 200 mikrosec.
period I have the following code:
ldx #$1000 Register Base Address
ldy Spi_Task
jsr Y
The routines are tested with hardware
68hc11 in expanded mode and the BYTERUNNER
*SPI_Address   RMB 2
*SPI_TimeOut   RMB 1
*SPI_Task      RMB 2
*SPI_pointer   RMB 2
*SPI_nrbytes   RMB 1
*--------------------------------------------
portd	EQU	$08
spcr    EQU 	$28
spsr    EQU 	$29
spdr    EQU 	$2A
*--------------------------------------------
InitSPI         ldaa    DDRD
		oraa    #00111000B
		staa    DDRD

		ldaa    #00110011B
		staa    SPCR

		ldaa    PORTD
		oraa    #BIT4+BIT3
		staa    PORTD

		ldd     #SPIvoidTask
		std     SPI_Task
		rts
****************************************
SaveToEEprom    ldd     #0
		std     SPI_Address
		ldaa    #64
		staa    SPI_nrbytes     64 Bytes
		ldd     #SPI_TEXT_DATA  TEXT-BLOCK in EPROM
		std     SPI_pointer
		ldd     #SendSPI_Task
		std     SPI_Task
		rts
*------------------------------------------------------------
LoadFromEEprom  ldd     #0
		std     SPI_Address
		ldaa    #64
		staa    SPI_nrbytes    64 Bytes
		ldd     #RamAddress
		std     SPI_pointer
		ldd     #LoadSPI_Task
		std     SPI_Task
		rts
*------------------------------------------------------------

*************************************************************
*                   R E A D  - T A S K S                    *
*************************************************************
************** Wait until device is ready
Slave_Busy	bclr	portd,X,BIT4   		Ack - Falling edge
		ldd     #LoadSPI_Task
		std     SPI_Task
		bset    portd,X,BIT4+BIT3
		rts
*************************************************************
*************************************************************
LoadSPI_Task    bclr	portd,X,BIT3    Start=Falling Edge
		ldd     #SPI__Add0      delay min 5 uSec
		std     SPI_Task
		clr     SPI_TimeOut
TURN_ON_SPI     bset	spcr,X,BIT6     SPI ON !
		ldab    #10100000B      CONTROL-BYTE[write]
		stab    SPDR
		rts
*               ------------------------
SPI__Add0       brclr	spsr,X,BIT7,spi_rts

		bclr	spcr,X,11000000B     SPI-Off
		bset	portd,X,BIT4      Ack - Rising edge

		ldab    PORTD
		bitb    #BIT2
		bne     Slave_Busy

		ldd     #SPI__Add1
		std     SPI_Task

		bclr	portd,X,BIT4   	  Ack - Falling edge

		bset	spcr,X,BIT6     SPI ON !
		ldaa    SPI_Address
		anda    #00011111B
		staa    SPDR
		clr     SPI_TimeOut
		rts
***************************************
SPI__Add1       brclr	spsr,X,BIT7,spi_rts
		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - Rising edge
		bitb    #BIT2
		bne     nack

		ldd     #SPI_StartRead
		std     SPI_Task
		bclr	portd,X,BIT4      Ack - falling edge
		bset	spcr,X,BIT6     SPI ON !

		ldaa    SPI_Address+1
		staa    SPDR
		clr     SPI_TimeOut
		rts
***************************************
SPI_StartRead   brclr	spsr,X,BIT7,spi_rts
		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD         Ack - Rising edge
		bitb    #BIT2
		bne     nack
		bra	XXX

spi_rts         inc     SPI_TimeOut
		ldaa    SPI_TimeOut
		cmpa    #100
		bls     spi__rts
		jmp     SPI_TIMEOUT
spi__rts	rts

XXX		orab    #BIT3
		andb    #11101111B     Ack - falling edge
		stab    PORTD

		ldx     #SPI_ControlRead
		stx     SPI_Task

		orab    #BIT4
		stab    PORTD
		rts
************************************************************
SPI_ControlRead bclr	portd,X,BIT3    Start=Falling Edge
		ldd     #SPI_TestRead    delay  min 5 uSec
		std     SPI_Task
		clr     SPI_TimeOut
		bset	spcr,X,BIT6     SPI ON
		ldab    #10100001B      CONTROL-BYTE[read]
		stab    SPDR
		rts
***************************************
nack    	ldaa    DDRD
		oraa    #BIT3
		staa    DDRD
		ldd     #SPIvoidTask
		std     SPI_Task
		rts
***************************************
SPI_TestRead    brclr	spsr,X,BIT7,spi_rts      RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - Rising edge
		bitb    #BIT2
		bne     nack

		ldx     #SPI_Read
		stx     SPI_Task

		andb    #11101111B  Ack - falling edge
		stab    PORTD

		oraa    #BIT6
		staa    SPCR          SPI-On

		ldaa    #$FF
		staa    SPDR
		rts
***************************************
SPI_Read        brclr	spsr,X,BIT7,spi_rts      RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - Rising edge
		bitb    #BIT2
		bne     nack

		clr     SPI_TimeOut
		ldx     SPI_pointer

		andb    #11101111B  Ack - falling edge
		stab    PORTD

		ldab    SPDR
		stab    X
		inx
		stx     SPI_pointer

		ldab    SPI_nrbytes
		decb
		ble     Read_exit       
		stab    SPI_nrbytes

		oraa    #BIT6
		staa    SPCR          SPI-On

		ldaa    #$FF
		staa    SPDR
		rts
***************************************
Read_exit  	oraa    #BIT6
		staa    SPCR          SPI-On

		ldd     #SPI_EXIT
		std     SPI_Task

		ldaa    PORTD
		oraa    #BIT3
		staa    PORTD          generate  NACK

		ldaa    #$FF
		staa    SPDR
		rts
*************************************************************
SPIvoidTask	rts
*************************************************************
*                   S A V E - T A S K S                     *
*************************************************************
SendSPI_Task    ldaa    PORTD
		anda    #11110111B      START Ramp
		staa    PORTD

		ldd     #SPI_Add0       delay min 5 uSec
		std     SPI_Task
		clr     SPI_TimeOut

		ldaa    SPCR
		oraa    #BIT6
		staa    SPCR            SPI On!

		ldab    #10100000B      CONTROL-BYTE[write]
		stab    SPDR
		rts
*               ------------------------
SPI_Add0        ldab    SPSR
		bpl     SPI_RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - rising edge
		bitb    #BIT2
		bne     SlaveBusy

		ldx     #SPI_Add1
		stx     SPI_Task

		andb    #11101111B  Ack - falling edge
		stab    PORTD

		oraa    #BIT6
		staa    SPCR          SPI-On
		ldaa    SPI_Address
		anda    #00011111B
		staa    SPDR
		clr     SPI_TimeOut
		rts
************** Wait until device is ready   ***********
SlaveBusy    	andb    #11101111B    Ack - falling edge
		stab    PORTD
		ldx     #SendSPI_Task
		stx     SPI_Task
		orab    #BIT4+BIT3    generate STOP Ramp
		stab    PORTD
		rts
*********************************************************
SPI_Add1        ldab    SPSR
		bpl     SPI_RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - rising edge
		bitb    #BIT2
		bne     NACK

		ldx     #SPI_DATwrite
		stx     SPI_Task

		andb    #11101111B  Ack - Fallflanke
		stab    PORTD

		oraa    #BIT6
		staa    SPCR          SPI-On
		ldaa    SPI_Address+1
		staa    SPDR
		clr     SPI_TimeOut
		rts
***************************************
SPI_RTI         inc     SPI_TimeOut
		ldaa    SPI_TimeOut
		cmpa    #100
		bls     SPIweg
		jmp     SPI_TIMEOUT
SPIweg          rts
***************************************
SPI_DATwrite    ldab    SPSR
		bpl     SPI_RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - rising edge
		bitb    #BIT2
		bne     NACK

		ldx     SPI_pointer

		andb    #11101111B   Ack - falling edge
		stab    PORTD

		oraa    #BIT6
		staa    SPCR          SPI-On

		ldaa    X
		inx
		stx     SPI_pointer
		staa    SPDR
		ldab    SPI_nrbytes
		decb
		ble     SPI_SaveExit      0 
		stab    SPI_nrbytes
		rts
**************************************************************
NACK            ldx     #SPIvoidTask
		stx     SPI_Task
		rts
*************************************************************
SPI_SaveExit    ldd     #SPI_EXIT
		std     SPI_Task
		clr     SPI_TimeOut
		rts
*************************************************************
SPI_EXIT  	 ldab    SPSR
		bpl     SPI_RTI

		ldaa    SPCR
		anda    #00111111B       SPI-Off
		staa    SPCR

		ldab    PORTD
		orab    #BIT4
		stab    PORTD        Ack - rising edge

		clr     SPI_TimeOut

		andb    #11101111B    Ack - falling edge
		stab    PORTD

		ldx     #SPIvoidTask
		stx     SPI_Task
		orab    #BIT4+BIT3    	STOP Ramp
		stab    PORTD
		rts
*************************************************************
SPI_TIMEOUT     ldd     #SPIvoidTask
		std     SPI_Task
		rts
*************************************************************