DIP switch select delay


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

    Default DIP switch select delay

    HI, I'm a beginner in PIC, I want to do a simple counter project using DIP switch to select Delay, the small project is actually working already with 2 x 7 segment display.. but I need a selectable startup delay.But I dunno how to do it.. keeps failing!
    here is what I wanted..
    dip1=on, dip2=on, dip3=on, dip4=on == 8secs delay
    dip1=on, dip2=off, dip3=off, dip4=off == 7secs delay
    dip1=on, dip2=on, dip3=off, dip4=off == 6secs delay
    dip1=off, dip2=off, dip3=off, dip4=off == 5secs delay
    and so on...
    I've attach a example sch.
    maybe you guys might know some reference code or similar(PICBASIC)I can view?

    Thanks!
    Attached Images Attached Images  

  2. #2
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    If dip1 = 1 and dip2 = 1 and dip3 = 1 and dip4 = 1 then
    pause 8000
    else
    if dip1 = 0 etc.



    endif

    or
    something var byte
    something = porta &%00111100
    if something = 60 then
    pause 8000
    else
    if something = etc.

    endif

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


    Did you find this post helpful? Yes | No

    Default

    It might help if someone knew what DIL 18 was.
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Default

    it's a 18 pins device with a DIL package I'll bet it's a black one
    Steve

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

  5. #5


    Did you find this post helpful? Yes | No

    Default Might not be black

    Some are dark brown. Some even have writing on them - I wonder what this one is?

    Another clueless student trying to fudge a class project by the looks of it.

  6. #6
    Join Date
    Nov 2007
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Yes it's 16F88 .. and it's 18pin... BLACK in color comes with some wording on top of it!! but I use my finger's to rub it off anyway.. and now it's unknown chips , so I put DIL18

    Thanks mat janssen , this is what I've done first... I'm sure somewhere must have coded wrongly.. I'll check tonite again!

    Thanks!

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


    Did you find this post helpful? Yes | No

    Default

    Something like this is where LOOKUP2 comes in handy.
    Code:
        @ DEVICE PIC16F88, WDT_OFF,INTRC_OSC,MCLR_OFF,BOD_ON,LVP_OFF,PWRT_ON,PROTECT_OFF
    
        DEFINE  OSC 8
        DEFINE  NO_CLRWDT 1
        OSCCON  = %01110000 ' 8MHz
        CMCON   = 7	        ' disable analog comparators
        ANSEL   = 0         ' disable A/D module, all digital
        X VAR WORD
        Index VAR BYTE
    
        LED VAR PORTB.0    
        TRISA = %00111100   ' RA2 to RA5 inputs for DIP switch
        PORTB   = 0
        TRISB   = %11111110 ' RB0 for LED
        OPTION_REG = %11111111
        
        WHILE OSCCON.2 = 0  ' wait for internal osc stable before moving on
        WEND
        
    Main:
        Index = (PORTA >> 2) & $0F ' Read DIP switch AND mask result
        LOOKUP2 Index,[200,300,400,500,600,700,800,900,1000,2000,_
        3000,4000,5000,6000,7000,8000],X
        HIGH LED
        PAUSE X
        LOW LED
        PAUSE X
        GOTO Main
    
        END
    Change the delay periods in the table to suite your app.
    Regards,

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

  8. #8
    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!

  9. #9
    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.

  10. #10
    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.

  11. #11
    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.

  12. #12
    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

  13. #13
    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.

  14. #14
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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.
    I thought about that, I really want to port this over to a 12F675 for a dedicated gadget. That is why I asked about using the other pins as outputs . . . so much to learn . . . so many cool tricks . . . so many Giving Caring Wonderful people . . . My 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.

  16. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    I thought about that, I really want to port this over to a 12F675 for a dedicated gadget. That is why I asked about using the other pins as outputs . . .
    AND, the POT command doesn't require an analog capable pin.
    Bonus points...

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 : 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