Data Shift Out

#1
In searching the forum for faster shift out code I came across Hippy's posted suggestion on 26.10.2005. I reads
......................................................................................................
'Untested; fastest way to clock 8 bits out msb first (removing the FOR-NEXT overhead), could be to put it in b0 (MSB of w0) and set b0 (LSB of w0) to $FF. As w0=w0*2 operates the LSB is eventually cleared.

b1 = $A5 'data to send
b0 = $FF

BitLoop:
pin0 = bit15
w0 = w0*2
IF b0 <> 0 THEN BitLoop

'To shift 16 bits out, w0 and another word variable can be used

w0 = $A593 'data to send
w1 - $FFFF

BitLoop:
pinX = bit15
w0 = w0*2
w1 = w1*2
IF w1 <&gt 0 then BitLoop
......................................................................................................

Would apprciate clarification on the following,

a) pinX = bit15. Substituting any numeral for X when running the code always results in the syntax error 'pin? unknown symbol'

b) In the line reading ...IF b0 <> 0 THEN BitLoop..what are the terms inside the IF....THEN command? Shouldn't there be an = sign somewhere?

I've not found any references in the manuals to pinX or the if ... then terms used here. Are they peculiar to a particular Picaxe model? The thread containing the post makes no limitation. I'm using this as a learning experience with an 08M.
 

hippy

Technical Support
Staff member
#2
a) That should work, X being replaced by a numeral. Which PICAXE are you using ? You can also try "outpinX =".

b) the <&gt is an artifact of the old forum, HTML markup for less than and greater than, the correct code should be

IF b0 <> 0 THEN BitLoop
IF w0 <> 0 THEN BitLoop

With the recent compiler improvements, the nicest code would be ...

Code:
b1 = $A5 'data to send
b0 = $FF
Do
  pin0 = bit15
  w0 = w0*2
Loop Until b0 = 0
 
#3
The SHIFTOUT command appears to be faster for those PICAXE that support this command.

For a test on my 4MHz PICAXE 40X1,

The following code executed 5000 loops in 19 SECONDS:
or deducting the time for an empty FOR..NEXT loop of 7 seconds
means 5000 bytes shifted out in 12 seconds
Code:
symbol serdata = 0        ; data output 
symbol clock = 1           ; clock ou
symbol latch = 2           ; latch (as for 74HC595)
symbol bitz = 8             ; number of bits to send
symbol msb_1st = 1       ; 1 = MSB first and idle state is low
symbol pattern = b20     ; variable to hold the LED pattern

pattern = $FF
FOR w5 = 1 TO 5000
	SHIFTOUT clock,serdata,msb_1st,(pattern/bitz)
	PULSEOUT latch,5
NEXT w5
Whereas the following code executed 5000 loops in 91 SECONDS
or deducting the time for an empty FOR..NEXT loop of 7 seconds
means 5000 bytes shifted out in 84 seconds

Code:
for w5 = 1 to 5000
	b1 = $A5 'data to send
      b0 = $FF
      Do
         pin0 = bit15
         w0 = w0*2
      Loop Until b0 = 0

	next w5
And this second version did not include any clock pulsing or latch pulsing signals

Just listening to the tone of a speaker attached to output pin 0 was a fair give indication as to what was going to be the results.

EDIT: So the SHIFTOUT is almost 4.8 times faster including to FOR... NEXT loop or in terms of just shifting out a byte of data the shift part alone is 7 times faster.
 
Last edited:
#4
Hippy, thanks

Pin0 is not useable with the 08M mentioned as it tries to nominate output 0 as an input, whereas 0 must always be an output. However, the outpin0 syntax is okay and uses just 18 bytes of memory. The code rattles out a nice string of data bits for devices requiring a serial input. A pulsout command as the penultimate line in the code makes it useful with a shift register. The resulting 20 bytes of memory is a big improvement over the 37 bytes required by the shiftout workaround suggested in the Rev-Ed shiftin_out.txt document.

What changes are needed for an LSB first output? I tried switching the b1 and b0 values and the w0 = w0*2 to w0 = w0 & 1 but no joy. Finding these pieces of maths troublesome and guidance would be appreciated.

Westaus55, thanks also.

Unfortunately the shiftout command is not available with the 08M. The nearest model to the 08M which accepts the command seems to be the 18X.
 

hippy

Technical Support
Staff member
#5
Lsb shifted out first, note data to send is now in b0 ...

Code:
b0 = $A5 'data to send
b1 = $FF
Do
  outpin0 = bit0
  w0 = w0/2
Loop Until b1 = 0
 

evanh

Senior Member
#7
There was a good thread that myself and Hippy answered with details of managing of the setup for using the SHIFTOUT command.

That might be worth linking here. Sadly, the search in the new web software doesn't work for me.


Evan
 
Top