ADC not working - I'm a new member


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    I don't have my book handy, but if you look at the ADCIN section of the PBP manual, there are several DEFINES you need in order to use the ADCIN command. Also to post code in the cool code box, you need to use tags. Really simple:
    [code[ <----should be the other, but then you won't see the tag
    All your code here
    [/code[ <----Again the second bracket should be } without the shift key.

    Maje sense?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  2. #2
    Join Date
    Jun 2011
    Location
    Kwa Dukuza, South Africa
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    Thanks for your help Bert,

    Bert said "[code[ <----should be the other, but then you won't see the tag
    All your code here
    [/code[ <----Again the second bracket should be } without the shift key."

    Ja dit maak sense, dis cool! (Afrikaans response to Bert's last question - For English, replace "this" for "dit" and "dis")

    I've tried the various DEFINE and REGISTER options but I really don't know what I am doing. Nothing happens. I haven't linked up a UART/USART yet so I can't debug and I am in the dark. Hardware RS232 is my next exercise and I think I'll need to understand REGISTERS for that.

    Kind regards from Kwa Dukuza, lost somewhere in darkest Africa
    John Bond

  3. #3
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    Well now we need the basic stuff - lol. What pic? what version PBP? MPLAB or MCS? PM or MPASM? Internal osc or external? Some of it we don't need for this problem, but its a good idea to let us know what you are working with.

    What doesn't work? do any led's work? Can you make them blink? Is the value just wrong?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    I've just glanced at an old 16f690 program, which was unfeasibly huge so I've hacked it down & hopefully not missed anything during the slimming process...

    Code:
    '-------------------------------------------------------------------------
    DEFINE ADC_BITS  8     ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 1     ' ADC clock source  (Fosc/8)
    DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
    ADCON1 = %0100000 ' FOSC/4
    ' 
    CM1CON0 = 0  'turn the pesky comparators off
    CM1CON1 = 0  'turn the pesky comparators off
    CM2CON0 = 0 'turn the pesky comparators off
    CM2CON1 = 0 'turn the pesky comparators off
    ' 
    ANSEL       = %0000010        'RA1 (pin 18) AN1 analogue 
    ' 
    PotVal Var Byte
    '
     
    '********program starts......
    ' 
    adcin 1, PotVal
    '
     
    '[do cool stuff with contents of PotVal here, then have a cup of tea & a nap]
    You don't need to worry about ADCON0 (since the ADCIN command selects the ADC channel for you) *unless* you start wanting to use 10 bits, then you need to modify ADCON0.7 to suit (LH or RH Justify)
    Last edited by HankMcSpank; - 16th June 2011 at 19:31.

  5. #5
    Join Date
    Jun 2011
    Location
    Kwa Dukuza, South Africa
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    Thanks Bert and Hank

    No time for tea or a nap but I'm sure this will solve my ADC problem. I now have a problem with the compiler. It crashes after a couple of downloads and only works again after reloading both PBP and MPLAB. I will make a new post on these issues

    Hank - Thanks for that warning on 10 bit (LH or RH Justify) because I will be using 10 bit to measure piezo pressure sensor and I will discarding the upper 1 or 2 bits to reduce the span to 8 bits. I am looking for a 0.05 Bar (peak to peak pressure fluctuation) to determine the speed of an air motor running at between 5 bar and 6 bar. 8 bit, without reducing the span, makes it a bit tight!

    Thanks again and kind regards

    John

  6. #6
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    Here is another example of a small test program that will read the AN2 a/d converter and send it to a debug window.

    one thing to remember is that the a/d converter names do not always match the pin #. example... AN2 does match PortA.2 but AN3 is actually on PortA.4.

    If you have not used the debug feature of the PICkit programmer, you program the chip, power it up and then goto the Tools Menu and choose UART Tool. This will open up another Terminal window where you choose 2400 baud in the upper left and then click on Connect.


    Code:
    ' 16F690
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2007 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 12/4/2007                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ' 10-bit A/D conversion 
    ' Connect analog input to channel-2 (RA2)
    ' -----[ I/O Definitions ]-------------------------------------------------
     DEFINE DEBUG_REGG PORTA        'set debug port to porta
     DEFINE DEBUG_BIT 0             'use pin a0 of porta for debug
     DEFINE DEBUG_BAUD 2400         'set baud rate to 2400
     DEFINE DEBUG_MODE 0            'communicate in true mode
     
    ' Define ADCIN parameters
    ' Set number of bits in result
    DEFINE  ADC_BITS        10 
    ' Set clock source (3=rc)
    DEFINE  ADC_CLOCK       3   
    ' Set sampling time in microseconds
    DEFINE  ADC_SAMPLEUS    50     
    adcVar  VAR WORD ' Create variable to store result
    ' Set PORTA to all input
    'TRISA = %11111111     
    ' Set up AD
    ANSEL  = %00000100  'enable AN2, ALL THE REST DIGITAL
    ANSELH = %00000000  'AN8-11 DIGITAL
    ADCON0 = %10001000   ' RIGHT JUSTIFIED
    ADCON1 = %00010000  'fOSC/8     
    Pause 500               ' Wait .5 second
    main:  
      ADCIN 2, adcVar ' Read channel 0
      ' do something with adVar here
      debug DEC adcvar,"  ",BIN adcVar,$0D,$0A
      Pause 100              ' Wait.01 second
    GoTo main                   ' Do it forever
    Good Luck
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  7. #7
    Join Date
    Jun 2011
    Location
    Kwa Dukuza, South Africa
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: ADC not working - I'm a new member

    Thanks Dwight - that will sort me out once I've got my Microchip PICKit 2 dongle to start working again. The fault with the dongle seems like it may be electronic, not software.

    When I started this thread, I didn't have a MAX232 rigged up but I do now so from now on I can use the debug

    Kind regards from Kwa Dukuza

    John Bond
    Last edited by johnbond; - 19th June 2011 at 13:02.

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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