PDA

View Full Version : 74HC165 Spin Test code, need some help



DavidM
02-11-2007, 03:36 PM
Hi,

As part of my BCD THUMB-WHEEL experiment I have tried to get the 74HC165 Shift register to work , but I need some help.
I have a Breadboard made up with 8 tactile switches, a 74HC165 chip and resistors etc. I put this together based on the BS2 example. This is connected to my PROPELLOR DEMO BOARD, and the idea is to press one or more of the buttons and have the corresponding LED's turn on .

Can someone please check my code to see what I have missed.

I am not sure what value is required for the initial LOAD PIN Pulse..

I have written this all in one routine, just to make it easier for me for now.





'' DAVES SERIAL SHIFT REGISTER TEST FOR 74HC165 - Parallel IN To Serial OUT

CON
_CLKMODE =XTAL1 +PLL16X
_XINFREQ =5_000_000

VAR
BYTE ByteValue
LONG DataPinNo
LONG ClockPinNo
LONG InBit
LONG NoOfBits
LONG LoadPinNo

PUB RunTest

ClockPinNo := 0
DataPinNo := 1
LoadPinNo := 2
NoOfBits := 8

DIRA[16..23]~~ ''This is just a flash of LED's to indicate my program is running in the PROP
OUTA[16..23]~~
WAITCNT(50_000_000+CNT)
OUTA[16..23]~

REPEAT

ByteValue := ShiftIn ( LoadPinNo, DataPinNo , ClockPinNo , NoOfBits)

CASE ByteValue ''Check what data against the values

1 : outa[16]~~
2 : outa[17]~~
3 : outa[18]~~
4 : outa[19]~~
5 : outa[20]~~
6 : outa[21]~~
7 : outa[22]~~
8 : outa[23]~~

WAITCNT(10_000_000+CNT)
OUTA[16..23]~

PUB ShiftIn ( vLoadPinNo, vDataPinNo ,vClockPinNo , vNoOfBits ) : vByteValue | vInBit

DIRA[vLoadPinNo]~~ ''Pulse Clock First
OUTA[vLoadPinNo]~~
WAITCNT(1_000 + CNT) ''Wait for some time ( should change to pulse duration)
OUTA[vLoadPinNo]~

DIRA[vDataPinNo]~
OUTA[vClockPinNo]:= 0
DIRA[vClockPinNo]~~

vByteValue:=0

REPEAT vNoOfBits

InBit:=INA[vDataPinNo]
vByteValue := (vByteValue << 1) + vInBit
!OUTA[vClockPinNo]
!OUTA[vClockPinNo]
WAITCNT(1000 + cnt)




Thanks

Dave M

inservi
02-11-2007, 05:18 PM
Hello,

For the 74HC165, in the worst case the Clock puls time must be at least 100ns but can be fine at about 25ns (at 25 C and ~ 4.5V) that mean:
For 100ns: if you take clkfreq witch give you the frequencies of clock, you must wait clkfreq *0.0001 Sec = 8_000 (cnt+8000)
for 25ns you must wait clkfreq *0.000025 Sec = 2_000

A good way to write that is




VAR
...
word PulsSize
...

PUB main
...
PulsSize := clkfreq / 8000 ' for 100 ns
OR PulsSize := clkfreq / 40000 ' for 25 ns

...
WAITCNT(PulsSize + cnt)
...




dro

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
in medio virtus

Post Edited (inservi) : 2/11/2007 9:22:27 AM GMT

DavidM
02-11-2007, 05:41 PM
Thanks,

I added the CLKFREQ /8000 +CNT to my code, I also believe that I am returning a value as the vByteValue is being calculated during the REPEAT cycle, I have added the return function anyhow?

thanks

dave M

OzStamp
02-11-2007, 06:10 PM
Hi Dave.

Your defining shiftin but do not have the obj loaded.
Go back to basics...

The HC165 needs a HIGH "1" on the load pin initially.
Then it needs a quick pulse on the load pin hi and low

Then set up a repeat loop of 8 times do the following
Since the data needs to be sample in MSBPRE mode ( see BS2 sample code that you have)

