Fastest way to bitbang out data

Im passing data to a 4 digit seven segment display. It requires the data to be passed serially, and clocked using a separate pin.

I have tried a simple bit banging approach and it all words fine...but i want to have a clock display that increments every 10ms, as it will be filmed with a high speed camera. Sadly, Passing the data using my current code takes just a bit too long to support this. A fragment of the code is shown below. I'm just testing bit values in the B0 variable and setting the data line low or high as appropriate and toggling the clock line.

symbol Digit = B0
symbol Digit1_SS = B1
symbol datapin = C.0
symbol clockpin = C.1

Digit1_SS= %00110011

Digit = Digit1_SS
if Bit0=1 then
low datapin
else
high datapin
end if
high clockpin
low clockpin

if Bit1=1 then
low datapin
else
high datapin
end if
high clockpin
low clockpin

if Bit2=1 then
low datapin
else
high datapin
end if
high clockpin
low clockpin

and so on.....

I wonder if theres anything I can do to speed if up ? Im running at the fasted clock freq for the chip.

cheers

Nick

Nick
 
Your code would execute faster if you replaced each section with something like
Code:
datapin = bit0
pulsout clockpin
(remembering, of course, to define datapin as OUTPUT (it doesn't happen by default).
Also, what speed are you using? Picaxe M2s can operate up to 32MHz (default is 4MHz) and X2s up to 64MHz (some need an external resonator), default is 8MHz.
 
Pete- I didn't know anything about SPI you mentioned it. It looks very promising. But only for x2 parts? I only have M2.

Aries- Thank you. I will try it. I have nowhere to go on clock freq. Running at 32mhz
 
Examples 2 & 3 on page https://picaxe.com/basic-commands/advanced-io-interfacing/shiftout/ show a software method of implementing SPI. This might help you. However, I think Aries method will give you the fastest output, but will need a lot of repetitive code.

Assuming you have 8 digit x 7 seg+DP, then you need to send 64 bits. Easiest way is to use b0-b3 as bit0-bit31, then use 32 Aries blocks to send the first 4 four digits. Then set b0-b3 to second 4 digits, and use 32 more blocks.

Code:
symbol datapin = pinC.0
symbol clockpin = C.1

'
' Put code here to load first 4 digits
'

' Send first 32 bits
datapin = bit0
pulsout clockpin,0
datapin = bit1
pulsout clockpin,0
datapin = bit2
pulsout clockpin,0
'
' More code blocks
'
datapin = bit30
pulsout clockpin,0
datapin = bit31
pulsout clockpin,0

'
' Put code here to load second 4 digits
'

' Send second 32 bits
datapin = bit0
pulsout clockpin,0
datapin = bit1
pulsout clockpin,0
datapin = bit2
pulsout clockpin,0
'
' More code blocks
'
datapin = bit30
pulsout clockpin,0
datapin = bit31
pulsout clockpin,0

' Done !
 
Last edited:
Hi,

There are numerous tricks to speed up PICaxe execution time, so I would expect that transmitting 4 (or even 8) bytes within 10 ms should be feasible. Firstly, avoid "slow" instructions such as GOSUB (CALL) / RETURN, ELSE and preferably IF/GOTOs (and many others) if possible. As suggested by Aries, a PinX.Y = bitN will be much faster than an IF ... THEN {GOTO}s ... , etc.. A PULSOUT pin , 1 might be faster than HIGH pin : LOW pin but only if the pulse is quite narrow (I don't think I've ever tried a pulse width of zero ! ). Use "linear" code (consecutive instructions) to process the bits within a byte, with loops only at the byte (or perhaps nibble) level. If the bits within a byte need to be rotated/shifted, use Left-Shifting, e.g. b0 = b0 + b0 {+ carryin} (noting that Word operations are NOT any slower than with bytes), reading bits out at the MSB end.

The M2 chips don't have native SPI instructions, but the "Base Chip" still contains the SPI hardware module, which can be used with POKESFR instructions (as can the serial UART hardware). But personally, I always try to use I2C Bus - based interface devices because the HI2COUT instructions are about the fastest way of moving data bytes out of a (M2) PICaxe chip (at least when using native instructions).

Cheers, Alan.
 
Apparently the TS already has a specific module which requires serial input data.
Otherwise, as you mentioned, I would use I2C. Or if speed is at premium, output a pair of BCD nibbles in parallel, and allow a pair of CD4511 to perform the BCD to 7 segment decoding. As the CD4511s are latched, there won’t be missing segments during the video recording. For four digits, a simple multiplexing will do the trick.
 
Back
Top