View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Pin remains high, Why? |
Posted: Tue May 10, 2011 9:43 am |
|
|
hi all
I am trying to do a Battery meter for my project.
Before you tell me to read all other posts (I know there are a lot of them and I have read them),
I'm trying to do somthing a bit different.
I do not want to spend more than 1 (one) pin for this purpose and use as little power as possible.....
So my idea is to:
1) set a pin to Digital I/O
2) charge a capacitor to a reference level with the circuit below
3) switch the pin to an analoge input
4) read the capacitor value and then do the Vbat=[(1023*Vref)/ADC_reading]
Code: |
Vcc = Vbat
---
|
|
-------------------
|
|
|
|
|
| PIC 16F876A
1k |
-------------/\/\/\----| A/D input (AN3)
| | |
--- Green --- C1 |
\ / LED --- 100uF |
--- ~1.19 | |
| | -------------------
| | |
/// /// ///
GND GND GND |
(I have build the circuit as above and coded a routine as described).
I start with the ADC off,
set the port to digital
set the pin high
wait 50ms
set up the ADC ports as analoge
read the voltage
turn the ADC off
LED remains on.....this is my problem
I've gotten it to work by turning the pin low afterwards.
Why does the pin remain as an output and high, when I have explicitly set the port to be analog ?
I do seem to be able to ready the Voltage properly though!
If I set a port as digital, do something and then set the port as analog input, the state at which the pin was prior to being switched to analog should be ignored or overwriten ..... any one care to explain?
If you have any suggestions to my circuit, please advise...(I know there are zener diodes... I didn't have any at hand... and I kinda like the LED idea because if provides visual feedback aswell.)
Thanks for your help...
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
the plot thickens! |
Posted: Tue May 10, 2011 10:39 am |
|
|
hi again,
so... the datasheet says:
Quote: | The TRISA register controls the direction of the port
pins even when they are being used as analog inputs.
The user must ensure the bits in the TRISA register are
maintained set when using them as analog inputs. |
that would explain why it remains set....(sorry)
instead of directly changing my pin from an digital output to an analoge input..
i should:
set as digital output
charge capacitor
set as digital input (for high impendance)
set as analogue input....
.... did i get this right?
G.. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 10, 2011 11:48 am |
|
|
Yes, you need to use output_float() to turn the pin into an input pin.
Here is a related example for ultra low power wake-up. Notice how
the my_sleep_ulpwu() function sets pin A0 high, waits for a delay,
and then sets it to be an input:
http://www.ccsinfo.com/forum/viewtopic.php?t=42076&start=1 |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue May 10, 2011 4:12 pm |
|
|
PCM: thank you for your kind response... very much appreciated!
ill test this and post my code for all to have...
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
|