Continue to Site

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.

  • 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.

multiple inputs: resistor ladder vs. shift register

Best way to increase input pins to a PIC?

  • Use a resistor ladder to one or more A/D pins.

    Votes: 0 0.0%
  • Use a shift register, or more than one for more inputs.

    Votes: 0 0.0%
  • Do something else (please post your idea!).

    Votes: 0 0.0%

  • Total voters
    0
Status
Not open for further replies.

Hank Fletcher

New Member
Maybe I'm stating the obvious here, but I'm trying to realize the best way to interface multiple on/off inputs to a PIC.

I think I have this right: an 8-bit shift register still uses 4 PIC pins for its operation, providing only twice as many inputs that'd other wise be used. If using multiple shift registers, can you use the same clock and load pins for all shift register ICs, for a greater amount of inputs and also a greater, new-inputs-to-PIC-inputs ratio?

My impression is that it'd be easier to just build a resistor ladder for the inputs, given that most PICs have multiple A/D pins. It shouldn't be hard to get consistent results out of just one A/D pin that way. That's my vote, but maybe I'm missing something?
 
Last edited:
Use a bigger PIC, they have ones with buckets of I/O.
I want to stick within my comfort range of the DIP PICs for now. For the first recorder robot prototype, I'm going to try to use the 16F887. Even with all its I/O, I'm still going to have to be efficient about their use.

Presuming:
- 8-bit paralllel output for eight fingers
- 16-bit parallel input for sixteen limit switches (for detecting whether each finger is fully pressed or fully retracted)
- at least a couple pins for a user interface
- a couple pins for control of the robot lungs
- a couple pins for control of the robot tongue

That's a lot of I/O! Eventually, the second prototype will need even more, with at least 9 extra pins to enable it to half-hole the recorder (needed for the full pitch-range of the instrument).
 
All things considered, would it be wasteful to just use an additional PIC? I could have one dedicated to controlling the robot, and the other dedicated to reading feedback sensors, and slave the second to the first via I2C. What do you think?
 
Last edited:
Look into SPI ICs, some old TTL 74HC series chips would work well. Or if I2C is your thing Philips and many others make quite a variety.

Multiple PICs would work as SPI / I2C slaves too. This for your flute?
 
