help setting up 16F886 for ADC


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: help setting up 16F886 for ADC

    Hi to all,

    I have troubles with reading 10 bit on AN0 , too .
    Can somebody give me some ideas, please ?

    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/2/2018                                          *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    'PIC16F886
       #config
       __CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
       __CONFIG    _CONFIG2, _WRT_OFF & _BOR21V
       #endconfig
        
    define osc 4
    include "lcd886.bas"
    CM1CON0 = 0 			'Comparator Off
    CM2CON0 = 0 			'Comparator Off
       	
    Define ADC_BITS 10 		'Set number of bits in result (10)
    Define ADC_CLOCK 3 		'Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 	'Set sampling time in microseconds (50us)
    
    TRISA = %00000001       'Set PORTA.0 as input
    TRISB = %00000000       'Set PORTB as outputs
    TRISC = %00000000       'Set PORTC as outputs
    
    ANSEL  = %00000001      'Set PORTA.0 as analog
    ANSELH = %00000000      'The rest ports are digital
    'ADCON0 = %11000011      'Set adc channel = AN0
    'ADCON1 = %10000000  'SETUP ADC RIGHT JUSTIFY SET REFV to VDD & VSS		        
    	
    PORTA = 0
    PORTB = 0
    PORTC = 0
    
    light  var PORTA.3
    tc     var word
    
    '--------light is ON and initialize the LCD ---------
    pause  300
    high   light
    gosub  lcdrst
    lcdout $fe, $80,"TEST"
    gosub  lcdrst
    
    '---------------------PROGRAM------------------------
    MAIN:
    ADCON0 = %11000011
    gosub read_tc
    
    lcdout $fe, $80, #tc
    pause 200
    goto main
    end
    
    '---------------------Read AN0------------------------
    read_tc:
    WHILE ADCON0.1 = 1:WEND  'WAIT FOR CONVERSION
         tc.HIGHBYTE = ADRESH
         tc.LOWBYTE  = ADRESL
    return
    '--------------------lcd reset------------------------
    lcdrst:
    lcdout, $fe, 1
    pause 10
    return

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,172


    Did you find this post helpful? Yes | No

    Default Re: help setting up 16F886 for ADC

    Is it on a F886?

    You MUST NOT set the GO/DONE bit at the same time you enable the ADC converter (Data Sheet page 106, Note in the box).

    Also you do not setup the ADCON1. It is commented.

    Try this:

    Code:
            '****************************************************************
            '*  Name    : UNTITLED.BAS                                      *
            '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
            '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
            '*          : All Rights Reserved                               *
            '*  Date    : 10/2/2018                                          *
            '*  Version : 1.0                                               *
            '*  Notes   :                                                   *
            '*          :                                                   *
            '****************************************************************
            'PIC16F886
               #config
               __CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
               __CONFIG    _CONFIG2, _WRT_OFF & _BOR21V
               #endconfig
                
            define osc 4
            include "lcd886.bas"
            CM1CON0 = 0 			'Comparator Off
            CM2CON0 = 0 			'Comparator Off
               	
            Define ADC_BITS 10 		'Set number of bits in result (10)
            Define ADC_CLOCK 3 		'Set clock source (3=rc)
            Define ADC_SAMPLEUS 50 	'Set sampling time in microseconds (50us)
    
            TRISA = %00000001       'Set PORTA.0 as input
            TRISB = %00000000       'Set PORTB as outputs
            TRISC = %00000000       'Set PORTC as outputs
    
            ANSEL  = %00000001      'Set PORTA.0 as analog
            ANSELH = %00000000      'The rest ports are digital
            ADCON0 = %11000001      'Set adc channel = AN0
            ADCON1 = %10000000  'SETUP ADC RIGHT JUSTIFY SET REFV to VDD & VSS		        
            	
            PORTA = 0
            PORTB = 0
            PORTC = 0
    
            light  var PORTA.3
            tc     var word
    
            '--------light is ON and initialize the LCD ---------
            pause  300
            high   light
            gosub  lcdrst
            lcdout $fe, $80,"TEST"
            gosub  lcdrst
    
            '---------------------PROGRAM------------------------
            MAIN:
            ADCON0.1=1
            gosub read_tc
    
            lcdout $fe, $80, #tc,"       " ' I put some space here when you go from large result to 1 digit result.
            pause 200
            goto main
            end
    
            '---------------------Read AN0------------------------
            read_tc:
            WHILE ADCON0.1 = 1:WEND  'WAIT FOR CONVERSION
                 tc.HIGHBYTE = ADRESH
                 tc.LOWBYTE  = ADRESL
            return
            '--------------------lcd reset------------------------
            lcdrst:
            lcdout, $fe, 1
            pause 10
            return
    Ioannis

Similar Threads

  1. 16f886
    By Larry in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 4th November 2008, 17:57
  2. 16f886
    By hvacrtech in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 27th May 2008, 06:06
  3. Setting Address via ADC
    By breesy in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th October 2007, 15:33
  4. Setting up ADC....
    By robertmark68 in forum General
    Replies: 1
    Last Post: - 11th August 2006, 19:08
  5. Setting Up Adc Ports
    By GEEZER in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 22nd August 2005, 04:04

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