Menu

AD pin issues

Help
George
2010-11-27
2013-05-30
  • George

    George - 2010-11-27

    I thought I had a basic understanding of AD but not too sure now.  My 16f887 is configured with RA0 on a pot and an LED on PortD,0  So I wrote an exercise with GCGB using the ReadAD example under Help except I used 2 IF statements in a loop to turn the LED on or off depending on the pot.  It works fine and the LED turns on and off as I manipulate the pot.

    So for the heck of it I changed the I/O line to "DIR PORTA,1 In" and "ReadAD(AN1)"  (instead of the "0" pin) and execution does not changes and there are no compile errors.  I then changed to PORTA, 5 and ReadAD(AN5) and again no change.  Nothing is hooked to the 2nd or other pins of PORTA.  Am I missing something?  Thanks for such basic help.

     
  • Nobody/Anonymous

    Please copy and paste your code here.

    jooe

     
  • Nobody/Anonymous

    Check the document for 887
    they tricked you!
      RA5  is actually   AN4
    and     RE0 is AN5
    and    RE1 is AN6

     
  • Nobody/Anonymous

    Thanks, here's the code:

    Again, this works as designed reading AN0, the pin the pot is tied to.  The mystery to me is why the pot directs the LEDs to be on or off when I am reading a different pin??

    Thanks George

    ;Chip Settings
    #chip 16F887,8

    'Chip Settings
    'A program to flash two LEDs on PORTD, bits 0 and 1

    'Set the pin directions
    Dir PORTA.1 In
    Dir PORTD.0 Out
    Dir PORTD.1 Out

    var = ReadAD(AN1)

    'Main routine
    Start:

    'Turn one LED on, the other off
    If ReadAD(AN1) > 120 then
    Set PORTD.0 On
    Set PortD.1 Off
    end if
    If ReadAD(AN1) < 120 and ReadAD(AN1) > 80 then
    Set PORTD.0 Off
    Set PORTD.1 On
    End if
    Wait 10 ms
    GOTO Start

     
  • George

    George - 2010-12-10

    Ooops - forgot to logon:

    Thanks, here's the code: Again, this works as designed reading AN0, the pin the pot is tied to. The mystery to me is why the pot directs the LEDs to be on or off when I am reading a different pin?? Thanks George ;

    ;Chip Settings
    #chip 16F887,8 'Chip Settings

    'A program to flash two LEDs on PORTD, bits 0 and 1

    'Set the pin directions
    Dir PORTA.1 In
    Dir PORTD.0 Out
    Dir PORTD.1 Out

    var = ReadAD(AN1)

    'Main routine
    Start:

    'Turn one LED on, the other off
    If ReadAD(AN1) > 120 then
    Set PORTD.0 On
    Set PORTD.1 Off
    end if

    If ReadAD(AN1) < 120 and ReadAD(AN1) > 80 then
    Set PORTD.0 Off
    Set PORTD.1 On
    End if

    Wait 10 ms

    GOTO Start

     
  • Nobody/Anonymous

    If AN1 is just floating and not connected to anything then a charge could build up on the pins stray capacitance causing the A-D converter to measure a voltage and turn on the leds. Maybe there is just enough leakage between the pot pin and AN1 for the voltage on AN1 to follow the pot voltage. You could try connecting AN1 to ground if it is floating and see whether it stops.

     
  • Nobody/Anonymous

    I have not compiled the example.  I usually assign a variable to an analog reading, then perform conditional statements/maths on the variable.  A learned habit, or conditioned by limits of the compiler in "certain situations"?  For instance, this code worked fine for an '887.

    Iout = ReadAD10(AN1)    ;Vcurrent Maxim4081 or Allegro Hall Effect
        Iout = (Iout-512)*488/20
    

    Kent

     
  • George

    George - 2010-12-19

    Replies 6 and 7.  Excellent.  Yes putting AN1 to ground when reading AN0 fixed the problem.  Amazing experience.  And yes, for such learning I permitted myself to be a sloppy programmer…calling functions repeatedly instead of assigning variables.  I'm new at COW and I'm cautious about variables because I haven't seen type declarations, etc. anywhere.  Maybe there are none.n Thanks for all the help.  That mystery held me up a lot for such a simple test.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.