This for your flute?
Yes, it's for the recorder robot I began blabbing about last summer. I've since realized a solution to the shortcomings of the initial solenoid design. Now that I have a practical mechanical solution (I've tested it with great results with just one finger), it's just a matter of finding the time to build it. It'll be a quick job when I can make it, but that won't be until at least June.
 
As a rule I like multi PIC solutions but...

For output use 74HC595 shift registers for outputs. It is very common an works like a champ. 2 of them would get you 16 bits of output. IIRC you need clock, data, and latch lines. There is also a OE and a RESET which you may or may not want to use.
 
I agree. Or, use the TPIC6C595, MIC5841 or A6841 serial-to-parallel 500ma 50v "sinking driver" instead of the 74HC595 if that'll match your drive requirements

16 switches can be multiplexed with 5 pins using the N(N-1) scheme where N = 5 pins will support up to 20 switches (with diodes).

Mike
 
Multiplex the switches?
multiplex-switches-gif.17857
 
Hank Fletcher said:
That seems interesting, but I don't fully understand. Got a link?
Nothing in my bookmarks. Sorry. I seem to recall it's been mentioned in a Microchip Tips n' Tricks document and on PICLIST.

It's easier to grasp (for me) if it's drawn out in column and row format. Use Port B internal weak pullups. As you 'scan' a column, only one pin will be configured as an output and driven low. The other three pins are hi-z inputs. Work out the truth table. It's pretty cool...

Mike
 

Attachments

  • N(N-1) Switches.PNG
    N(N-1) Switches.PNG
    19.9 KB · Views: 344
Hank Fletcher said:
Maybe I'm stating the obvious here, but I'm trying to realize the best way to interface multiple on/off inputs to a PIC.

I think I have this right: an 8-bit shift register still uses 4 PIC pins for its operation, providing only twice as many inputs that'd other wise be used. If using multiple shift registers, can you use the same clock and load pins for all shift register ICs, for a greater amount of inputs and also a greater, new-inputs-to-PIC-inputs ratio?

My impression is that it'd be easier to just build a resistor ladder for the inputs, given that most PICs have multiple A/D pins. It shouldn't be hard to get consistent results out of just one A/D pin that way. That's my vote, but maybe I'm missing something?

Another consideration is response time.

With the shifter, you have to latch in the external data, enable the output (if you have more than one source), clock in all those bits serially, store them as bytes//words, disable the output (if needed), then process all those bytes to make sense of what's happening.
With the ADC, you take one sample, store the value, then use a lookup table to determine the next action.
With shifting, the more inputs you add, the longer you spend pounding in bits, and add more bytes//words to process.
With ADC, you can ( I believe ) sample 50 digital inputs in one fell swoop. I say 50 because most embedded uprocessors nowadays have 10 bit ADCs, which gives you 1024 increments. 50 inputs leads to a pretty large table though. Basically, you are creating a voltage divider. See the first diagram. There is a pullup resistor to tell the processor nothing has been activated. Every resistor you "switch in" is a unique value, and brings the input voltage down to some key value. You can't make these resistors too big as you have to supply a minimum current to make the ADC work.
Since internal jitter (AKA "noise") is always a factor, you make each input value ten increments, that is, give or take five each way. The first 'band', 0 - 9, is a switch which connects directly to ground. The next has a center value of 14 or 15, and the band runs 10 to 19. The band at the top, the highest values, is the pullup resistor. Since most processors use their Vcc as the top of the ADC's reference, each increment is 5V/1024, or 4.88mV or so. This is awfully tight, which calls for high precision resistors and very careful value selection, so don't plan on 50 inputs any time soon. However, 10 to 20 should be easy enough.

Which brings us to the keypad. This usually uses 7 pins (12 keys, 3x4) or 8 pins (16 keys, 4x4). The usual practice is to output three or four lows for the columns, then read the four row values. These seven or eight bits go into a byte, whose value is used with a table to determine the next action. If the row bits (all pulled up) are all high, nothing has been pressed, and the read process ends early. That's an entire port consumed in the job. And it takes two actions to do this, even if nothing is pressed.
Or you can read it in a single ADC sample. See the diagram supplied. You have a single pullup resistor going to the ADC pin. Also connected to this pin is four unique resistors. If no key is pressed, these have no effect. The other end of these resistors goes to a column connection on the keypad. The other end of the keypad switch, the row part, goes to another unique resistor. These row resistors then go to ground.
The idea is simple enough: every key closure connects a unique set of two resistors (row and column) to the pullup resistor. This forms a voltage divider, whose output goes to the ADC pin on the processor. The values of the resistors are designed to produce 12 (3x4) or 16 (4x4) unique voltages. You could theoretically have even more more unique voltages, so more keys, but resistor selection gets hard, fast. Of course, no key pressed = pullup only = highest voltage = we're done.

As always, you have to allow for multiple inputs happening at the same time. This is where ADC sampling falls down and using shifters shines. With the ADC, you may have to divvy up the inputs to minimize possible overlaps. This may take considerable study on the users' part. With the shifters, you just shift all the inputs in, then study them at your liesure. Look for every 'low' (or whatever level you selected to indicate activity) in each byte to find the active inputs needing service.

END $.02
kenjj
 

Attachments

  • ADC for Multiple Inputs.JPG
    ADC for Multiple Inputs.JPG
    27.3 KB · Views: 230
  • Keypad Using ADC.JPG
    Keypad Using ADC.JPG
    27.9 KB · Views: 564
Because you will have more than 1 switch closed at any one time then a resistor ladder is out. Using shift registers is a possibility but the easiest way would be an 8 by 3 (or 4) matrix to give 24 (or 32) switches. You would need a diode per switch as per Mike K8LH's diagram above. Reading it is a doddle, make 1 of the 3 (or 4) column lines an output and set it low and read in 8 switch positions on the 8 bit port. If you are using microswitches then there is a simple way to incorporate the diode on the actual switch. Most pinballs use this method with an 8 by 8 matrix due to it's simplicity and reliability.

Mike.
 
kenjj said:
Another consideration is response time.

With the shifter, you have to latch in the external data, enable the output (if you have more than one source), clock in all those bits serially, store them as bytes//words, disable the output (if needed), then process all those bytes to make sense of what's happening.


If he used a 20MHz crystal that would result in 5,000,000 instructions per second. It is hard to see were the reads would be too slow.
 
3v0 said:
If he used a 20MHz crystal that would result in 5,000,000 instructions per second. It is hard to see were the reads would be too slow.

Yes, speed is hardly an issue really - bear in mind electronic piano and keyboards are simply scanned matrix keyboards, as is the computer keyboard we're all typing on.
 
You only need to scan the keboard in under 50ms or so, it's not a high speed thing. Using the CD4013 idea with a frequency of 200Hz can easilly do this and adding another ten keys just requires another MCU input.
 
Hank Fletcher said:
All things considered, would it be wasteful to just use an additional PIC? I could have one dedicated to controlling the robot, and the other dedicated to reading feedback sensors, and slave the second to the first via I2C. What do you think?


I would recommend just using multiple PICs...

The main reason is because it drops the complexity down a bunch...
Also like you said you could have each dedicated to different functions and well i always like the idea of 1 PIC controlling another.
 
Why on earth would you add extra chips. It makes no sense. All commercial products use a key matrix to read multiple keys. If you can't afford 12 pins for a 6 by 6 matrix then use a bigger pic. I would however still use an 8 by 4 matrix for the simplicity of programming.

BTW, am I missing something or does reading 24 switches require 3 x 8 bit shift registers? Three extra chips to save 10 pins?:confused:

Mike.
 
The 16F887 has two more I/O pins than the 16F877A if you use its internal RC OSC. I've just started using TQFP and a schmartboard, easy as pie.
I may even upgrate my old Cricket thermostat design to use a 64pin TQFP (128Segment LCD driver)
 
The 886 (the OP's choice) has 25 I/O pins. You can have a 4 by 4 key matrix to read 16 switches, 8 outputs to drive fingers and still have 9 pins left to do other stuff. If you want to use in circuit debugging then you still have 6 pins free.

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top