Bit banging head banging...

DamonHD

Senior Member
Hi,

This subroutine happily bit-bangs SPI (sending a byte) for an RTC and a radio:

Code:
SPI shift out (ie write) a single byte, most-significant bit first, data pre-clock.
; Sends output data from SPI_DATAB byte variable.
; Destroys tempB0, tempB1, SPI_DATAB.
SPI_shiftout_byte_MSB_preclock:
	for tempB0 = 0 to 7
		tempB1 = SPI_DATAB & $80
        if tempB1 = 0 then
            low SPI_SDO
        else
            high SPI_SDO
        end if
	    pulsout SPI_SCLK_O, 1 		; pulse clock for 10us to send next bit
		SPI_DATAB = SPI_DATAB + SPI_DATAB
	next tempB0
	return
this version, however, which I unrolled (a) for speed and (b) to avoid trashing two registers, does not, and appears to always send a $00:

Code:
;FAILED OPTIMISATION: SEEMS TO ALWAYS BEHAVE AS IF SPI_DATAB == 0.
; SPI shift out (ie write) a single byte, most-significant bit first, data pre-clock.
; Sends output data from SPI_DATAB byte variable.
; Destroys B0.
; Unrolled for speed and to reduce the working memory required: a few bytes larger than loop version...
SPI_shiftout_byte_MSB_preclock:
    B0 = SPI_DATAB    
    SPI_SDO_PIN = bit7
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit6
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit5
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit4
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit3
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit2
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit1
 	pulsout SPI_SCLK_O, 1
    SPI_SDO_PIN = bit0
 	pulsout SPI_SCLK_O, 1
	return[/FONT]

Definitions look like this:

[FONT=Courier New]symbol SPI_SCLK_O = B.2			; SPI clock (output) 18M2 p8
symbol SPI_SDI = input6			; SPI data (input) C.6 18M2 p15
symbol SPI_SDO = B.3			; SPI data (output) 18M2 p9
symbol SPI_SDO_PIN = pinB.3     ; SPI data (output) in pinX.Y format.
symbol SPI_DATAB = b8			; SPI write byte from here and read byte to here
symbol tempB0 = b14			    ; temp working variable
symbol tempB1 = b15 		 	; temp working variable
I can point you to complete sources files on SourceForge if that would help!

Note the swerve already for SPI_SDO and SPI_SDO_PIN.

Any ideas?

Rgds

Damon
 
Last edited by a moderator:

Technical

Technical Support
Staff member
Two things
1) Make sure you set SPI_SDO_PIN as an output - you probably haven't. Easy way is to use low on it at the start of your program.

2) Ideally symbol SPI_SDO_PIN = outpinB.3 as it is an output so you want to address the output latch register not the port register
 

nick12ab

Senior Member
In the second bit of code you're writing the value direct to the pin variable. You must first set the pin as an output first for that to work. Add the line:
Code:
output SPI_SDO
at the beginning of your program.
 

DamonHD

Senior Member
Hi,

Thanks both of you.

I believe that I have tried all of those things, but I'll go and try them all at once for luck and report back! B^>

First a quick walk while the is some sunshine, to clear my head!

Rgds

Damon
 

DamonHD

Senior Member
Well folks, thank you!

I must not have been doing all those things at the same time, now I am, miraculously, it all works it seems!

Hurrah!

Rgds

Damon
 
Top