74HC165 question

JSDL

Senior Member
I have built a keypad decoding circuit around the MM74C922 decoder IC and a 74HC165 parallel IN, serial OUT shift register. After searching through the forums for coding the Picaxe to read from the 74HC165 and how it works, I got the circuit working correctly. I just want some clarification on the process for clocking in the bits from the shift register. I understand fully how the 74HC595 works in terms of the code, but am finding it difficult to wrap my hear around the 74HC165 operation. Here is my code:

Code:
setfreq m16
setint %00100000, %00100000

symbol CLOCK=b.1
symbol PL=b.2
symbol CE=b.3
symbol counter=b10
symbol thedata=b11
symbol SerIndata= pinb.0


main:

LOW CLOCK
LOW CE
HIGH PL

goto main


interrupt:
serout c.0, t9600_16,("Entered Interrupt Routine",cr,lf)

do while pinC.5=1:loop
pause 10

pulsout PL, 10


for counter = 0 to 7
	thedata=thedata * 2
	'serout c.0, t9600_16,(#serindata,cr,lf)
	if serIndata=0 then skipMSBPre
	thedata=thedata + 1
	
skipMSBPre:
	pulsout CLOCK,10
next counter

serout c.0, t9600_16,(#thedata,cr,lf)

setint %00100000, %00100000
return
I am looking more specifically for step by step explanation of the counter loop. Why add 1 to "thedata" if serindata value is 0? Also, what is the difference between sampling before the clock and after the clock? Lastly, why shift the data right (multiply by 2) before clocking out the bit. Wouldn't that discard the MSB before storing it into a variable? Any explanations would be greatly appreciated.
 

BESQUEUT

Senior Member
At start thedata must be zero.
IMHO
thedata=0 ' is missing
Why add 1 to "thedata" if serindata value is 0?
NO :
if serIndata=0 then skipMSBPre
So
thedata=thedata + 1
only if serIndata<>0

This woul be many more readable :
Code:
	if serIndata<>0 then
		thedata=thedata + 1
	endif
You can also write (quicker...) :
thedata=thedata + serIndata

why shift the data right (multiply by 2) before clocking out the bit. Wouldn't that discard the MSB before storing it into a variable? Any explanations would be greatly appreciated.
Euhhh ???
MSB is on the left, LSB at right.
multiply by 2 shift thedata to the left.
Yes it does ; but than one is always zero ==> so no matter !
thedata=thedata + 1
set the LSB.
 
Last edited:

hippy

Technical Support
Staff member
Code:
for counter = 0 to 7
	thedata=thedata * 2
	if serIndata=0 then skipMSBPre
	thedata=thedata + 1
skipMSBPre:
	pulsout CLOCK,10
next counter
These days, with support for block IF commands, that is probably more understandable as ...

Code:
For counter = 0 to 7
  thedata = thedata * 2
  If serIndata = 1 Then 
    thedata = thedata + 1
  End If
  PulsOut CLOCK, 10
Next
Which could be optimised to ...

Code:
For counter = 0 to 7
  thedata = thedata * 2 + serIndata
  PulsOut CLOCK, 10
Next
 

JSDL

Senior Member
sorry I actually meant to say the data is shifted to the left. What do you mean when you say "but than one is always zero so it doesn't matter"? Still trying to understand why the shift left (thedata = thedata * 2) before the CLOCK is pulsed? Is it because there is already 0 present at Q7 (serial OUT of the shift register) at the start?
 
Last edited:

BESQUEUT

Senior Member
sorry I actually meant to say the data is shifted to the left. What do you mean when you say "but than one is always zero so it doesn't matter"? Still trying to understand why the shift left (thedata = thedata * 2) before the CLOCK is pulsed? Is it because there is already 0 present at Q7 (serial OUT of the shift register) at the start?
Suppose you have to shift :
10110010
thedata will be :
00000000
00000001
00000010
00000101
00001011
00010110
00101100
01011001
10110010
Each time you shift left, a zero is lost, but no matter !

Note that if you set LSB, at start thedata may contain what you want (not only zeros) ==> it will be lost, and no matter...
For example, starting with 1's :
11111111
11111111
11111110
11111101
11111011
11110110
11101100
11011001
10110010
 
Last edited:

hippy

Technical Support
Staff member
Still trying to understand why the shift left (thedata = thedata * 2) before the CLOCK is pulsed? Is it because there is already 0 present at Q7 (serial OUT of the shift register) at the start?
That is correct; the D7 input will be presented immediately on Q7 output when the data is latched into the 74HC165. The first clock will shift and present D6 to Q7 etc.
 

JSDL

Senior Member
ok thanks, I think I get it now. Last thing, what is the difference between PRE and POST clock (with reference to shifting bits), and is there any benefit to shifting MSB vs LSB first, or is it just a matter of preference? Thanks again to everyone for their help.
 

hippy

Technical Support
Staff member
Last thing, what is the difference between PRE and POST clock (with reference to shifting bits)
It usually indicates when a data line is sampled; just below the clock's initial edge or after it.

and is there any benefit to shifting MSB vs LSB first, or is it just a matter of preference?
That is usually dictated by the hardware or protocol being used. The 74HC165 has Q7 as the output and clocks out msb first. Serial works the other way round, start bit, then lsb etc.
 
Top