ADCIN failure


Closed Thread
Results 1 to 7 of 7

Thread: ADCIN failure

  1. #1
    Join Date
    Nov 2006
    Posts
    70

    Angry ADCIN failure

    I'm trying to use an A/D converter for the first time and I'm totally stumped. I'm not doing anything fancy here--just want to convert a voltage and send the digital value to a pc with SEROUT2. The serial communication works fine, but the ADCIN value always prints out as 0. This is despite the fact that I can verify voltages on PORTA.0 between 0.5 and 4.5 V using a multimeter. I've pretty much copied my code from working examples--Here's what I've got:

    ' PIC16F874A with a 20MHz crystal
    ' Serial out pin connected to DB9 pin 2 through 100ohm
    ' Serial in pin connected to DB9 pin 3 through 22Kohm
    ' Ground connected to DB9 pin 5
    ' Input voltage for ADC connected to PORTA.0

    DEFINE OSC 20
    DEFINE ADC_BITS 10
    DEFINE ADC_SAMPLEUS 50

    B0 VAR BYTE 'just used for SERIN input and testing
    adcVal VAR WORD 'used for ADC value
    W1 VAR WORD 'used to test DEC function in SEROUT2

    TRISA = 255 'PORTA is all input
    ADCON1 = %10000000 'PORTA analog, no ref voltages, output right justified

    PAUSE 500
    W1 = 1022 'just to test DEC
    SEROUT2 1,16780,["Ready to go ", DEC W1, 10] 'Verify communication
    pause 10

    loop:
    SERIN2 2,16780,[B0] 'Read in a character on PORTB.2
    PAUSE 10
    Serout2 1,16780, ["Getting ADC value", 10]
    pause 10
    ADCIN 0, adcVal 'perform ADC and store 10 bits in adcVal
    pause 10
    SEROUT2 1,16780,["ADC value: ", DEC adcVal, 10] 'Send the decimal value
    Goto loop 'Start again
    END

    I know there is a lot of unnecessary stuff in there. I will parse it all down once the ADC is working. The serial output is always "ADC value: 0" no matter what voltage is going to PORTA.0. I've played around with 8-bit output and right vs. left justification, but no luck. The spec sheet for the PIC16F874A does not indicate anything unusual about the device (there is no ANSEL register, and PORTA.0 should work as an ADC input). Finally, I'm using factory-fresh PICs--same results on multiple ICs. Anybody got any ideas or resources I may not have turned up?

  2. #2
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    I just noticed that TRISA should be a six-bit register for my PIC. But using "TRISA = %111111" did not fix the problem.

  3. #3
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default Problem solved???

    OK. So I went back to the breadboard and started trying different pins. By a fluke (actually by mistake) I got it working when I used "ADCIN 1, adcVal" with the voltage input attached to PORTA.0 (NOT PORTA.1!!!). So what's going on here? Almost all of the examples of ADCIN I have seen use channel 0 and connect input to PORTA.0. Also, the first use of ADCIN always gives a 0. After that it seems to work fine. Does this make sense to anybody?

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


    Did you find this post helpful? Yes | No

    Default

    ADCIN 0 should for sure read AN0. Do you have your analog input connected to pin #2 on
    your 16F874A?

    If so, which version of PBP are you using?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default PBP version

    The voltage input is indeed attached to pin 2--right next to MCLR. All the other pins on PORTA are floating and read out at about .9 volts.

    I'm using PBP 2.47 issued by Reynolds Electronics on 11-9-06 (Cheers Bruce).

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


    Did you find this post helpful? Yes | No

    Default

    Strange problem. I can run pretty much the same thing you have here on an 877A and it
    works fine.
    Code:
    DEFINE OSC 20
    @ DEVICE HS_OSC
    
    DEFINE ADC_BITS 10
    DEFINE ADC_SAMPLEUS 50
    
    B0 VAR BYTE 'just used for SERIN input and testing
    adcVal VAR WORD 'used for ADC value
    W1 VAR WORD 'used to test DEC function in SEROUT2
    
    TRISA = 255 'PORTA is all input
    ADCON1 = %10000000 'PORTA analog, no ref voltages, output right justified
    
      PAUSE 500
      W1 = 1022 'just to test DEC
      SEROUT2 PORTC.6,84,["Ready to go ", DEC W1, 10] 'Verify communication
      pause 10
    
    loop: 
      SERIN2 PORTC.7,84,[B0] 'Read in a character on PORTB.2
      PAUSE 10
      Serout2 PORTC.6,84, ["Getting ADC value", 10]
      pause 10
      ADCIN 0, adcVal 'perform ADC and store 10 bits in adcVal
      pause 10
      SEROUT2 PORTC.6,84,["ADC value: ", DEC adcVal, 10] 'Send the decimal value
      Goto loop 'Start again
      
      END
    On a LAB-X1 board it reads the POT on RA0. Changing to ADCIN 1 it reads the POT on RA1.

    What happens if you disable A/D and try blinking an LED on RA0?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Yeah, I can blink an LED no problem. Of course that involves setting PORTA to output etc...

    Well, I'll just go with what I have for now. This is a prototyping project, and it is clear to me now that the PIC16F874A is overkill--I will use a smaller, simpler PIC for the final version, and hopefully this problem will go away.

    Thanks for the help.

Similar Threads

  1. ADCIN and PIC18F4331 Arghhhhh !
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th December 2010, 20:27
  2. How to make adcin stable?
    By Pic2008 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th January 2009, 07:41
  3. ADCIN Problem
    By JavPar in forum General
    Replies: 33
    Last Post: - 20th January 2009, 03:36
  4. ADCIN - AD settings are wrong
    By teverett in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th December 2006, 16:32
  5. help using adcin
    By harryweb in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2006, 07:46

Members who have read this thread : 0

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