PDA

View Full Version : Replacing shift register with PIC



TonyA
- 7th April 2008, 16:10
Hi,

I was wondering if it is possible to replace a 4021B (parallel to serial) shift register chip with a PIC using shiftin and shiftout functions?

I am assuming that I would first need to shiftin the latch and clock data from the master, then shiftout the (serial data) bits to the master.

The "master" in this case is a Nintendo NES video game system which uses a standard 4021B shift register in it's joystick to read 8 buttons in a parallel fashion, and then shifts the 8 bits out serially to the data input pin of the NES joystick input port.

The NES sets the latch line low when it is ready to receive the serial data from the shift register. The shift register also receives clock data from the NES (master), but I'm not sure what to do with the clock data received.

I want to mimic a 4021B shift register with my pic, but not sure how to handle the data received from the "master" clock and latch.

Hope I'm not too confusing. If I am I'll try to clarify.

Thanks for any help,
T

skimask
- 7th April 2008, 16:17
I was wondering if it is possible to replace a 4021B (parallel to serial) shift register chip with a PIC using shiftin and shiftout functions?
I don't see why not. I think the biggest problem would be how fast the incoming data and outgoing clock signal would have to be.
But yes, 8 data lines and a latch signal for an input, easy enough... Put the data on the lines, watch for the latch to go low (or high, whatever), grab that data and store it, wait for the latch line to go back high (or low) and start waiting again.
Transfer that data to an output line using either shiftout or the builtin MSSP, easy enough also. If you use shiftout, the PIC would be tied up shifting that data out, if you used the MSSP, you could theoretically clock it out at about 2.5Mhz, depending on the PIC and clock speed used.

TonyA
- 7th April 2008, 16:25
The pic would only be receiving clock data and latch data from the master in my case.

And then it would only need to send serial data, and (clock data?) back out to the master.

I'll post my code and try to clarify this a bit. Thanks for your reply.

T

skimask
- 7th April 2008, 16:28
The pic would only be receiving clock data and latch data from the master in my case.
And then it would only need to send serial data, and (clock data?) back out to the master.
T
Just read the rest of your 1st post.
Yes, if the NES is sending out a clock, then that would make your PIC a slave device, clocked by something else.
Most PICs support both master and slave SPI operations.

TonyA
- 7th April 2008, 16:55
Yes, right the pic would be a slave. Here is what I have.

shiftin latchPin_in, clockPin_in, MSBFIRST, [latch_receive, clock_receive]

' clockPin_in is a pin on the pic that is attached to the clock line of the NES gamepad port
' latchPin_in is a pin on the pic that is connected to the latch line of the NES gamepad port


if latchPin_in = 0 then 'if we receive a zero from the master's latch output data then shiftout serial data.

shiftout serialOut_pin, clockPin_in, MSBFIRST, [serial data output] 'send a byte out to the master.

In this case what do I do with the clock data received when shifting in? I'm not sure how to handle the clock data received.


Does a 4021B shift register (slave) receive latch and clock data in a standard (fixed) way? Or does this rely on how the latch and clock data are being sent from the master?

You said I would "tie up" the pic doing this. If that's true than maybe I'll skip this and just use a pic to send the parallel input into a real shift register, then out to the master.

I was thinking about using a pot via ADCIN and then send the shifted out serial bits based on the pot reading. Would that work with shiftin and shiftout going on as well?

Thanks again,
T

skimask
- 7th April 2008, 18:12
Does a 4021B shift register (slave) receive latch and clock data in a standard (fixed) way? Or does this rely on how the latch and clock data are being sent from the master?
Datasheets for the 4021 should tell you all you need to know...about the 4021.
I guess the main thing would be to 'scope out what the NES is sending out, figure out how fast it's going, what it's logic levels are, and so on.


You said I would "tie up" the pic doing this. If that's true than maybe I'll skip this and just use a pic to send the parallel input into a real shift register, then out to the master.
Shiftin/Shiftout are both 'blocking' commands. You can't do anything else until the commands are done executing because it's all done in software.
If you used the MSSP module in a PIC, you could just punch a byte to the TX register, hit a bit to send it, and go about your business. When you have another byte to send out, check the busy bit, if it's good, send another byte. The MSSP does all the clocking and shifting in hardware.


I was thinking about using a pot via ADCIN and then send the shifted out serial bits based on the pot reading. Would that work with shiftin and shiftout going on as well?
Depends...on the time it takes to read the pot, how fast shiftin/shiftout run, among other things...
You could start the ADC, shiftout a byte. By the time a byte is shifted in or out, the ADC would be finished and ready for a read. Then restart the process...

TonyA
- 7th April 2008, 18:31
Thanks again for your help, sincerely appreciate it.

(Will experiment)

Tony