• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How fast can a Microchip PIC latch pin switch?

Status
Not open for further replies.

krazy

New Member
Hi all,

I am trying to control a serial-to-parallel 74HC595 shift register with my Microchip PIC.

I need to load 8 bits into the shift register, then, clock the externally facing layer of registers so they will pick the value off of the shift register and send it out to the world.

Right now, I am assuming the best way to clock this layer of registers is by connecting that clock to a GPIO latch-controlled PIC pin and clock it manually in my code when I need to. The only problem with that is I need some general control over the duration of this clock pulse.

So.... how fast can a latch pin on a Microchip device (specifically, the dsPIC33FJ128GP802) switch? As fast as the device clock? Or am I totally off and should be doing something different?

Thanks! :)
 

Sceadwian

Banned
However many cycles it takes the bit toggle operation to run.
 

3v0

Coop Build Coordinator
Forum Supporter
Are you thinking you can clock the 595 so fast that it will fail. The max speed depends on logic family. you are using HC the data sheet shows you can run the clocks at 100MHz.

I do not think you have to worry.

If you want to find the actual time, look at the code in the disassembler or use the simulator and stopwatch.
 
OOPS, slow fingers once again. Well here is your double dose anyway:).

The best way to find out is to put your code in an MPLAB project, and run the Simulator. Set the PC (program counter/green arrow) just prior to entering your code loop. Also set the program break point (red arrow) to just after your code loop. Be sure to have the view stopwatch on, set the clock speed in the debugger settings. Clear the stopwatch and press the run button. There you have it.

You need control over what clock? If its the '595 clock then use PWM on the '595 OE pin, or interrupts. Have not used the dsPic33, but clock speed is not doable with all the the clock toggling, looping, latching etc.
 

krazy

New Member
Yeah, I knew I couldn't outpace the HC595... its blazingly fast.

Ok, so maybe I'm making the wrong assumptions and, therefore, seeking the wrong piece of information.... here is my situation:

I'm using the dsPIC to drive an array of LEDs. The goal is to have them light up in fanciful patterns and have the patterns alternate during a certain period (to a beat.. so like every 1/4 second). As such, I am using the serial-to-parallel to address each array element.

Since the serial-to-parallel is shift register-based, that means I have to clock in 8 bits, however fast, then, AFTER the last bit is clocked in and BEFORE the next bit is clocked in after that, I need to clock the external layer of registers to send the data out to the array (thereby, plucking the data off of the shift register and sending outward).

Now, my conundrum is getting the timings down for all of this. What would best practice be for this situation? I need one clock which is speed X and then another clock which is speed X/8. At the same time, I may need some kind of granular, manual method of, ultimately, sending in data and clocking it out (so I execute this 'refresh' function when I want to update the array pattern).

Related question: Being as I need to write the patterns to memory and I will be using up alot of cycles for math calculations, wouldn't it be inefficient/impossible to rely on a constant loop refresh that is continually feeding and latching the serial-to-parallel (therefore, requiring the implementation of a manual 'refresh' function)?

Any suggestions here?

Thanks for all who have followed along so far. :)
 
Last edited:

krazy

New Member
Bump for an answer to this. Please help out if anyone has an answer. This is the last key to kingdom of my semester long project. :)

Thank you so much.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Just write a subroutine that outputs the 8 bits and toggles the latch pin. It shouldn't take more than 100 cycles. Then just call the subroutine at a regular interval.

Mike.
 

Someone Electro

New Member
If you want it realy fast use the SPI port,it can be set up to make signals that the shift register understands.I think the SPI can shift data out at the speed of the instruction clock speed(thats i think 40MHz for that pic)You also benigit a lot of cpu time as sending 16bits trougn the SPI takes a single register write. But bewnare that 40MHz might have trouble over long wirex,making it worse is the low pin current capability of the high end pics
 

krazy

New Member
Mike,

Thanks for the reply.

One question... wouldn't it be plausible that, after the 8 bits were clocked in, the latch may or may not clock fast enough so that another bit may get clocked in, pushing the whole thing forward one bit? Keep in mind, the shift register clock will still be running.
 

krazy

New Member
If you want it realy fast use the SPI port,it can be set up to make signals that the shift register understands.I think the SPI can shift data out at the speed of the instruction clock speed(thats i think 40MHz for that pic)You also benigit a lot of cpu time as sending 16bits trougn the SPI takes a single register write. But bewnare that 40MHz might have trouble over long wirex,making it worse is the low pin current capability of the high end pics
Oooh... how long is 'long'? :p I'm wiring up the insides of a coffee table (yes, coffee table) so wires may run 2-3 ft long (~.5-1m).
 

Pommie

Well-Known Member
Most Helpful Member
Mike,

Thanks for the reply.

One question... wouldn't it be plausible that, after the 8 bits were clocked in, the latch may or may not clock fast enough so that another bit may get clocked in, pushing the whole thing forward one bit? Keep in mind, the shift register clock will still be running.
Why are you trying to sync to an external clock. Use 3 pins, data, clock and latch.

Mike.
 

dougy83

Well-Known Member
Just put the '595 on one of the many SPI ports of the picmicro. Set up the SPI port as a master, and toggle the SS line in the SPI done ISR.

