PORTA.4 as analog input


Closed Thread
Results 1 to 22 of 22
  1. #1
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31

    Default PORTA.4 as analog input

    Hi all,
    I'm having problem to use the porta.4 as the analog input for PIC16F88... When i connect the signal line (0.1v to 0.9v) to porta.4, the signal voltage will become 0v. I have tried to put a 10M series resistor to porta.4 and connect another 10M resistor to the ground. Anyone have the idea?

    I also tried to connect the signal line to other AN, for example AN2. Now the voltage on the signal will be .2 lesser than the original voltage. I really don't have any idea on this. Please help!!

    Thanks!

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Without knowing more, all I can tell you is that 10M is far too high a resistance for the input of a PIC to measure. If you want any kind of accuracy, you should keep the input impedance to below 4K or so. Use two 5K resistors
    in series between Vcc and GND and connect the junction to the A/D input, then read the A/D and see what you get.
    Charles Linquist

  3. #3
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Charles Linquis View Post
    Without knowing more, all I can tell you is that 10M is far too high a resistance for the input of a PIC to measure. If you want any kind of accuracy, you should keep the input impedance to below 4K or so. Use two 5K resistors
    in series between Vcc and GND and connect the junction to the A/D input, then read the A/D and see what you get.
    Thanks for your reply... I just try for 2.2k resistor but also give me the same result. The Signal voltage to measure also drop to 0v...

    From the internet surfing...... the PortA.4 is open collector.... maybe this is the reason.....? Or do i need to put a pull up resistor or something like that?

    Thanks!

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Have you properly configured the pin for INPUT (eg set the TRIS for that pin)?

    Have you DISABLED the COMPARATORS that use that pin?

    Have you ENABLED that pin for ANALOG input?

    What EXACTLY makes you think that RA4 is Open Collector? Some dubious second-hand information that you got off the internet? Just because RA4 is Open Collector on one PIC, doesn't mean it is on another... so find out if that statement is true or not.

    And if it was Open Collector, what has that got to do with using the pin for INPUT?

    There is ONLY ONE definitive source of reliable information for any PIC chip. That is the manufacturers DATASHEET downloadable from Microchips website. Go do it.

  5. #5
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply.

    Below are my declaration

    OSCCON = %01101110 ' 4 MHz, primary clock, internal RC

    PIR1 = %00000000 ' clear all flags particularly A/D IF bit 6
    SSPCON = %00000000
    SSPBUF = %00000000
    TXSTA = %00000000
    RCSTA = %00000000
    OPTION_REG = %00000000 'weak pullups enable

    'pin IO 76543210
    TrisB = %10000100
    TrisA = %01010101

    cmcon =7 'disable the comparator register


    ADCON1 = %10000000 ' right justified, AD Clock divided by 2,Vdd = Vref+, Vss = Vref-
    ANSEL=%1010100'only the RA4/AN4, RA2/AN2, RB7/AN6 as analog input
    CCP1CON = %00001100


    The adc are working on RA2 and RB7, but not on RA4.

    Please help.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    OK...

    Your settings look good, the ones that you have shown us... but here are some comments...

    If you have problems, follow the instructions in the Datasheet.

    They tell you that ANSEL should be set before ADCON1.

    Also, if you are setting an entire Register using the Binary notation %, and the Register is eight bits wide, then you really should be setting all eight bits... ANSEL=%01010100 and not just seven bits.

    You've not shown us your setting of ADCON0.

    Have you also checked that your RA4/AN4 pin is not shorted out against any other pin or part of your circuit? Measure the Resistance between that pin and 0v, then unplug the PIC and measure it again on the socket. This way you will determine whether the excessive loading is internal to the PIC (indicating a possible setting error or other problem with the PIC), of if it is external on your PCB or breadboard layout.

    Finally, beware of settings like...

    OPTION_REG = %00000000 'weak pullups enable

    That does a lot MORE than just set weak pull-up's.

    OPTION_REG.7=0 ' Set weak Pull-up's ONLY (leave the other settings alone!)

    Take a look at the OPTION REG in the Datasheet to see what I mean.

  7. #7
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Thank you Melanie,

    Thanks for your reply. I have learn something what i have missed. I will try them out and post the result later. Thanks!

    Really appreciate your help, Melanie

  8. #8
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Red face

    Hi all,
    I have tested the setting, but the PORTA.4 still pull down the signal to 0v.
    Just for your info, the signal is Voltage fluctuated between 0 to .9 v.

    don't know where i did it wrong......

  9. #9
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Anyone can explain to me:

    The maximum recommended impedance for analog sources is 10 kΩ. This is required to meet the pin
    leakage specification.


    1.What does this means?

    2. How to measure the impedance for analog sources?

    3.How to add or minus the analog sources?

    Thanks for the answer....

  10. #10
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Let me expain what is a complex subject in simple terms.

    Pretend that the Input pin of a PIC is a 10K Resistor connected between the pin and 0v (it isn't, but just for argument think of it this way). If you now feed that pin with a high-impedance source (eg +5v via a 1M Resistor), whilst you may measure +5v with a DMM on the end of the Resitor BEFORE you connect it, the instant you connect that Resistor to your PIC pin (10k), the voltage will collapse and you will only measure a few mV. Simple Ohms Law determies what has happened. Now this explaination isn't actually correct when dealing with impedance as we have to take the frequency of the signal into account, but will serve aquequately without going into a lecture on AC theory.

    Please post your electrical schematic, so that we can understand your circuit. It could be something very basic that you are doing wrong, or you could be doing everything correctly and may have a defective component. It is difficult to give advice when blind.

  11. #11
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie,
    First of all thanks for your reply. I really appreciate it.

    Attached is the schematic. The problem happens at PortA.4. However I already connect the incoming signal to PortB.7 and it is working.


    Include "modedefs.bas"
    Define OSC 8
    Disable Interrupt 'to disable the interrupt bcoz the PortB.0 is in used

    define INTRC_OSC, CCPMX_OFF, MCLR_OFF, LVP_OFF, PROTECT_ON
    define MCLR_OFF, LVP_OFF

    '@ __CONFIG _CONFIG1, _INTRC_OSC_NOCLKOUT&_CP_ALL

    @ __CONFIG _CONFIG1, _CCP1_RB3

    OSCCON = %01111110 ' 8 MHz, primary clock, internal RC

    PIR1 = %00000000 ' clear all flags particularly A/D IF bit 6
    SSPCON = %00000000
    SSPBUF = %00000000
    TXSTA = %00000000
    RCSTA = %00000000
    OPTION_REG.7 = 0 'weak pullups enable

    'pin IO 76543210
    TrisB = %10000100
    TrisA = %01010101

    cmcon =7 'disable the comparator register

    ANSEL=%01010100'only the RA4/AN4, RA2/AN2, RB7/AN6 as analog input
    ADCON1 = %10000000 ' right justified, AD Clock divided by 2,Vdd = Vref+, Vss = Vref-

    CCP1CON = %00001100

    CurrentLevelSensor con %00100000 'PortA.4/AN4
    WaterLevelSensor con %00010000 'PortA.2/AN2

    Start:
    ADCON0 = OX2Input
    gosub readadc
    pauseus 60
    Goto Start

    '---------------read the ADC here ----------------------------------------
    ReadADC:
    ox2Result = 0
    ADCON0.bit0 =1

    'Pauseus 150 'can try 50 ~ 200us
    Pause 250

    'start the convertion here
    ADCON0.bit2 = 1

    while ADCON0.bit2 = 1

    wend

    ox2Result.Lowbyte = ADRESL
    ox2Result.highbyte = ADRESH
    return
    '----------------------------
    Attached Images Attached Images  

  12. #12
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    So what do you have connected to J3?

    In one position (NC), the Relay contact at RLY2 just shorts out the two pins at J3 and not much else.

    In the other position (NO) that Relay contact cross connects RB3 (which is pulled up via internal weak pull-ups) via whatever you have plugged into J3 to RA4.

    At the moment I'm still puzzled what you are trying to achieve.

  13. #13
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Post

    Hi Melanie,

    This is my explanation:

    1.The J3.1 is the incoming signal(iV) that i want to measure, so i connect it to RA4.
    2.The iV also connect to a relay at NC. At the normal operation mode, the iV will connect to J3.2 via the NC on Rly2.
    3.In abnormal operation mode, the voltage signal from RB.3 will connect to J3.2 via the NO. That means the Rly2 need to be on and the iV need to be cut off from connecting to J3.2.
    4.The RB.3 output signal will also based on the iV but with some amendment before PWM on RB.3.

    Thanks!

  14. #14
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You're still not giving the whole picture.

    OK... so one end of your signal (let's call it the positive end) is connected to 13.1, but what is the other side of your signal connected to? Is it 13.2?

    If it's not connected to 13.2, then where is it connected?

    What is plugged into 13.2?

    What is this signal we are talking about from?

    Let's have the whole thing, not a pile of sockets where we are guessing what may or may not be connected properly.

  15. #15
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Hi,

    The signal is a output voltage form a sensor and it is connected to the ECU (engine control unit) that will pulsing the fuel injector. The pic is always monitoring the signal (on portA.4) and under some situation it will overwrite the signal that the sensor send to ECU by switching on the relay; the pic will PWM on PortB.3 to send the voltage to ECU.

    Hope this will make the thing clearer.

    Thanks!

  16. #16
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Try this...

    1. Unplug PIC from board.
    2. Measure volts on socket at RA4. You should have what you expect to see from your sensor. This signal should be the same at RA4, J3.1 and J3.2. If it isn't, you have a problem.

    3. Plug PIC back into board... voltage at RA4 should be what you had before. This signal should be the same at J3.1

    If there is a difference between the PIC being plugged-in or not, then...

    A. The PICs software is not configured for INPUT proprerly
    B. There is a defect with the PIC
    C. The Sensor is defective or otherwise has insufficient drive for the PIC.

    I have assumed that the Return for the Sensor is part of the Engine GROUND, and you have also connected 0v on your board to Engine GROUND.

  17. #17
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default O2's are high impedance

    My suspicions to the problem is "A" and "C", definitely "C".
    kblim, you need to buffer the O2 sensor output signal before sending it to the PIC. This keeps it from loading it down as you have seen when it almost worked on RA7 and RB2. I've provided an old circuit that I used back in the day of playing around with injector PW modification. It's basically the same thing you are doing but I used a pot where you want to use the PIC (before my eyes were opened ).
    Good luck on the original RA4 "short to ground" problem.
    Attached Images Attached Images  
    Louie

  18. #18
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Smile

    Hi LinkMTech,
    thanks for your help. I will try it out, but if you can provide me the simple schematic or guidance to setup the op-amps without offsetting the voltage, that will be good. I'm not familiar with the op-amps... Thanks again.


    Another problem i'm facing now is the ADC to measure some small voltage drop in the low ohmic resistor (for current sensing).

    A low ohmic resistor will be connected serially with the 13.8V on the left and a Load (RL) on the other end. When in operation mode, some voltage dropped (Vi) in the low ohmic resistor and the ADC will pickup Vi and convert into the current reading. The question are:

    1. How do i setup the ADC to measure the Vi. The source is 13.8V, but pic is powered by 5V in my design.

    2. The PIC is configured in VREF+ = AVDD, VREF- = AVSS to measure others voltage reading, what do i need to change for the VREF in order to measure Vi.

    Thanks! Really appreciate your help.

    .

  19. #19
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    KBLIM, in the attached schematic there are two suggestions for your project:

    First suggestion: Modify the relay driver, in your shematic the base current is the collector current. The base of the PNP transistor should be polarized (pulledup) and the base current limited with a resistor. I don't think these two transistors will last long wired as per your schematic.

    Second suggestion: How to solve, in a simple way, your current reading problem.

    Al.
    Attached Images Attached Images  
    All progress began with an idea

  20. #20
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Thank you, aratti.

    Really appreciate your suggestion 1 and 2. Your suggestion one really help me to protect my resistor and the opto couple.

    However please forgive me because didn't give the connection on low ohmic resistor to measure Vi. Because of the site condition, the resistor must be connected between the positive source and the load.

    13.8V 0-------------^^^Vi^^^------------====Load=====-------------0 0V

    Therefore the suggestion 2 is not suitable for my case. The 0V will not be suitable to be my Vref- in the ADC configuration. Just wonder how :

    1. How do i setup the ADC to measure the Vi. The source is 13.8V, but pic is powered by 5V in my design.

    2. The PIC is configured in VREF+ = AVDD, VREF- = AVSS to measure others voltage reading, what do i need to change for the VREF in order to measure Vi.


    Again very thank you to aratti!

  21. #21
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    If you cannot mount the resistor on low side, read this application note perhaps you will find the solution.
    Al.

    http://www.zetex.com/3.0/appnotes/apps/an39.pdf
    Last edited by aratti; - 1st December 2008 at 18:13.
    All progress began with an idea

  22. #22
    Join Date
    Jul 2005
    Location
    Selangor, Malaysia
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Thank you aratti, I will study the pdf.





    By the way, what is the maximum Vref-, Vref+ and the voltage the pic can measure?

Similar Threads

  1. pic18f analog comparator problem
    By david.silaghi in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th May 2009, 09:38
  2. Analog input on 18f252
    By kiteman in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th January 2009, 08:34
  3. Analog Input on PIC16F88
    By penelopepug in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2008, 22:49
  4. 16f88 - pin RA4 as analog input
    By savnik in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 15th December 2006, 13:55
  5. Analog pins for digital input
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 9th September 2005, 00:32

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts