DIP switch select delay


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce!

    your code helps alot, and it looks much simpler then what I've done... thanks again!

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    Code:
     Index = (PORTA >> 2) & $0F ' Read DIP switch AND mask result
    If I get this right . . translates to portA / 4 & %00001111 , what I do not get is what it means, are we doing math here to get a number ??? & = AND. What does it mean, Mask result?
    I am really trying to get my head around how this works, because it works pretty sweet, Thanks

    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default

    The OP show switch connected on RA<5:2>
    >>2 : yes it's divide by 4... but also easier to figure out when you use the 'Shift it 2 positions to right' term. Here it will shift the whole PORTA reading, 2 bits to the right... hence remove PORTA<1:0>

    the & $0F is indeed a Bitwise AND. This way it keep only bits <3:0>... here PORTA<5:2> once shifted to the right.
    Code:
    	                 76543210	
    Index=                  %10101111
    Index=Index >>2   ' now %00101011
    Index=Index & $0F ' now %00001011
    Last edited by mister_e; - 16th January 2008 at 07:25.
    Steve

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

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Thank You Mister_e,
    I will have to think on this awhile, but I see it throws out the 2 LSB and then throws out the 4 MSB, seems odd, I just have to get my thick head around it. Would it be possible to use say PortB and use only 4 ports in BCD fashion while using the rest as outputs? Something like
    Index = PORTB<3:0>?
    Thanks
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Joe,

    It's a lot simpler than it might look at first glance.

    Index = (PORTA >> 2) & $0F ' Read DIP switch AND mask result

    By shifting the value read in from the 8-bit porta register right by 2, we're just shifting the 4-bits of interest into the lower 4-bits of the Index variable. Next we AND the intermediate result with %00001111 so the Index variable can never be larger than 15.

    We do this because the lookup table has 16 entries. 0 to 15.

    LOOKUP2 Index,[200,300,400,500,600,700,800,900,1000,2000,3000,400 0,5000,6000,7000,8000],X

    If Index = 0 then 200 is placed in X. If Index = 15 then X = 8000.

    If you used RA0 to RA3 for the switches you wouldn't need to shift right by 2 since the switches would be connected to the lower 4-bits of the port. You would still want to AND the result with %00001111 however just to make sure the result in Index never exceeded 15 since the lookup table only has 16 entries. 0 to 15.

    You could of course use portb or any other port with at least 4 inputs for your switches.

    Say you already had the lower 4-bits of portb being used for something else, and you added 4 switches to RB4 to RB7.

    You could do something like Index = (PORTB >> 4) & $0F for the same effect we had above with the lookup table by limiting the result to
    0 - 15.

    If you use RB0 to RB3 for the switch inputs, then Index = PORTB & $0F is all you would need. The & $0F just makes sure Index stays within range by masking out the upper 4-bits.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce !
    Now I Get It !
    Thanks Also To Mister_e
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    Thanks Bruce !
    Now I Get It !
    Thanks Also To Mister_e
    I'd save the pins, and use an old pot and the POT command, if tight accuracy wasn't needed anyways.
    Save the pins, possibly save some code space, most likely save some PCB space.

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. RF Transmitter
    By et_Fong in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th October 2005, 16:34
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

Members who have read this thread : 0

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