ADC Problem with PIC16F88 Configuration


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    13

    Unhappy ADC Problem with PIC16F88 Configuration

    I am using PortA.0, PortA.1 and PortA.2 to read the individual values for my line following sensor. The sensor is a TCRT5000 device x 3.

    Here is my code, any ideas please ?

    @ DEVICE XT_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF

    DEFINE LOADER_USED 1
    Include "Modedefs.Bas"

    Define OSC 4 ' We're using a 4MHz Resonator


    define DEBUG_REG PORTB ' DEBUG PORT
    DEFINE DEBUG_BIT 6
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_PACING 500

    CLR CON 1 ' CLR LCD command
    LINE1 CON 128 ' LCD line #1
    LINE2 CON 192 ' LCD line #2
    LINE3 CON 148 ' LCD line #3
    LINE4 CON 212 ' LCD line #4
    INS CON 254 ' LCD command mode parameter


    DEFINE ADC_BITS 8 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

    TRISA = %11111111 'RA.0:3 Input
    TRISB = %00000000 'all PORTB output

    'ANSEL = %00000001 'only porta.0 is analogue
    ADCON1 = %00000010 'right justify the result(top bits are read as zero)
    CMCON = 7 'Turn off comparators

    piezo var portb.7
    LOW PIEZO

    msensor var word
    lsensor var word
    rsensor var word

    ' motor driver logic to 0 - low all port pins
    low portb.0
    low portb.1
    low portb.3
    low portb.4
    pause 500

    init:

    DEBUG INS,CLR
    pause 30

    DEBUG INS,LINE1,"L= R= " : PAUSE 30
    DEBUG INS,LINE2,"M= " : pause 30

    checkit:

    ANSEL = %00000001 ' Only porta.0 is analogue
    ADCON0 = %01000101 ' Control register 000 RA0/AN0

    adcin porta.0,lsensor
    DEBUG INS,LINE1+3, dec lsensor," " : pause 30

    '--------------------------------------------------------------------

    ANSEL = %00000010 'only porta.1 is analogue
    ADCON0 = %01001101 ' Control register 000 RA1/AN1

    adcin porta.1,msensor
    DEBUG INS,LINE2+3, dec msensor," " : PAUSE 30

    '--------------------------------------------------------------------

    ANSEL = %00000100 'only porta.2 is analogue
    ADCON0 = %01010101 ' Control register 000 RA1/AN1

    adcin porta.2,rsensor
    DEBUG INS,LINE1+11, dec rsensor," " : pause 30

    goto checkit

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


    Did you find this post helpful? Yes | No

    Default

    ADCIN must use the channel number, not the PIN name.

    ADCIN 0, YourVar ---> read PORTA.0, AN0
    ADCIN 2, YourVar ---> read PORTA.2, AN2
    ADCIN 6, YourVar ---> read PORTB.7, AN6

    HTH
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    You don't need to redefine ADCON0 and ANSEL all the time, only once at the top, and ADCIN should handle everything for you.
    Steve

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

  4. #4
    Join Date
    Dec 2008
    Posts
    13


    Did you find this post helpful? Yes | No

    Wink Pic16f88 a/d

    OK, I got it....

    I ended up doing like this in the end :

    @ DEVICE XT_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF

    DEFINE LOADER_USED 1
    Include "Modedefs.Bas"

    Define OSC 4 ' We're using a 4MHz Resonator


    define DEBUG_REG PORTB ' DEBUG PORT
    DEFINE DEBUG_BIT 6 ' *** Debug pin Bit ***
    DEFINE DEBUG_BAUD 2400 ' *** Debug Baud Rate ***
    Define DEBUG_MODE 1 ' Set Serial Mode 0=True, 1=Inverted
    Define DEBUG_PACING 500 ' Delay 'in Us' between characters sent

    CLR CON 1 ' CLR LCD command
    LINE1 CON 128 ' LCD line #1
    LINE2 CON 192 ' LCD line #2
    LINE3 CON 148 ' LCD line #3
    LINE4 CON 212 ' LCD line #4
    INS CON 254 ' LCD command mode parameter

    ' ** Port configurations **

    TX VAR PORTB.5 ' Bootloader connection for TX
    RX VAR PORTB.2 ' Bootloader connection for RX
    PIEZO var PORTB.7 ' On board Piezo
    IR_REC VAR PORTA.3 ' On board Infrared Receiver

    DEFINE ADC_BITS 8 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds


    LOW PIEZO

    ' motor driver logic to 0 - low all port pins
    low portb.0
    low portb.1
    low portb.3
    low portb.4
    pause 500

    ' *** Clear Serial LCD ***
    DEBUG Ins,clr : PAUSE 30

    ' *** Setup Line 1 and Line 2 ***
    DEBUG Ins,LINE1,"Left Mid Right" : PAUSE 30
    DEBUG ins,LINE2," " : PAUSE 30

    TRISA = %11111111 ' All PORTA Inputs
    TRISB = %00000000 ' All PORTB output

    ANSEL = %00000111 ' Set 0,1,2 to A/D, A/D clock = Frc

    RESULT VAR BYTE[3] ' 3-BYTE ARRAY STORAGE FOR 3 A/D CONVERSIONS
    C VAR BYTE ' ADC CHANNEL NUMBER BYTE VARIABLE

    MAIN:
    FOR C = 0 TO 2 ' 3-CHANNEL COUNTER LOOP
    ADCIN C, RESULT[C] ' Read all 3 channels
    PAUSE 10 ' Pause 10mS
    NEXT C
    PAUSE 250 ' PAUSE 250mS


    DEBUG ins,LINE2,dec RESULT[0]," "
    debug ins,line2+6,dec RESULT[1]," "
    debug ins,line2+11,dec result[2]," "

    GOTO MAIN ' DO IT AGAIN

    END ' END PROGRAM

  5. #5
    Join Date
    Dec 2008
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Pic16f88 a/d

    Thank you for your help MR. E

Similar Threads

  1. PIC18F2423, ADC problem
    By mistergh in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th March 2009, 01:31
  2. 16F88 ADC problem
    By greensasquatch in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 7th September 2008, 16:16
  3. Mutliple ADC problem with PIC16F877
    By andyto in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd October 2007, 17:47
  4. ADC problem with PIC16F917
    By eetech in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th March 2007, 21:22
  5. PIC16F88 problem with TOGGLE command?
    By russman613 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th September 2006, 23:31

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