Manual Error - SPI shiftout_MSBFirst routine

RonCzap

Member
I was trying to use the SPI shiftout bit-banging routines on page 229 of the PICAXE manual and having no success.

I searched the forum and found this old thread from 2007 in the archive: Shiftout Error in samples directory code

When I compared the code to the shiftin_out.bas code (in the samples folder) to the code in the manual, there is indeed a difference.

I changed my code to match that in the shiftin_out.bas and the routine works great now!

I assume that the manual is in error.


Code from manual
if mask = 0 then skipMSB
Code:
shiftout_MSBFirst:
 for counter = 1 to bits ‘ number of bits
  mask = var_out & MSBValue ‘ mask MSB
  high sdata ‘ data high
  if mask = 0 then skipMSB
  low sdata ‘ data low
skipMSB: 
  pulsout sclk,1 ‘ pulse clock for 10us
  var_out = var_out * 2 ‘ shift variable left for MSB
next counter
return
Code from shiftin_out.bas
if mask = MSBValue then skipMSB
Code:
shiftout_MSBFirst:
 for counter = 1 to bits   ' number of bits
  mask = var_out & MSBValue  ' mask MSB 
  high sdata     ' data high
  if mask = MSBValue then skipMSB   
  low sdata    ' data low
skipMSB:
  pulsout sclk,1    ' pulse clock for 10us 
  var_out = var_out * 2   ' shift variable left for MSB 
 next counter
 return
 

westaust55

Moderator
Hi Ron.

Yes that topic has arisen previously on a few occassions.

In one past post I gave an example with the HIGH and LOW commands swapped (relative to the current manuals):
http://www.picaxeforum.co.uk/showthread.php?15079-SPI-problems
That sets the data line low and then jumps if the mask value is zero (0).

Likewise here (post9)
http://www.picaxeforum.co.uk/showthread.php?13816-Clocking-data-the-best-way-to-do-it&p=116886#post116886

I am sure Rev Ed will review and amend the manual 2 on the next version
 
Last edited:

westaust55

Moderator
From Manual2 at V5.1, through V7.5 inclusive:
'***** Shiftout MSB first *****
shiftout_MSBFirst:
for counter = 1 to bits ‘ number of bits
mask = var_out & MSBValue ‘ mask MSB
low sdata ‘ data low
if mask = 0 then skipMSB
high sdata ‘ data high
skipMSB: pulsout sclk,1 ‘ pulse clock for 10us
var_out = var_out * 2 ‘ shift variable left for MSB
next counter
return

Than Manual 2 V7.7 has:
'***** Shiftout MSB first *****
shiftout_MSBFirst:
for counter = 1 to bits ‘ number of bits
mask = var_out & MSBValue ‘ mask MSB
high sdata ‘ data high
if mask = 0 then skipMSB
low sdata ‘ data low
skipMSB: pulsout sclk,1 ‘ pulse clock for 10us
var_out = var_out * 2 ‘ shift variable left for MSB
next counter
return
So a change has crept in with the latest version(s)
 
Last edited:

RonCzap

Member
So a change has crept in with the latest version(s)
The code from the previous manuals works but neither version matches the current shiftin_out.bas sample code, which also works. :confused:

It appears that the editor for the manual and the sample code programmer need to put their heads together and choose the best method.

I only started using the PICAXE in September, so I don't have the earlier versions of the software or documentation.
 

westaust55

Moderator
Yes, both the version from earlier manuals and the "modified" version you found will work correctly.

As the old adage goes: "there is more than one way to skin a cat".
But even for these small routines there are some differences in efficiency of program space.

The older version has:
IF Mask = 0 THEN
Because of the way the PICAXE program is compressed, the value 0 needs only 1 bit of space.​

The "modified version has:
IF Mask = MSBValue THEN
MSBValue will have the value $80 for a byte of data being sent.​
$80 requires a full byte to save the data.

That bit versus byte space requirement in older "M" series PICAXE with only 256 bytes of program space could make the difference in whether the program could fit in the PICAXE or not. Not so curcial with the newer M2 and the X/X1/X2 sereis of parts with far more program space.
 
Top