PDA

View Full Version : DIP switch select delay



mongater
- 19th November 2007, 15:41
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!

mat janssen
- 19th November 2007, 17:00
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

Bruce
- 19th November 2007, 21:21
It might help if someone knew what DIL 18 was.

mister_e
- 19th November 2007, 23:12
it's a 18 pins device with a DIL package :D I'll bet it's a black one ;)

BrianT
- 20th November 2007, 00:47
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.

mongater
- 20th November 2007, 08:58
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!

Bruce
- 20th November 2007, 14:38
Something like this is where LOOKUP2 comes in handy.


@ 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.

mongater
- 21st November 2007, 11:27
Thanks Bruce!

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

Archangel
- 16th January 2008, 04:59
Hi Bruce,


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

mister_e
- 16th January 2008, 07:15
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.

76543210
Index= %10101111
Index=Index >>2 ' now %00101011
Index=Index & $0F ' now %00001011

Archangel
- 16th January 2008, 07:57
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

Bruce
- 16th January 2008, 12:44
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.

Archangel
- 17th January 2008, 01:44
Thanks Bruce !
Now I Get It !
Thanks Also To Mister_e
:)

skimask
- 17th January 2008, 03:13
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.

Archangel
- 17th January 2008, 05:50
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

skimask
- 17th January 2008, 14:13
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...