PIN state VS Variable read


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2007
    Posts
    4

    Question PIN state VS Variable read

    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

  2. #2
    Join Date
    Nov 2007
    Location
    Lake Villa Il.
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Pin State vs. Variable State

    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)

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 01:06
  2. Cleaning up code
    By Tobias in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2009, 08:14
  3. SEROUT WORD variable problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th April 2009, 12:20
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  5. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 18:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts