PDA

View Full Version : dip switchs & inputs



grounded
- 18th September 2006, 12:41
I have a little project were I need to use dip switchs as inputs.currect me if I'm wrong
but if these switch are left on. would this not be constant current flow and extra drain on the battery?
if so would this work or do you have a better way.
if I set these pins as inputs,pull up resistors on @ power up read there state and put it in a Var and then make them an out put and set to 0.
THANK YOU

example:
dip switch on gpio.2 and gpio.4

@ DEVICE pic12F629
@ DEVICE pic12F629, INTRC_OSC_NOCLKOUT
@ DEVICE pic12F629, WDT_ON
@ DEVICE pic12F629, MCLR_OFF
@ DEVICE pic12F629, CPD_OFF
@ DEVICE pic12F629, BOD_OFF
@ DEVICE pic12F629, PWRT_ON
@ DEVICE pic12F629, PROTECT_OFF
DEFINE OSC 4
Pause 5000 'Allow pic to Stabilize

TRISIO = %00011100 'MAKE GPIO.2,.4 INPUTS (GPIO.3 IS ALWAY A INPUT)
OPTION_REG =%00000000 'WEAK PULLS ENABLED
WPU = %00010100 'GPIO.2,.4 WEAK PULL UPS (GPIO.3 HAS EXTERN.RESISTOR)
CMCON = 7


SYMBOL RED = GPIO.0 'RED LED
SYMBOL NA1 = GPIO.1 'N/A
SYMBOL DIP1 = GPIO.2 'DIP SWITCH 1 MODE,IF MADE PIN IS LOW
SYMBOL NA2 =GPIO.3 'N/A
SYMBOL DIP2 = GPIO.4 'DIP SWITCH 2 PC, IF MADE PIN IS LOW
SYMBOL GREEN = GPIO.5 'GREEN LED
RED=0
GREEN=0
MODE VAR BYTE
MODE =0

PC VAR BYTE
PC=0

IF DIP1 =0 Then GoTo LONG
GoTo CHECKIT
LONG:
MODE= MODE +2

CHECKIT
IF DIP2 =0 Then GoTo SHORT
GoTo CHANGE
SHORT:
PC=PC+2

CHANGE:
TRISIO = %00001000 'MAKE ALL OUTPUTS EXCEPT GPIO.3
'RESISTORS ARE AUTO.OFF WHEN MADE OUTPUTS
DIP1=0
DIP2=0

MAIN:
' (SOME CODE HERE DOING WHAT EVER)
IF MODE=>1 Then RED=1

IF PC=>1 Then GREEN =1

GoTo MAIN

keithdoxey
- 18th September 2006, 12:58
How about using external resistors and driving them from a spare output pin when you want to read the switches. That way there is only ever power supplied to the resistors just before you read them. After you have read the switches, turn the output pin off again.

mister_e
- 18th September 2006, 13:41
The extra i/o to supply external pull-up will work and it's very common.

You could also disable internal pull-up if you decide to use them. But it will leave the i/o floating... Maybe set them as output when you don't need to read from them. Just make sure of the I/O and the switch level to avoid 'short circuit'

grounded
- 18th September 2006, 18:25
thank you both but Iam not sure I'm following what your saying. I'm still a newbee at this . In this little project it will start from a interupt and depending on how the switchs are set it will decide what to do then go back to sleep til next interupt.
But Please for give me and please expain in a little more detail for this simple mind of mine
thanks

Dave
- 18th September 2006, 20:55
grounded, Attached is a portion of a circuit I have used in the past to preset the port for a product. All you need to do is replace the switch SW9 pin 1 or 3 with an appropriate port pin from the micro and when you want to read the 8 position dip switch just place a high level on it then read the port then place it back low.

Dave Purola,
N8NTA

grounded
- 19th September 2006, 13:37
well that was headed right over my head. it just did'nt catch what yall were saying untill I looked at dave's attachment then it clicked.
thank again.