Analog In for 16F88 with IR Sensor.


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2012
    Posts
    3

    Default Analog In for 16F88 with IR Sensor.

    I'm having difficulty getting the following code to work. I have it wired such that the Sensor (QRE1113 Sparkfun) inputs to RA1/AN1, and outputs to a LED on RB4. I have verified the functionality with a digital signal (pushbutton), but cannot get it to work in Analog.

    Even now, my push button works, using the following code, but the sensor does not. Using a Voltmeter, my sensor currently provides a range of about 1.7-4.6 Volts, so hopefully it should switch whenever it crosses 2.5.

    I feel that it may be one or a combination of the following:

    - Not Defining my Analog controls correctly / completely
    - Not having a reference voltage (maybe?)
    - Not understanding how to define 8 bit thresholds properly.

    Any assistance appreciated, and thank you for your time,

    Code:
    '----------Variables------------
        x  var byte              ' Byte for potentiometer input       
    '----------Initialization---------
      
        TRISB = %00000000
        TRISA = %00000010
        
        'DEFINE OSC 8        ' Sets the internal clock speed to 2 MHz               
        'OSCCON.4 = 1        
        'OSCCON.5 = 0                                                 
        'OSCCON.6 = 1
    
        ANSEL = %00000010  ' Leaves AN1 in analog mode, all else digital.                
                                                                          
    '--------Main Code--------                            
        
    myloop:
    
        adcin 1, x                 ' Read analog voltage on AN1 and
                                   ' convert to 8-bit digital value
                                   ' and store as x.     
        if x > %10000000 then
            PORTB.4 = 1
        endif
        
        if x < %00000111 then
            PORTB.4 = 0  
        endif  
     
    goto myloop                  ' Go to loop label   
    
    end

  2. #2
    Join Date
    Oct 2012
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Re: Analog In for 16F88 with IR Sensor.

    (newbe here)

    Should that be High PORTB.4 and LOW PORTB.4?

    I also don't see an ADCON1 being setup

    Code:
    if x > %10000000 then
            PORTB.4 = 1
        endif
        
        if x < %00000111 then
            PORTB.4 = 0  
        endif
    Last edited by norohs; - 2nd November 2012 at 03:26.

  3. #3
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: Analog In for 16F88 with IR Sensor.

    There are two versions of the QRE1113 - one digital, one analog . Are you sure you have the analog version?

  4. #4
    Join Date
    Nov 2012
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: Analog In for 16F88 with IR Sensor.

    Quote Originally Posted by norohs View Post
    (newbe here)

    Should that be High PORTB.4 and LOW PORTB.4?

    I also don't see an ADCON1 being setup

    Code:
    if x > 000000 then
            PORTB.4 = 1
        endif
        
        if x < 000111 then
            PORTB.4 = 0  
        endif
    You're correct, for my purposes, the high / low settings should be reversed. I'm trying to read up more on how the ADConverter works, to get this circuit working. There are lots of examples of using it with a LCD on RA4, but I can't find anything more.. general.

    And yes - I'm sure I have the Analog version. Also, looks like you can't edit old posts, I meant to set my thresholds at 128 on both sides, but forgot to edit it before copying in. The low threshold should be set at 128, not 7.

  5. #5
    Join Date
    Nov 2012
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: Analog In for 16F88 with IR Sensor.

    I'm getting the impression that I need the following things for my 16F88

    Code:
    ADCON1.7 = 1
    or

    Code:
    DEFINE ADC_BITS 10
    Do I need both of these if I only want my A/D to be 8 bits? (IE: ADC_BITS 8)

Similar Threads

  1. 16F88 + 2 Analog inputs
    By ruijc in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th July 2010, 23:17
  2. 16F88 Analog In
    By Mugelpower in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 25th March 2008, 08:30
  3. analog data acquisition with 16f88
    By dru_x in forum Code Examples
    Replies: 3
    Last Post: - 29th January 2008, 13:38
  4. 16f88 - pin RA4 as analog input
    By savnik in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 15th December 2006, 13:55
  5. Anyone Got a 16F88 to Work in Analog?
    By thunderstrike44 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st August 2004, 22:34

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