DIP Switch, PIC16F628a, JAL - basic newbie type question.

Status
Not open for further replies.

Hippogriff

Member
Hi all,

Earlier, I had asked about getting a potentiometer working with a PIC16F628a and JAL. I was told it was going to be difficult because the 16F628a doesn't have A to D capabilities, and I was advised to look at the 12F683. I am continuing to do that and have already ported my 16F628a code to the 12F683 successfully.

However, I also had a couple of DIP switches lying around and I thought it could be a good idea to try to use that and see if I could control some program settings from it via digital (on or off) inputs. I've seen a few schematics where the DIP switch input path is connected to +5V via a 10K Ohm resistor, before splitting off to a PIC pin set to read digital input, the other side of the switch goes to ground.

Is that how it must be? Because I set up the following very simple circuit and the LED lights up when I switch the first switch - so power is getting to the LED when I want it to, and not when I don't want it to. This all makes perfect sense to me, too - the LED lights up if it can be reached, but if it can't, it stays off.

**broken link removed**

So, next thought, can I employ the exact same principle with the PIC?

It seems not. When I code up the following very simple JAL program for the 16F628a, which sets pin 7 to be an input, pin 8 to blink an LED every two seconds and pin 9 to light an LED depending upon the switch position, I get some (what I consider) strange results.

Code:
pin_B1_direction = input  -- Pin RB1, Pin 7 (DIP 1)
pin_B2_direction = output -- Pin RB2, Pin 8 (Blue LED)
pin_B3_direction = output -- Pin RB3, Pin 9 (Blue LED)

alias DIP is pin_B1
alias LED1 is pin_B2
alias LED2 is pin_B3

forever loop
  LED1 = off
  LED2 = DIP
  _usec_delay (2000000)
  LED1 = on
  LED2 = DIP
  _usec_delay (2000000)
end loop

I end up with the LED that I think is supposed to be controlled by the DIP switch being on all the time. The LED on pin 8 is blinking every two seconds (as I would expect) but it doesn't seem to matter whether the DIP switch position is up or down for the LED attached to pin 9 - the LED is always on.

**broken link removed**

But the circuit makes sense to me. +5V is going into the DIP switch - if the switch isn't connected then nothing reaches the input pin 7 of the 16F628a, thus (I think) it should read that as a LOW, FALSE or OFF. If the switch is connected, then I'd expect the PIC input pin 7 to read a HIGH, TRUE or ON. Instead, it always seems to be ON.

I think this is pretty basic stuff... and I'm missing something rather obvious, I think. But, as is often the case with these things, you just don't know what you don't know. Is anyone able to take a quick scan at these images and simply tell me - "you're not using the DIP switch in the right way"?

As always, guidance appreciated.
 
I don't know Jal but my guess is that the simple way to fix the problem is to connect a 1k resistor from B1 to ground. A better way is to turn on the internal weak pullups and make the switch ground the pin.

Mike.
 
It's kind of hard to tell from the pictures and you probably already made sure of this but check and make sure that the leads of the two resistors aren't touching. It looks like from the pictures that they could be overlapping. If that isn't the issue, try using a pull down resistor with the DIP switch. This will ensure that the input pin goes down to ground or is "pulled down" to ground when the DIP switch is opened.

Here is a diagram showing both pull up and pull down resistor configurations:

**broken link removed**
 
It's kind of hard to tell from the pictures and you probably already made sure of this but check and make sure that the leads of the two resistors aren't touching. It looks like from the pictures that they could be overlapping.

It's OK, I made sure that they weren't. Sorry the images didn't make that obvious.


In both those diagrams, the switch eventually goes to ground... mine doesn't at all. The switch going to ground is also the case is other diagrams I've seen, but I found that wasn't necessary to merely light an LED, and I don't like to just copy things blindly. So... to get this working I guess I need the DIP switch to go to ground and a resistor somewhere between the DIP switch and the digital input pin of the PIC. That's two extra components / connections... and I'm as yet unsure why the PIC isn't getting 0 / LOW / OFF / FALSE if the switch isn't allowing stuff through to that pin, but seems to be getting 1 / HIGH / ON / TRUE all the time.

I do accept that it isn't working, and that I'll have to try the other way, but I'll not say that it makes sense to me... yet.
 
Last edited:
In both those diagrams, the switch eventually goes to ground
I'm not sure what you mean by "the switch goes to ground". What you should be concerned with is the voltage at the I/O pin. In the case of the pull up configuration, the I/O pin sees 5V while the switch is open and 0V when the switch is closed. In the case of the pull down configuration, the I/O pin sees 0V while the switch is open and 5V when the switch is closed. If you're still uncertain I urge to to get our a multimeter and test it.

Whether or not adding a pull down resistor will fix your problem I can't say but the basics of pull up and pull down configurations are as I stated above. You can easily change your code to light the LED up when the I/O pin sees 0V but based on how you described what you want to happen and the code snippet you provided, you want the pull down configuration. I don't know JAL so I can't comment on your syntax but the flow of the code makes sense.
 
I'm not sure what you mean by "the switch goes to ground".

What I was trying to mean is, +5V goes into one side of the switch and coming out the other side it goes to ground and the IO pin, whether that's via a 10K Ohm resistor or not. In my pictures, coming out of the bottom of the DIP switch it heads straight for input pin 7; never going to ground - I still expected input pin 7 to see either 5V or 0V - however, it seems to see 5V all the time (or something dicky that I don't get yet).


Either of these would be fine, it's just a boolean test anyway... whatever it saw, if the input changed then the LED should change at some point as the program looks at pin 7 every 2 seconds. That's all I'm looking for. At the moment it doesn't change at all, so I need to try and implement one of what you have suggested.
 
OK, I have it now - thanks for the assist there!

**broken link removed**

So, I now know how to use digital switches so that users can change the behaviour of my program and circuit to their liking. Takes a few more 'bits' (components) than I'd hope for, but that's life, I suppose. I expect that this is like riding a bike and I will now never forget...

Ah, how small things make small minds happy. Oh, and at least my Noddy code wasn't to blame!
 
Last edited:

I understood from the pictures how you hooked up the DIP switch and I understand how what I suggested you try was different. In your configuration, when the switch is closed the I/O pin sees 5V but when it is open, the pin is floating and floating doesn't necessarily equate to 0V. With the pull down resistor, the I/O pin will be forced or "pulled down" to 0V when the switch is open.
 
Aaaaaah! Hence "pull down"... so it pops up to +5V and, somehow, then just hangs around unless it's specifically dragged back to 0V.

Thanks!
 

Glad it's working. What you have there is a "pull up" configuration. The only difference with that from what I suggested is that now the LED is on (i.e. the I/O pin sees 5V) when the switch is open instead on the other way around. I shouldn't matter for your purposes just make sure you understand the difference as it may matter for future projects.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…