A2D on PIC 16F685


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    21

    Angry A2D on PIC 16F685

    Please i've tried everything!
    I want this stupid program to work on my PIC 16F685. I programmed it using PIC BASIC and the U2 EPIC programmer. I configured the oscillator to RCIO with the EPIC programming software.

    I've been trying to get this to work for hours!!!

    Please include an explanation for changes in my code. None of the sample programs coming with the PIC BASIC compiler are working!

    What should I configure my oscillator to in the EPIC programmer?

    All i want is an A2D conversion to be displayed to my LCD connected to RB.7.

    Here is the code I used

    ' 10-bit A/D conversion
    ' Connect analog input to channel-0 (RA0)

    ' Define ADCIN parameters
    ' Set number of bits in result
    DEFINE ADC_BITS 10
    ' Set clock source (3=rc)
    DEFINE ADC_CLOCK 4
    ' Set sampling time in microseconds
    DEFINE ADC_SAMPLEUS 10

    adcVar VAR WORD ' Create variable to store result

    ' Set PORTA to all input
    TRISA = %11111111

    ' Set up ADCON1
    ADCON1 = %10000010

    Pause 500 ' Wait .5 second
    HIGH PORTB.6 ' Turn on status LED
    main:
    ADCIN 0, adcVar ' Read channel 0
    serout portb.7, 6, ["S.1 =", #ADCVAR]

    Pause 10 ' Wait.01 second
    GoTo main ' Do it forever

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


    Did you find this post helpful? Yes | No

    Default

    What speed oscillator are you using?

    Are you using an external crystal, resonator, the internal osc?

    Do you have a pull-up resistor to /MCLR?

    Have you managed to get an LED blinking yet?

    Does it work sending anything to your serial LCD?

    Little stuff like that makes it easier for people to help.
    Regards,

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

  3. #3
    Join Date
    Oct 2007
    Posts
    21


    Did you find this post helpful? Yes | No

    Default Internal

    internal osc, mclear disabled. LCD works and blinking LED works. JUST A2D is messed up for soem reason.

    What speed of the ADC_Clock do i set it to for my microcontroller. I have it set to 3 now, what should i change it to.

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


    Did you find this post helpful? Yes | No

    Default

    Microchip likes to switch things around a bit from time to time. Some PICs have the A/D channel select & conversion clock select bits in ADCON0. Some newer versions have moved a few bits into ADCON1. Of course this totally screws up 3rd party compilers with nifty
    commands like ADCIN for using the A/D modules...;o}

    ADCIN expects the first version. Where channel select & conversion clock select bits are in ADCON0.

    Until MeLabs changes the PBP library commands to catch up, you'll need to do it the old yuck fashioned way.

    This should work. If not let me know. Unfortunately, I don't have a 16F685 to test it out on.
    Code:
    @ DEVICE PIC16F685,MCLR_OFF,WDT_OFF,INTRC_OSC_NOCLKOUT,PROTECT_OFF
    
    adcVar VAR WORD ' Create variable to store result
    
    ' Set PORTA to all input
    TRISA = %11111111 
    
    ' Set up ADCON1
    ADCON0 = %10000001 ' right justify for 10-bit, select AN0 channel, A/D module enabled
    ADCON1 = %00110000 ' A/D conversion clock = Frc
    
    Pause 500        ' Wait .5 second
    HIGH PORTB.6     ' Turn on status LED
    
    main: 
      ADCON0.1=1       ' Set GO/DONE bit to start A/D conversion
      WHILE ADCON0.1=1 ' Wait for it to complete
      WEND
      adcVar.HighByte = ADRESH ' Read high-byte of result
      adcVar.LowByte = ADRESL  ' Read low-byte of result
    
      serout portb.7, 6, ["S.1 =", #adcVar]
    
      Pause 10 ' Wait.01 second
      GoTo main ' Do it forever
    
      END
    Regards,

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

  5. #5
    Join Date
    Oct 2007
    Posts
    21


    Did you find this post helpful? Yes | No

    Default Thanks!

    It worked!
    But how would I change it to read AN0 and AN1

    How do I change the program to read multiple channels?

  6. #6
    Join Date
    Oct 2007
    Posts
    21


    Did you find this post helpful? Yes | No

    Default Other PICs

    which PICs work with the ADCIN command? WHich one that you used worked with that command?

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Forkosh View Post
    It worked!
    But how would I change it to read AN0 and AN1

    How do I change the program to read multiple channels?
    Quote Originally Posted by Forkosh View Post
    which PICs work with the ADCIN command? WHich one that you used worked with that command?
    Code:
      ADCON0.1=1       ' Set GO/DONE bit to start A/D conversion
      WHILE ADCON0.1=1 ' Wait for it to complete
      WEND
      adcVar.HighByte = ADRESH ' Read high-byte of result
      adcVar.LowByte = ADRESL  ' Read low-byte of result
    Instead of the part above, you may try
    ADCIN 0, AdcVar

    and to continue with multiple channels, you may try
    ADCIN 1, AdcVar1
    ADCIN 2, AdcVar2

    etc.

    ADCIN command will take care of setting ADC registers etc.


    Edit:

    Here is another version of reading as many ADC channels as available on your PIC.

    Code:
    NumOfCh CON 7                   ' Number of Analog Channels we will read. 
                                    ' Change this number as desired.  
    
    AdcValue   VAR BYTE[NumOfCh+1]  ' Array to store ADC Values in a row.
    Channel    VAR BYTE             ' Loop variable to go through channels.
    
    Start:
    
        FOR Channel = 0 TO NumOfCh      ' Will read 7 analog cannnels and store  
            ADCIN Channel, AdcValue[Channel]    '..ADC values in AdcValue Array.
        Next Channel
        
    ' do something here.
    
    GOTO Start
    Last edited by sayzer; - 16th October 2007 at 15:08. Reason: Added Array.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    What version of PBP are you using?

    I don't have a 685 to test this with, but I ran it through MPSIM using ADCIN, and it appears to work
    fine with v2.47. The only difference is instead of setting ADCON1.7 for right justification, you need to
    set ADCON0.7. Give this a shot and let me know if it works.
    Code:
    @ DEVICE PIC16F685,MCLR_OFF,WDT_OFF,INTRC_OSC_NOCLKOUT,PROTECT_OFF
    
    DEFINE ADC_BITS 10 
    ' Set clock source (3=rc)
    DEFINE ADC_CLOCK 3 
    ' Set sampling time in microseconds
    DEFINE ADC_SAMPLEUS 10 
    
    adcVar VAR WORD ' Create variable to store result
    Index  VAR BYTE ' Loop index for channels
    
    ' Set PORTA to all input
    TRISA = %00000111 ' AN0 to AN2 inputs
    ANSEL = %00000111 ' AN0 to AN2 analog inputs, rest digital
    ANSELH = 0        ' AN8 to AN11 digital
    
    ' Set up ADCON0 (not ADCON1) like in the manual
    ADCON0 = %10000000 ' right justify for 10-bit
    
    Pause 500        ' Wait .5 second
    HIGH PORTB.6     ' Turn on status LED
    
    main: 
      FOR Index = 0 TO 2 ' Display 3 channel results
          ADCIN Index,adcVar ' Read AN0 to AN2
          GOSUB Display      ' Display channel results
      NEXT Index
      Pause 10  ' Wait.01 second
      GoTo main ' Do it forever
      
    Display:  
      serout portb.7, 6, ["Channel #",#Index,#adcVar]
      RETURN
    
      END
    You can enable more A/D pins if you need to by setting the ANSEL or ANSELH bits for each port pin
    with A/D like shown in the data sheet.
    Last edited by Bruce; - 16th October 2007 at 16:04.
    Regards,

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

Similar Threads

  1. SMS via pic
    By kenandere in forum GSM
    Replies: 15
    Last Post: - 10th March 2010, 10:00
  2. Replies: 67
    Last Post: - 8th December 2009, 02:27
  3. HSERIN & Interupts (aka controlling PIC programs from a remote PC)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th June 2009, 14:46
  4. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 21:01
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 1

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