e.g.
Code:
void interrupt SPI_DONE()
{
   SS_PIN = 1;    // latch in the last byte
   if(dataQueue.count() > 0)
   {
      SPI_DR = dataQueue.read();
      SS_PIN = 0;  // get ready to latch in this byte
   }
   else
   {
      disableSPIinterrupt();
   }
}
And to send the data:
Code:
void sendData(data_t dat)
{
   // wait for space in queue
   while(dataQueue.full()) {}

   // add data to queue and make sure SPI interrupt can send it
   dataQueue.write(dat);
   enableSPIinterrupt();
}
 

3v0

Coop Build Coordinator
Forum Supporter
Krazy,

Stop thinking of these signals as free running clocks. they are signals you send out as needed.

Next note that the 595's are cascadable. Lets say you have 4 of then cascaded. Use a loop to set the data and toggle the clock 32 times. Then clock latch and wait for a period of time (display time/rate).

Lets say you have even more LEDs to handle. Lets assume you placed 4 cascaded 595's on each bit of a port. This will control 32x8 LEDs. The software is the same as for as about but we write to all 8 strings of 595's at once. It executes at the same speed.

Next lets cover timing. If all the cpu is going to do is manage the LEDs you can do without interrupts. Store each light sequence in an array. Each array element has one bit for each light (on or off). You can optionaly add a byte for how long to display the data for this array element.

The program is a simple loop. Shift out one array element and wait, then shift out the next.

------------------------------------------------------------
The following may be overkill for your application.

It is even possible to write an engine with its own set of opcodes. I have C code for such an engine setup to work on 2 cascaded 595's / 16 LEDs. If you want it all you have to do is ask.

Code:
// high order bit of top nyble is breakpoint bit
// the remaining 3 bits are used for opcodes

// progaram flow
#define BREAK 0x8000  // set breakpoint in ms array
#define NOP   0x0000  // display the LED data
#define HALT  0x0100  // halt execution
#define JMP   0x0200  // jump to absolute array index (dec or hex)
#define JMPR  0x0300  // jump relative  "JMPR, 4," or "JMPR, -8," (signed decimal)
#define FOR   0x0400  // for (startPC, endPC), 4 bytes OC COUNT START END
//   display data and modifiers
#define INV   0x0500  // invert data   data = data XOR 0xFFFF
#define NOINV 0x0600  // turn off INVRT
#define REV   0x0700  // reverse order of data bits (mirror)
#define NOREV 0x0800  // turn off REV
#define DISP  0x0900  // display the data in the next word
More opcodes are possible. This is a starting point.

3v0
 

krazy

New Member
3v0

Thanks for the tips. So you're suggesting not to let the clock run continuously but to just manually clock 3 pins (via GPIO, I guess?)?

What do you mean by "Stop thinking of these signals as free running clocks."? I thought the CLK pin connected to my SPI module will run continuously while the processor is running.

If this is the case, wouldn't it make sense to devise a careful way to clock the latch (within the timings of the 8th, 16th, so on serial register clocks)?

I have C code for such an engine setup to work on 2 cascaded 595's / 16 LEDs. If you want it all you have to do is ask.
Could I?

Thank you! :)
 
Last edited:

Nigel Goodwin

Super Moderator
Most Helpful Member
3v0

Thanks for the tips. So you're suggesting not to let the clock run continuously but to just manually clock 3 pins (via GPIO, I guess?)?

What do you mean by "Stop thinking of these signals as free running clocks."? I thought the CLK pin connected to my SPI module will run continuously while the processor is running.
No, and it would be a complete disaster if it did - as he said "Stop thinking of these signals as free running clocks."

You send out a clock pulse for each bit you want to clock in or out, and at no other time.
 

3v0

Coop Build Coordinator
Forum Supporter
3v0

thanks for the tips. So you're suggesting not to let the clock run continuously but to just manually clock 3 pins (via gpio, i guess?)?
yes
data, clock, and latch


what do you mean by "stop thinking of these signals as free running clocks."? I thought the clk pin connected to my spi module will run continuously while the processor is running.

i use gpio pins and set them as needed. KISS. It is easy to understand and troubleshoot. A free running clock would shift data in every cycle even when you have no valid data. This is not a problem execpt you have to time the latch operaton so that it falls between the last valid data bit and the first meaningless one. You need to shift out 8*(number of 595 cascaded) then latch. I think that may be tricky with spi hardware, if possible. It is very easy in software.

if this is the case, wouldn't it make sense to devise a careful way to clock the latch (within the timings of the 8th, 16th, so on serial register clocks)?

Covered this above.

Could i?

Thank you! :)
My first choice is to advoid dedicated hardware. Software drivers can be used on any pin and tweaked if needed.

Once you understand how to drive a device with software you can use it on most any processor.

There are times when you can and should use the hardware but I do not see this as one of them.

The latest copy of the software is at school. Give me 24 hours to look it over and do a bit of documentation.

3v0
 

krazy

New Member
A free running clock would shift data in every cycle even when you have no valid data. This is not a problem execpt you have to time the latch operaton so that it falls between the last valid data bit and the first meaningless one. You need to shift out 8*(number of 595 cascaded) then latch.
That was exactly the question I had.

Ok, thanks for that dose of reality. I'll KISS and just try clocking everything manually.

Thanks! :)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top