CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pin remains high, Why?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Pin remains high, Why?
PostPosted: Tue May 10, 2011 9:43 am     Reply with quote

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
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

the plot thickens!
PostPosted: Tue May 10, 2011 10:39 am     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 11:48 am     Reply with quote

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
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue May 10, 2011 4:12 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group