NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » "DIP switch" project (basic nerdkit)
December 20, 2011 by glendon |
Hi, My "DIP switch" project works but I'm stumped by a couple of things. Hope someone could throw some light on these:
Thanks. |
---|---|
December 20, 2011 by Striker |
I'm still new but I think I know the answer. The second question sort of relates to the first, so I'll answer that first. In order for the MCU to determine the logic state of a pin, it needs some sort of reference value (high/low). If nothing is connected to the pin, it has no value at all, so the MCU gets confused. The remedy is to either pull the voltage up with an external resistor or use the MCU's internal pull-ups to force it to '1' so it has a starting state. The sample code uses the internal pull-up, so that the pin is set to logic '1' when nothing is connected to it, and '0' when it's connected to GND. Thus, we have to reverse the way we think about the switch. When the switch is off, there is nothing connected to the pin, so it defaults to 1. When the switch is on, it connects the pin to GND and registers a 0. That's why it seems reversed to you. With that in mind, think back to your first question and how you left the LED connecting the pin to GND. This means that the pin will always read 0 since it always has something connected to it! There is such thing as a pull-down which would allow the switch to work the way you would think, but Google couldn't find me a common MCU with internal pull-downs. If you're curious about how we turn the pull up on: When a pin is set to OUTPUT, we send signals using PORTB/C/D. When a pin is set to INPUT, we receive signals using PINB/C/D and set the pull-up using PORTB/C/D |
December 20, 2011 by glendon |
Hi Striker, Your explanation really helps. Thanks a lot! While doing the "DIP switch" project, I researched on the term "high impedance" and "pull up/down registers". I found a really good article on tri-state buffer - http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/CompOrg/tristate.html - It talks about why "high impedance" is needed and how it's being achieved through tri-states. Now, for some reason, I was thinking that "pull up/down registers" has something to do with "high impedance" too. Your explanation about setting the pull-up register to 1 and that gives the pin a default value clears my thought quite a bit. So, does this mean that if the MCU has a built-in pull-down register, we should use that instead and set that to 1; and by doing so, when the DIP switches are OFF, they give a 0 (and if ON, give a 1)? Also, for the LED, does it mean that since its cathode is connected to GND and its anode is connected to MCU's pin 27, that pin is set to 0; thus overriding the fact that the pull-up register for pin 27 has been set? |
December 20, 2011 by Striker |
Deciding between a pull-up and pull-down may depend on what you're using the pin for. I think (and I'm just guessing) some components may send analog signals in such a way where you may need one or the other. But from what I can tell, internal pull-ups are standard (I think pull-downs may even be considered rare), so everything's probably geared towards integrating with pull-ups. But to answer your question, if the pin had an internal pull-down, then you could make the switch act the way you first assumed it would. The only difference is that the DIP switch would need to connect the pin to +5V, not GND. As for the LED, remember that connecting a pin to GND sets the pin to 0 when the internal pull-up is enabled. Since electricity only flows one way, the DIP switch's state does nothing to prevent the pin from crossing over the LED into GND, thus causing the pin to always read 0. A good way to visualize it would be to imagine that a pulled up pin is a bucket of water that gets filled by the MCU using water from +5V. The LED pokes a hole in the bucket and the water drains instantly into the GND. Toggling the DIP switch on/off will poke/fill its own hole in the bucket, but the LED's hole is still there, keeping the bucket empty. Thus, the pin always reads 0. If you were using a pull-down, you would reverse the roles. The MCU would drain the pin and the LED and DIP switch would fill it using +5V. |
December 21, 2011 by Striker |
I was tinkering around today and discovered that you can make your own pull-down by disabling the internal pull-up, connecting the pin to GND with a resistor (experiment with different values), then connecting the DIP switch to +5V. |
December 22, 2011 by glendon |
Ok, I just set up DIP switch 6 to +5V, placed a 10k resistor between MCU pin 28 and GND and turned off MCU pull-up resistor for MCU pin 28. Now when DIP switch 6 is OFF, it gives 0 (if ON, gives 1). My concept of pull-up resistor was a bit fuzzy before this. But, by implementing the pull-down resistor, the concept is now pretty clear to me. I also found this article that helps: http://dangerousprototypes.com/docs/Pull-up_resistors,_high_impedance_pins,_and_open_collector_buses Can't stop grinning right now! Partly because I managed to get the simple experiment working, but mostly because I felt I really learnt something. Thanks for your sharing, Striker. You've made my day! :) |
Please log in to post a reply.
Did you know that a microcontroller can measure an RC time constant? Learn more...
|