Menu

A/D conversion does not work

DeonS
2014-06-08
2015-07-11
  • DeonS

    DeonS - 2014-06-08

    Hi
    I am trying to use the A/D conversion ports of the 12F683, 12F674 or 16F684 but obviously I do not have a clue how it works. The code uses AN0 for input of the voltage which I vary from 0.1 volt to ca 1.6 volt. However, I can vary the volts as much as I like; basically the pic (anyone of the three above) turns on the portA.4 and it stays on regardless the input voltage?

    The pic is connected to a regulated power supply of 4.98V. Can anyone help? (If you are wondering I am extremely new to this!)

    Regards
    Deon

    ;Chip Settings

    chip 16F684,8

    ;Variables
    Dim volts As byte

    'Set the input pin direction
    Dir PORTA.0 In
    Dir PORTA.1 Out
    Dir PORTA.2 Out
    Dir PORTA.3 In
    Dir PORTA.4 Out
    Dir PORTA.5 Out

    'Program starts here
    Main:
    volts = ReadAD(AN0)
    wait 1 s
    if volts > 230 then
    PORTA.1 = 1
    PORTA.2 = 0
    PORTA.4 = 0
    PORTA.5 = 0
    if volts > 180 and volts < 230 then
    PORTA.1 = 0
    PORTA.2 = 1
    PORTA.4 = 0
    PORTA.5 = 0
    if volts > 153 and volts < 180 then
    PORTA.1 = 0
    PORTA.2 = 0
    PORTA.4 = 1
    PORTA.5 = 0
    if volts > 112 and volts < 153 then
    PORTA.1 = 0
    PORTA.2 = 0
    PORTA.4 = 0
    PORTA.5 = 0
    if volts > 0 and volts < 112 then
    PORTA.1 = 0
    PORTA.2 = 0
    PORTA.4 = 1
    PORTA.5 = 1
    else
    PORTA.1 = 1
    PORTA.2 = 1
    PORTA.4 = 1
    PORTA.5 = 1
    end if
    end if
    end if
    end if
    end if
    goto main

     
  • Anobium

    Anobium - 2014-06-08

    Hi and welcome.

    Nested if's are not supported. Simply change your code are shown below.

    ~~~~
    ;Chip Settings

    chip 16F684,8

    ;Variables
    Dim volts As byte

    'Set the input pin direction
    Dir PORTA.0 In
    Dir PORTA.1 Out
    Dir PORTA.2 Out
    Dir PORTA.3 In
    Dir PORTA.4 Out
    Dir PORTA.5 Out

    'Program starts here
    Main:
    volts = ReadAD(AN0)

     if volts > 230 then
       PORTA.1 = 1
       PORTA.2 = 0
       PORTA.4 = 0
       PORTA.5 = 0
     end if
    
     if volts > 180 and volts < 230 then
       PORTA.1 = 0
       PORTA.2 = 1
       PORTA.4 = 0
       PORTA.5 = 0
     end if
     if volts > 153 and volts < 180 then
       PORTA.1 = 0
       PORTA.2 = 0
       PORTA.4 = 1
       PORTA.5 = 0
     end if
     if volts > 112 and volts < 153 then
       PORTA.1 = 0
       PORTA.2 = 0
       PORTA.4 = 0
       PORTA.5 = 0
     end if
     if volts > 0 and volts < 112 then
       PORTA.1 = 0
       PORTA.2 = 0
       PORTA.4 = 1
       PORTA.5 = 1
     end if
    
     if volts = 0  then
       PORTA.1 = 1
       PORTA.2 = 1
       PORTA.4 = 1
       PORTA.5 = 1
     end if
    

    goto main

     
  • DeonS

    DeonS - 2014-06-08

    Hi Anobium
    Thanks so much. It works now on the 16F684. Tried the 12F683 and it does not work, but I will work on this still bit more before I ask again.
    Anyway removing the nested if structure worked for the 16F684.

    Thanks again!

     
  • Anobium

    Anobium - 2014-06-08

    Do not struggle. Post your code if you want to.

     
  • DeonS

    DeonS - 2014-06-09

    Hi Anobium

    All three chips now working! Thanks again and thanks for the forum whoever needs to be thanked for it.

     
  • Hugh Considine

    Hugh Considine - 2015-07-11

    A clarification for anyone who finds this thread, nested ifs are usually supported. The issue with the first post is that if the condition on the outer If is true, then the condition on the inner If will always be false. So feel free to use nested Ifs, but just be careful with the conditions!

     

Log in to post a comment.