# Need help to read 6 DIP switches on a single input

Started by February 9, 2004
 Need help to read 6 DIP switches on a single inputMicrochip’s Application note AN234 shows how a single input can sense multiple switches.  I need to determine combination of 6 bit switches but am not sure if this is too many switches for this concept to work. The app note shows two methods: 1. Using an ADC input and a switched resistor network, the value on a voltage divider varies.  Detect switch combinations. Resistors chosen so the voltage resulting from each switch combination is unique.  In short, the ADC input is tied to ground thru a resistor, and also tied high thru each switch that is in series with its companion resistor. In total, one 6 position DIP switch and seven resistors. 2. Comparator input can be used to charge a cap based on resistor network value with the PIC determining the charge time. I prefer the ADC method as the code is likely to be much easier. I am a PIC programmer beginner, so would like to choose the easiest method before digging in. Has anyone done this, perhaps know what resistor values to use?  Has anyone been able to get reliable readings from 6 or more switches or is this likely to be too many?  Does anyone have sample code? All thoughts and suggestions most welcome! Ransom Peek Fort Collins CO
 ----- Original Message ----- From: "ransom peek" <> To: <> Sent: Tuesday, February 10, 2004 12:42 AM Subject: [piclist] Need help to read 6 DIP switches on a single input > Microchip's Application note AN234 shows how a single input can sense > multiple switches. I need to determine combination of 6 bit switches > but am not sure if this is too many switches for this concept to work. > > The app note shows two methods: > > 1. Using an ADC input and a switched resistor network, the value on a > voltage divider varies. Detect switch combinations. Resistors chosen so > the voltage resulting from each switch combination is unique. In short, > the ADC input is tied to ground thru a resistor, and also tied high thru > each switch that is in series with its companion resistor. In total, one > 6 position DIP switch and seven resistors. > > 2. Comparator input can be used to charge a cap based on resistor > network value with the PIC determining the charge time. > > I prefer the ADC method as the code is likely to be much easier. > > I am a PIC programmer beginner, so would like to choose the easiest > method before digging in. > > Has anyone done this, perhaps know what resistor values to use? Has > anyone been able to get reliable readings from 6 or more switches or is > this likely to be too many? Does anyone have sample code? How about using an R-2R ladder network, as is used on DACs? You only need two values of resistor. It will work very well with 6 inputs, using 1% resistors. Leon -- Leon Heller, G1HSM Email: My low-cost Philips LPC210x ARM development system: http://webspace.webring.com/people/jl/leon_heller//lpc2104.html
 > I am a PIC programmer beginner, so would like to choose the easiest > method before digging in. 74HCT165 Dave
 The ADC method works fine as long as you do not use too many switches. The limiting factor is the precision of the resistors and the ADC. If you binary weigh the resistor you can detect any combination of switches. I am using a similar scheme for 3 switches, but using constant current sources for accuracy. Each current source is twice the previous. Then it is simply a matter of averaging the input(analog or digital) and doing a window detect on the ADC. I have sample code, but it is not universal because I am only looking for certain combinations, but not hard to do. Chad --- In , "ransom peek" wrote:> Microchip's Application note AN234 shows how a single input can sense > multiple switches. I need to determine combination of 6 bit switches > but am not sure if this is too many switches for this concept to work. > > The app note shows two methods: > > 1. Using an ADC input and a switched resistor network, the value on a > voltage divider varies. Detect switch combinations. Resistors chosen so > the voltage resulting from each switch combination is unique. In short, > the ADC input is tied to ground thru a resistor, and also tied high thru > each switch that is in series with its companion resistor. In total, one > 6 position DIP switch and seven resistors. > > 2. Comparator input can be used to charge a cap based on resistor > network value with the PIC determining the charge time. > > I prefer the ADC method as the code is likely to be much easier. > > I am a PIC programmer beginner, so would like to choose the easiest > method before digging in. > > Has anyone done this, perhaps know what resistor values to use? Has > anyone been able to get reliable readings from 6 or more switches or is > this likely to be too many? Does anyone have sample code? > All thoughts and suggestions most welcome! > > Ransom Peek > Fort Collins CO
 Thank you Leon Heller (R2R ladder), Dave Mucha (device add-on), and Chad Russel (excellent ADC binary ladder suggestion) for ideas to use one I/O for multiple bit switches.  Cannot add cost of addl IC, and cannot have constant current drain (no power off mode in design, just lets PIC go to sleep) thru voltage divider.  Have simplified requirement, so need to read just 3 bit switches with on PIC I/O.   The App Note AN234 shows the circuit below, using comparator.  Need to measure time it takes to charge capacitor.  Need to choose resistors to provide enough gap in charge times so that each switch combination is unique.   I think the idea is to change I/O to output ground, discharging capacitor.  Then change back to comparator input to let capacitor charge. Software to measure the time it takes the cap to charge to the “threshold”.  A value of 100 ohms for R5 is recommended.   Need some help with these questions:   1.  What kind of values should I try for R1, R2, R3, and C1?   2.  How long a delay to discharge the capacitor?   3.  What is meant by “threshold” level of cap charging up to?   For a 3 volt nominal voltage circuit (VDD=3 volts) what voltage level should I test for?   4.  How to know when threshold voltage is reached – do I just keep checking the comparator while timing or does the comparator have an interrupt upon reaching a threshold voltage?                                                                                                       VDD                                        (100 ohms)                                              !                                           R5                         /                             ! PIC COMPARATOR I/O-----/\/\/\/\/---------------------/     ---------/\/\/\/\/---------                                                     !         !      SW1           R1          !                                                     !         !                                      !                                            C1  ===       !        /                             !                                                     !         !------/      ---------/\/\/\/\/-------!                                                     !         !      SW2            R2         !                                                  GND      !                                      !                                                               !      /                               !                                                               !----/       ----------/\/\/\/\/-------!                                                                    SW3              R3 Ransom Peek