ADCIN - AD settings are wrong


Closed Thread
Results 1 to 6 of 6
  1. #1
    teverett's Avatar
    teverett Guest

    Default ADCIN - AD settings are wrong

    Photocell connected to PortA.0. I am using Picbasic Pro, ADCIN command. My programs just counts (incrementing a 2 or 4 “some-arbitrary” number) and then stops. It is suppose to tell me a digital value as read from the Photocell. Can someone tell me what I have done wrong? I am assuming I have my AD settings wrong but I do not know.

    Using internal clock 8 MHz
    10 bit resolution

    Terence

    ‘------- Code below ------------------

    '************************************************* ***************
    '* Name : Test-Analog Photosensor.BAS *
    '* Author : Terence *
    '* Notice : Copyright (c) 2006 *
    '* : All Rights Reserved *
    '* Date : 12/01/2006 *
    '* Version : *
    '* Notes : Chip 16F767 with 24C32A Mem *
    '* Reason : Testing A.0 ADCIN A/D converter *
    '* Photocell 4K - 11K Ohm 5.10mm PDV-P8101-ND (Digi-key) *
    '* PhotoCell Connected to Pin2 (PortA.0) + 10K to ground *
    '* PhotoCell also connected to 5 VDD *
    '* *
    '************************************************* ***************
    '---------------- Includes ----------------------
    Include "modedefs.bas"

    '---------------- Set I/O ----------------------
    TrisA = %00000001 ' set inputs and outputs for A's ports
    TrisB = %00000001 ' set inputs and outputs for B's ports
    TrisC = %00000000 ' set inputs and outputs for C's ports

    '---------------- Defines for Display -----------
    Define DEBUG_REG PORTB ' Debug Port A
    Define DEBUG_BIT 1 ' *** Debug pin Bit-1 ***
    Define DEBUG_BAUD 9600 ' *** Baud Rate 9600 8 mhz ***
    Define DEBUG_MODE 1 ' Set Serial Mode 1=Inverted
    Define DEBUG_PACING 300 ' Delay 'in Us' between characters sent
    ' Remember all defines must be in UPPERCASE !!!!!!!
    '---------------- Defines for ADCIN -----------
    Define ADC_BITS 8 ' Set resolution of conversion
    Define ADC_CLOCK 4 ' Set clock source (x/FOSC or FRC)
    Define ADC_SAMPLEUS 50 ' Set sampling time (in uS)

    '--------------- OSC Settings -------------------
    Osccon = $78 ' set the timing $68 4 MHz -$78 8 MHz
    Define OSC 8 ' 7E for 16F767

    '----------------- E_Byte --------------------------
    Define I2C_SCLOUT 1 'No need for the pullup resistor on the SCL pin

    '---------------- Define Analog ------------------
    Adcon0 = %11000111 ' set up for reading analog inputs
    Adcon1 = %01001110 ' set up for reading analog inputs
    Adcon2 = %00010000
    CMCON = 0 ' set up for reading analog inputs
    '-------------------- Var --------------------------
    ADVAL var word
    Addr Var Word ' 16-bit memory address within the Eeprom
    PA1 var PortA.1
    PA2 var PortA.2
    PA3 var PortA.3
    PA4 var PortA.4
    PA5 var PortA.5
    PB1 var PortB.1
    PB2 var PortB.1
    PB3 var PortB.1
    PB6 var PortB.1
    PB7 var PortB.1
    PC0 var PortC.1
    PC3 var PortC.1
    PC6 var PortC.1
    PC7 var PortC.1
    '---------------------- Init -------------------------
    Gosub SetUnUsedPinsToLow:
    '---------------------- Main --------------------------
    Main
    gosub ReadPhotoSensor
    Debug 254,1
    Debug 254,2
    Debug "Adval ",Dec Adval
    Pause 1000
    Goto Main
    '-------------------------- Read Photo -----------------------------
    ReadPhotoSensor: ' Determines Photo Sensor (On/Off)
    ADCIN 0, ADVAL ' Set PortA.0 to be read for Photo
    Return

    '----------------------- Set Used Pins -----------------------
    SetUnUsedPinsToLow:
    low PA1
    low PA2
    low PA3
    low PA4
    low PA5
    low PB1
    low PB2
    low PB3
    low PB6
    low PB7
    low PC0
    low PC3
    low PC6
    low PC7
    Return
    '------------------------ End ---------------------------
    End

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    1. You need 10-bit res, but you set it to 8.
    May not be the problem though.

    2. Add @ DEVICE PIC16F767, INTRC_OSC to the first line.
    Also this may not be the problem.
    ------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    how the Photocell is connected? any pull-up/down?

    1.CMCON=0? maybe CMCON=7.. but they're already off at POR so..
    2.your ADCON0 setting use Channel 8 but adcin should override this for you
    3.adcon1 should be set for right justified results
    4. the maximum input impedance for analog source is 2.5K (section 12.1) so.. maybe you'll need something between the PhotoCell and the PIC a/d input. sometimes it works, sometimes not. look at your Photocell specs or measure it. What's the value when there's no light? maybe a little bit high
    Last edited by mister_e; - 2nd December 2006 at 19:15.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    teverett's Avatar
    teverett Guest


    Did you find this post helpful? Yes | No

    Default It works... thank you

    I made the changes and you were both right. Impedance was wrong and the settings were wrong -- now it works … Thank you very much.

    Terence

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Now, you will not know which setting corrected it.


    ------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    teverett's Avatar
    teverett Guest


    Did you find this post helpful? Yes | No

    Default Which one...

    I believe that the CMCON = 7 was key. I had tried all the other settings backward and forward trying to get it working before posting to this forum (Adcon0, Adcon1, Adcon2) but I had forgot about CMCON.

    Once it was working - most of the resistors I tested on the Photocell (breadboard) came back with results. But once I started looking into the resistance values on my prototype, I found that an incorrect connection was made under a capacitor (SMT) which gave me false readings. I rewired and changed the settings - it then started working.

    So what this forum gave me was several clues to finding solutions two different problems. I really appreciate all the time and effort you guys put in to helping everyone here online.

    You guys make using PICs and Picbasic Pro possible!

    Thanks again!
    Terence

Similar Threads

  1. Problem with AD inputs at PIC16f873a
    By wurm in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2008, 10:01
  2. LM35DZ and 18F2455 settings
    By Mark J in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 25th January 2008, 08:31
  3. I neat some help on 12F675 ad
    By Kmt in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd November 2007, 18:48
  4. AD problems
    By Samoele in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 12th February 2007, 20:48
  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