1 You add the data data = data + indata(from HC165)
2 Move the data to the left <<1 ( it is ionitially 0 so zero shifted left is still zero first time)
3 Now do a clk pulse HI for awhile low for awhile..
4 return to to line 1

Knock that up in SPIN use short wait constants.. the HC165 is lightning fast..it is a shift register ...

Come on Dave you can do this.
I have posted stuff before re sampling the ADC0831 chip very similar code look at that ...

Ronald Nollet Australia

DavidM
02-11-2007, 06:21 PM
Hi Ron,

Do I need to have SHIFTIN defined as an object? this is just a sub routine, and besides My app compiles OK and I did some tests and found that my repeats and call are working OK..


The Problem Is in the GUTS of the ShiftIn REPEAT loop,

or with my circuit of my breadboard, I have just checked my bread board and all wiring is as per BS2 Example.


regards

Dave M

OzStamp
02-11-2007, 07:25 PM
Hi Dave

Attached a quick knock up translated BS2 code across to spin

Hope you get the drift with this code.

Not tested but it is close..

Everybody will say use the BS2.functions and call the shiftin ..

I agree but b4 you do that understand the way how this chip operates.

Ronald Nollet Australia

DavidM
02-11-2007, 08:18 PM
Hi Ron

I used some of your code ( just a few lines) and guess what I WORKS!

Well at least I got a LED to light UP, I have been trying all day,

So I got exited and worked out the rest and I diddent even have to change any of my breadborad it was correct from first GO!

here is my refined code for using a 74HC165 Serial Out to PARALLEL in SHIFT REGISTER.

The circuit is based on the one in the BS2 example..






'' DAVES SERIAL SHIFT REGISTER TEST FOR 74HC165 - Parallel IN To Serial OUT
'' By Dave Metus IMIX MULTIMEDIA , Please feel free to comment and Use..
'' 11/02/2007

CON
_CLKMODE =XTAL1 +PLL16X
_XINFREQ =5_000_000

VAR
BYTE ByteValue
LONG DataPinNo
LONG ClockPinNo
LONG NoOfBits
LONG LoadPinNo

PUB RunTest

ClockPinNo := 0
DataPinNo := 1
LoadPinNo := 2
NoOfBits := 8

DIRA[16..23] ~~

REPEAT
ByteValue := ShiftIn ( LoadPinNo, DataPinNo , ClockPinNo , NoOfBits)
OUTA[23..16] := ByteValue
WAITCNT(1_000+CNT)

PUB ShiftIn ( vLoadPinNo, vDataPinNo ,vClockPinNo , vNoOfBits ) :vByteValue | vInBit

DIRA[vLoadPinNo] ~~ '' SET the LOAD Pin to OUTPUT
OUTA[vLoadPinNo] := 0 '' TOGGLE the LOAD Pin OFF
WAITCNT(1000 + CNT) '' Wait for some time
OUTA[vLoadPinNo] := 1 '' TOGGLE the LOAD Pin ON

DIRA[vDataPinNo] ~ '' Set DATA Pin to OUTPUT

DIRA[vClockPinNo] ~~ '' SET the CLOCK Pin to OUTPUT
OUTA[vClockPinNo] := 0 '' TOGGLE the CLOCK Pin OFF

REPEAT vNoOfBits '' Start REPEAT LOOP for vNoOfBits ie 8

vInBit:=INA[vDataPinNo] '' READ the DATA pin (bit value 1 or 0)
vByteValue := (vByteValue << 1) + vInBit '' SHIFT Left and Add to the BYTEVALUE the Next Bit
OUTA[vClockPinNo] := 1 '' TOGGLE the CLOCK Pin ON
OUTA[vClockPinNo] := 0 '' TOGGLE the CLOCK Pin OFF
WAITCNT(1000 + CNT) '' WAIT for some time and REPEAT





Thanks RON and Others for the help, It was 90% correct!

Now all I have to do is modify it slightly to work with my THUMBWHEELS

regards

Dave M