Working with Analog Pins in PIC16F877 (C language)

Discussion in 'Embedded Systems and Microcontrollers' started by Gtofig, Feb 6, 2008.

  1. Gtofig

    Thread Starter Member

    Jan 4, 2008
    Hello. I am very new to PIC programming. I am using MikroC compiler for PIC. The problem I have is:
    I have a distance sensor, which will give me a range of voltages from 0 to 3 Volts. I need to get this value as a float variable in the program, so that I can use it in my C program. How do I work with Analog inputs? I think I should connect sensor to the RA0, for example. Then I set TRISA.F0=1 (declare as input). What should I do next? How to read the sensor data into variable of float type? I know that I should somehow tell the PIC that it is analog input, not digital.
    Any advise would be appreciated.
    Thank you,
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    The 16F877 has 8 A to D inputs that make analog voltages into digital values. The info about them appears starting on page 111 of the data sheet.
  3. Gtofig

    Thread Starter Member

    Jan 4, 2008
    Thank you. This seems to be very well explained there indeed.
  4. ramar

    New Member

    Jan 13, 2010
    Hi ,i m using pic 16f877a for my project.i want to know maximum input current and voltage rating of pic 16f877a(i want to measure input current of 5Amps).if i need to reduce means,what type of circuit should i need to use ,,,,Please give suggestion..
  5. retched

    AAC Fanatic!

    Dec 5, 2009
    5 amps will fry your pic. did you mean 5 mA?

    If not, you want to divide your current by 100 using a circuit, then measure that. In your code you can multiply the measured number by 100 to get the actual number.

    Here is information on current dividers:

    also, you should have the datasheet for your PIC onhand at all times. You can find all the information on current maximums and many other important facts.
    the datasheet is located here:

    PAGE 1 shows the MAX sink-source current at 25 mA
    Last edited: Jan 13, 2010
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    Your PIC do not have a current sensing input. But analog to digital converters for voltage. Current measurement is usually done by inserting a small resistor in the current path. Then we measure the resulting voltage drop over this resistor. An Instrumentation amplifier is ideal for such a task.