PDA

View Full Version : PIN state VS Variable read



cburtis
- 13th December 2007, 18:55
I am putting together a project which will have some options selected by dip switch settings. I have enough pins to dedicate a pin to each dip switch.
My question: is it better to scan the dip switches at startup and then store those values in a variable or is it OK to look at the pins that the dip switches are connected to every time I want to check the option? Is it slower to read the PIN state as opposed to reading the variable?

Thanks

Chris

Sneaky-geek
- 13th December 2007, 23:33
Hello,
I do not know how much as I am a nubee at this(this is my first response/1st post).
But the are several questions that you must think about and answer.
1) Does the input data ever change while the pic is running?

If the data to be read CAN change or MUST be 'allowed' to change then you want to read
the data after the pic starts.

2) Dip switch or BCD switch?

Using a DIP switch, (a 4 switch ((position)) used here for example, you have a maximum of
4 inputs for your 4 bits. I use a lot of Greyhill 94 HAB 16 switches. These are 16 position
4 BIT output units. So as you can see that this only uses 4 pins on the chip but you can
have 16 unique input conditions. This save the number of inputs used.

The downside is a few more line of code to decode what each position of the switch
represents. So again you need to figure out exactly what you need your inputs to do.

I hope this helps and not muddy the waters.

73,
Terry, K9HA (sneaky-geek)

mister_e
- 13th December 2007, 23:55
Well, several way to do it.
1)You could use a Timer interrupt which will read your dipswitch all the time, so in your main code you just need to read the DipSwitch variable.

2) Create a simple subroutine which will read the dipswitch when YOU decide

Now what's the better solution to read them... i will prefer to read the whole port where my dipswitch are attach to and only keep the interesting bits . Let's say i have 4 dipswitch connected to PORTB<3:0> i'll use

DipSwitch = PORTB & $0F

the & $0F is and bitwise AND with 0F hex. this mask <7:4> bit, and keep only <3:0>.. where my dipswitch are connected.

You could still use
DipSwitch.3=PORTB.3
DipSwitch.2=PORTB.2
DipSwitch.1=PORTB.1
DipSwitch.0=PORTB.0

Both do the same job.. i prefer the first one. Which is the faster.. i didn't measure it yet. I feel the first is faster...

HTH

Dave
- 14th December 2007, 11:54
cburtis, Its really up to the way your program is meant to work. If the information from the dip switches can change during the operation of the code then yes you will have to read them during the operation. However if they are some type of preset or an address that is used during the constant operation of the program then they only need to be read once. In the past I have used diodes to common source the switch's so I can also use the pins for other purposes during the operation. The diodes (anodes) are connected to a single pin of the processor so that during the initialization of the code I would place this pin high and read the switches. After reading them I place the line low and away we go.....

Dave Purola,
N8NTA