Analog Digital Conversion ??


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Location
    Türkiye
    Posts
    103

    Default Analog Digital Conversion ??

    Hi , I'm new in this forum , and to the pic programming too ...

    I was trying to understand a code already written by an anonymous programmer to manage an ADC of a PIC ..
    But there is that part of which function that I can't understand , particularly consisting a mathematical code ...

    BTW It's basically a voltmeter measuring the voltage on a connected pot or sth

    Here is the program ;
    --------------------------------------------------------------------------------------------------------------

    TRISA=000001
    TRISB=0
    TRISC=0
    TRISD=0

    @ DEVICE pic16F877
    @ DEVICE pic16F877, WDT_on
    @ DEVICE pic16F877, PWRT_ON
    @ DEVICE pic16F877, PROTECT_OFF
    @ DEVICE pic16F877, XT_OSC
    '-------------------------------------------------------------------------------
    DEFINE LCD_DREG PORTB
    DEFINE LCD_DBIT 4
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 3
    DEFINE LCD RWREG PORTB
    DEFINE LCD_RWBIT 2
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 1
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    '-------------------------------------------------------------------------------
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 100
    '-------------------------------------------------------------------------------
    ADCON1=001110
    '-------------------------------------------------------------------------------
    HAM var word
    VOLT var word
    MVOLT var byte
    '-------------------------------------------------------------------------------
    LOW PORTB.2
    LCDOUT $FE,1
    PAUSE 200
    '-------------------------------------------------------------------------------


    START:
    ADCIN 0,HAM

    CTRL:

    IF ADCON0.2=1 THEN CTRL

    LCDOUT $FE,1," HAM =",# HAM
    HAM=HAM+1


    VOLT=(HAM*/ 1250)/100
    MVOLT=VOLT // 10

    VOLT=VOLT/10

    LCDOUT $FE,$C0," VOLT=",#VOLT,",",# MVOLT
    PAUSE 500
    GOTO START




    ------------------------------------------------------------------------------------
    The part that I don't understand is the code scripted as below ;

    HAM=HAM+1


    Volt=(ham */ 1250)/100
    Mvolt=Volt // 10

    Volt=Volt/10

    --------------------
    NOW HERE ARE MY QUESTIONS ;

    1. Where does this decimal "1" come from and why do we add it to the "ham" variable
    2. Where does this 1250 , 100 come from ?
    3. What are these operators written as "//" and "*/"

    I'd be so appreciated if you could help me with these ....
    Thanks in advance ...

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    644


    Did you find this post helpful? Yes | No

    Default Re: Analog Digital Conversion ??

    Hi,

    Welcome to the forum. The operators "*/" and "//" can be found in the 'Math Operators' section of the PBP manual.

    http://melabs.com/resources/pbpmanual/

    Now, about your other questions, many of us don't have the good habit of writing a good comment after every line of code. So, I am not sure what the programmer wanted to do there. Maybe somebody else here has a better answer.

    Robert
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Analog Digital Conversion ??

    If we knew what this code was written for we might be able to answer your other questions, but random code ...
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Analog Digital Conversion ??

    1 is clearly an offset. The reason for using it is unknown, it could depend on the necessity to alline the reading with other device

    1250 is a coefficient for converting the ADC reading into volts. The actual coefficient should be 12.5 but due to the integer math limitation the author did multiply it by 100 to obtain 1250 and then he devide the product by 100.

    All these numbers are related to a specific application, so you should ignore them, since very likely, your application will require different numbers as coefficient and eventually an offset.

    Cheers

    Al.
    All progress began with an idea

Similar Threads

  1. analog and digital
    By lerameur in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th June 2008, 02:40
  2. digital to analog conversor
    By sirvo in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd January 2008, 20:46
  3. PortA analog and digital IO
    By krohtech in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th November 2007, 02:25
  4. Digital to Analog Converter
    By kutsi in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 27th June 2007, 19:17
  5. Replies: 2
    Last Post: - 14th April 2006, 09:42

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