View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
Detecting pin low and not connected? |
Posted: Sat Jul 09, 2005 3:12 am |
|
|
Hi
I have a pin that is either connected to H, or L, or not connected. I would like the PIC16F873 to view the L or "not connected" as L.
I have used:
Code: | if(!input(PIN_C2)){ |
this works sometimes, but sometimes it does not.
Can someone please help me?
Thank you
arrow |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Sat Jul 09, 2005 3:25 am |
|
|
You should pull-down this pin to GND with a resistor. The value should be high enough to let the circuit that drives it (when connected) easily drive it high. A value of 100k is usually OK. Depends on optimum of the max. sourced current of the connected circuit, the need for low power cunsumption, and the noise immunity issues.
The PIC will see it as L (both when not connected and when the connected circuit sets it to L) |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Sat Jul 09, 2005 3:29 am |
|
|
Hi Libor
Thank you for your suggestion.
Can I put in say a 1M resistor- this will limit current even further, and make my battery application last even longer?
Regards
arrow |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Sat Jul 09, 2005 3:57 am |
|
|
Yes, 1M will be also fine. But only if you're not using out-of the box long wires to connect your circuit to the PIC. Long wires can act as antennas, and might give you false pulses against the 1M in an electrically noisy environment.
There are many other issues to make your battery application last longer, e.g. make the communication protocol on this pin so, that it will not stuck in High state for prolonged periods, so it will not have to drive current against this resistor for long time. If this line is mainly in high state, then consider to make the opposite, pull it up to VCC with a resistor and let the circuit drive it low, if the low state is the one that is less frequent in time. This pull-high solution is also better suited for a circuit that has an open-collector output, so can work on different voltage levels.
You should also consider using the sleep state of the PIC, etc. |
|
|
valemike Guest
|
|
Posted: Sun Jul 10, 2005 5:21 pm |
|
|
I'd say exactly what Libor says.
But then, i just recalled the following app note called "Tips N' Tricks"...
http://ww1.microchip.com/downloads/en/DeviceDoc/40040b.pdf
Look at Tip #3: "Reading Three States from One Pin". You actually have to add a capacitor though. It's a pretty clever trick. |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Mon Jul 11, 2005 12:42 am |
|
|
Hi Libor and ValeMike
Thank you for your replies. I have tried what Libor suggests and it works well.
I have one further question: If after poling the pin_C2 I find it H, and if I set the output on pin_C2 H then, will it limit my current draw? (even though pin_C2=H)
(And if pin_C2 is L, I set pin_C2 L- will that limit the current consumption and prolong battery life)?
Something like this:
Code: | if(input(PIN_C2)){
output_high(PIN_C2);
|
Regards
arrow |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Mon Jul 11, 2005 3:30 am |
|
|
1. It is dangerous thing, if your circuit suddenly wants to drive the pin Low and you have set it High then the stronger amp circuit will kill the other. However you can limit this current by adding a resistor in series with the pin.
Note: Anyway I suggest adding a series resistor with a value that limits the current thru the pin well below 20mA (or even lower depending on your connected circuit's max. load), so you can not accidentaly destroy your circuits when testing/learning. A 1k resistor will do.
2. It was not clear whether both the connected circuit and your PIC operate from battery, I suppose both. If you make the PIC to 'alleviate' the connected circuit's job by sourcing the current itself thru the pull-down resistor, the PIC itself will drive more current from the battery with the same plus as your connected circuit would have to. (There's no free lunch here)
3. If you are concerned with the power consumption that much, you poll the pin's state periodically, and have one more spare pin (lets' call it C1), there's a solution: Connect the the pull-down resistor between C2 and C1. The C1 will act as a 'controlled GND', you can make it low (pull-down enabled), make it high (pull-up enabled), and let it float by making it input (pull-anywhere disabled) = no extra current thru the pull resistor.
Most of the time (let's call it 'unasked state') you keep the pull-anywhere disabled. You can enable the pull-down function just before polling the pin for a short time only, and disabling it after the polling. (add some delay in between for the pin's state to settle, to shorten the setting-time, and make the device more noise-proof I would use a less value resistor here, not 1M but rather 100k, don't worry it'll be used only in a short time-slice anyway: using a 100k in a 0.1% duty-cycle would equal as using a 100M continuously).
To lessen power comsumption you should make the polling (aka duty-cycle) as rare and as short as possible, though it depends on what are you monitoring. I don't know what is your application, you can also make an adapting algorithm, make the polling rare if nothing happens (kind of a sleep state), and can speed up if communicating. (you might recall those devices that only 'wake-up' at the first keypress)
Note: If you are using the same pins in both directions (input and output) I would suggest to use the compiler's FAST_IO directive, so you can explicitly control each pin's direction, instead of the confusing implicit input-output commands that make the compiler change direction. I always use this method, I explicitly set the pin's directions in all my applications with the SET_TRIS... commands, so I am always aware of what direction the pin is set to at any place of the code.
Last edited by libor on Mon Jul 11, 2005 3:52 am; edited 1 time in total |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Mon Jul 11, 2005 3:51 am |
|
|
Hi Libor
Thank you very much for your thorough response.
My application requires to poll the state of PIN_C2 only once.
If it is H I do one thing (and then switch off the device),
if it is L I do something else (and switch off the device).
Your idea of putting a high value resistor on PIN_C2 is a very good idea.
It is thus always L. If a H comes onto PIN_C2 the pin will see it as H.
The lost current here will be 3.3V/R of pull down.
I am wondering if I drive PIN_C2 H (or should it be L?) after I poll it that one time, and make the correct decision, I will achieve savings in current?
To minimize current consumption, should I be doing anything to the other not used pins?
Thank you once again
Regards
arrow |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Mon Jul 11, 2005 3:52 am |
|
|
Hi Libor
Thank you very much for your thorough response.
My application requires to poll the state of PIN_C2 only once.
If it is H I do one thing (and then switch off the device),
if it is L I do something else (and switch off the device).
Your idea of putting a high value resistor on PIN_C2 is a very good idea.
It is thus always L. If a H comes onto PIN_C2 the pin will see it as H.
The lost current here will be 3.3V/R of pull down.
I am wondering if I drive PIN_C2 H (or should it be L?) after I poll it that one time, and make the correct decision, I will achieve savings in current?
To minimize current consumption, should I be doing anything to the other not used pins?
Thank you once again
Regards
arrow |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Mon Jul 11, 2005 4:03 am |
|
|
What device are you switching off ? Only the 'connected device' ? Or both (PIC's self-kill action) ? Who will switch it on again and how ?
The PIC has mains supply = unlimited power ? If it operates from the same battery, than you will lose the same in the PIC as you gain in the connected circuit.
You should drive the unused pins either low or high, or make them input, and pull them anywhere with resistors, main thing, do not leave them floating. |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Mon Jul 11, 2005 4:11 am |
|
|
Hi Libor
To switch thigns off I send a H to my voltage regulator- what that does is it goes into hybernate- and my current consumption falls down to 160uA.
If there is a way to switch off the PIC programatically- please can you tell me how to do this?
To switch things on again- I just flip the switch to "off" and then "on" again- this starts my new cycle.
At the moment all my unused pins are floating- I shall change that- I will drive them all L- thank you for the suggestion.
One more question: is more current drawn by setting a pin H versus setting it L? (or does it not matter?)
Thank you
arrow |
|
|
|