New to shift registers

Andrew Cowan

Senior Member
I'm working my way around shift registers, and learning how to use them.

I'm just wondering - is there an easier way to code this:
pulsout 1,1
pulsout 2,1 '(clock)
pulsout 1,1
pulsout 2,1 '(clock)
pulsout 2,1 '(clock)
pulsout 2,1 '(clock)
pulsout 3,1 '(latch)

Thanks,

Andrew
 

westaust55

Moderator
Andrew,

from looking at your past posts looks like you have a 28X.
So you do need to bit bash the byte out one bit at a time - which what I believe you are trying to do.

In simple terms, you need to
1. identify if the bit to be sent is a 1 or 0
2. set up the output data pin to suit (as 1 or 0)
3. pulse the clock pin (while the data is steady)
4. shift the data in PICAXE to lok at next bit
5, goto 1 (above) until required number of bits are sent.
6. pulse the latch pin to move data within shift register to the shift register outputs.

Suggest have a look at Dr Acula's thread here:
http://www.picaxeforum.co.uk/showthread.php?t=9116&highlight=595*
 

Andrew Cowan

Senior Member
Thanks westaust55. Although I do not need this for any current project, I'm just experimenting. Currently I have connected the shift register to an 08M. The Programming and customizing the Picaxe Microcontroller book has a good tutorial on shift registers.

Your post said I would have to bit bang if I use a 28X - are there other chips (eg 28X1) that don't need bit banging? What do you do with these?

Thanks,

Andrew
 

westaust55

Moderator
With the X1 parts (28X1 and 40X1), the SHIFTOUT command can be used.

Here is a sample of the code that is then used:

Code:
; test program to drive 8 LED via a 74HC595
; program uses the shiftout command instead of pushing bits out one at a time
; lit LED starts at the LSB (right) side and steps to the MSB (left) side
; then steps back again to the LSB side. Continues back and forth forever . . .

symbol clock = 4           ; clock on Output 4
symbol serdata = 5        ; data output on Output 5
symbol latch = 6           ; latch to 595 outputs with high pulse on Output 6
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
symbol delaytime = b21  ; delay between 'stepping' to next LED pattern

init: low latch
      low clock
      pattern = %00000001
      delaytime = 500    ; 500 = 0.5 sec for slow step rate. At 20 if "flies"

main:
	DO
		GOSUB out_595
		pause delaytime
		pattern = pattern <<1
	loop until pattern=%10000000
	DO
		GOSUB out_595
		pause delaytime
		pattern = pattern >>1
	loop until pattern=%00000001
	goto main

out_595:
	shiftout clock,serdata,msb_1st,(pattern/bitz)
	pulsout latch,5
	return
 

hippy

Ex-Staff (retired)
In terms of 'easiest' and 'best' ways to code control of shift registers it frequently depends upon what the criteria is; fastest code where program memory used isn't a problem, smallest code where speed isn't that important.

The X1's offer SHIFTOUT which is both fast and compact. For others, bit-banging can be done in a FOR-NEXT loop ( slow but compact ) or 'unrolled' so it's a sequence of set and pulse commands ( faster but more code ).

The X1's also have HSPIOUT which is even faster than SHIFTOUT but requires certain I/O pins to be used. Bit-banging techniques can still be used on the X1's if that's prefered.
 

Andrew Cowan

Senior Member
Thanks - I'll let you know how I get on. I think I will start by counting with a seven segment display via an 08M.

Andrew
 
Top