Shop OBEX P1 Docs P2 Docs Learn Events
Analogin — Parallax Forums

Analogin

MacGeek117MacGeek117 Posts: 747
edited 2007-12-26 01:03 in General Discussion
I can't seem to find any info on the ANALOGIN command. Could someone provide a circuit and the syntax?
RoboGeek

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I reject your reality and subsitute my own!"

Adam Savage, Mythbusters
www.parallax.com
www.goldmine-elec.com
www.expresspcb.com
www.startrek.com
·

Comments

  • ZootZoot Posts: 2,227
    edited 2007-12-25 22:34
    From the SX/B help file:

    "

    ANALOGIN
    ANALOGIN InPin, OutPin, Result {, Prime}
    Function
    The SX/B ANALOGIN command converts an analog voltage to a digital value by using a method known as
    continuous calibration. This method requires one input pin and one output pin on the SX chip. The only required
    hardware is two resistors and one capacitor.
    • InPin is the input pin.
    • OutPin is the output pin.
    • Result is a byte variable that will receive the value.
    • Prime is the number of priming cycles to execute before taking the measurement. Prime is optional; if
    not specified the default is 1 cycle.
    Explanation
    The method works by taking advantage of the input pin's threshold voltage. This is the voltage level that makes
    the input pin read as either a "0" or a "1". Normally on the SX the input threshold is set to the "TTL" level, which
    is 1.4 volts. So voltages above 1.4 volts are read as a "1" and voltages below 1.4 volts are read as a "0". To allow
    the measured voltage to range from 0 volts to Vdd we need to set the pin to the "CMOS" threshold level, which is
    1/2 Vdd (or 2.5 volts when operating the SX from a 5 volt supply).

    ' Example program using ANALOGIN command 
     
     
    DEVICE   SX28, OSC4MHZ, TURBO, STACKX, OPTIONX 
    FREQ   4_000_000 
     
    InPin   PIN  RA.0 INPUT CMOS 
    OutPin   PIN  RA.1 OUTPUT 
     
     
                   10K            10K 
    'RA.1 Pin ----\/\/\/-----+---\/\/\/----- Voltage to measure 
    '                        | 
    'RA.0 Pin ---------------+ 
    '                        | 0.01uF 
    '                        +--|(-- GND 
     
    a   VAR  Byte 
     
    PROGRAM Start NOSTARTUP 
     
    Start: 
      ANALOGIN InPin, OutPin, a, 2 
      WATCH a 
      BREAK 
      GOTO Start 
    END 
    
    



    For this example we will assume the SX is operating from 5 volts, and that you have set the input pin to "CMOS"
    threshold levels (the easiest way to do this is to use the PIN definition "InPin PIN INPUT CMOS"). Here is how the
    components are connected:

     
                        10K           10K 
    SX Output Pin ----\/\/\/-----+---\/\/\/----- Voltage to measure 
                                 | 
     SX Input Pin ---------------+ 
                                 | 0.01uF @ 4MHz or 0.001uF @ 50MHz 
                                 +--|(-- GND 
    
    
    



    What the ANALOGIN command does is read the input pin, and make the output pin the opposite of what the
    input pin reads. If the input pin reads "0", it makes the output pin a "1". If the input pin reads "1", it makes the
    output pin a "0". It does this 255 times, and keeps a count of how many times the input pin was a "1". This count
    is what is returned as the result of the command. This value is proportional to the voltage level.
    Basically the ANALOGIN command attempts to keep the input pin right at the threshold voltage. If the voltage
    input was not connected, and the capacitor wasn't there, the output would just toggle from high to low. And the
    count would end up being 128. Now if you added the cap, the output pin would still toggle, but not every time
    (since it takes the cap some time to charge and discharge), but over the long run it would still return a count of
    128.
    Now imagine if you have the complete circuit connected and the voltage input is 0 volts. The input pin will read
    as a "0" so it will make the output pin high (5 volts). So now we have 0 volts through a 10K resistor and 5 volts
    through a 10K resistor. That will make the junction (where the cap and input pin are connected) equal 2.5 volts.
    So the input pin will never get above 2.5 volts regardless of how long the output pin stays high, so it will always
    read as a "0" and our count will be zero.
    Now imagine if the input voltage is 5 volts. The input will read as a "1", so it will make the output low (0 volts).
    Now we have the same situation reversed. The voltage at the input pin can never get below 2.5 volts regardless
    of how long the output pin stays low, so it will always read as a "1" and our count will be 255.
    When the input voltage is between 0 volts and 5 volts, then things get interesting. If the input voltage is 1.25
    volts (1/4 the maximum), then the input pin will see a pattern of 0's and 1's such that the number of 0's is 3
    times the number of 1's. Over 255 samples it will return a count of 63 (since only 1 in 3 reads of the input pin
    were 1’s). Depending on the clock speed of the SX and the value of the capacitor, the pattern may be something
    like "000100010001" or it may be something like "000000110000001100000011". But over the 255 samples you
    will still get a count of about 63 1's in the pattern.
    It may take some experimentation to get the optimum values for the capacitor. In general the faster the SX clock,
    the lower the capacitor value, and the slower the SX clock, the higher the capacitor value.
    Another factor that affects the stability of ANALOGIN is that the method assumes the input pin is already at the
    threshold voltage before it starts counting the 1's read at the input pin. To accomplish this the ANALOGIN
    command actually primes the capacitor by running 255 samples BEFORE starting to count the pulses. Then it
    runs another 255 samples while counting the 1's. There is an optional parameter that can be used with the
    ANALOGIN command if you want or need more priming cycles (255 samples per cycle). More priming cycles
    allow the use of a larger capacitor and that gives more stable readings, but takes more time to complete the
    ANALOGIN command. So if you can afford the extra time, and want a more stable reading, then increase the
    value of the capacitor and increase the number of priming cycles.

    Okay so what if you want to read voltage ranges other than 0 volts to 5 volts. Well if you want get full scale
    values from a voltage lower than 5 volts, one easy way is to just set the input pin to it’s default setting of “TTL”
    threshold levels. Since the “TTL” threshold level is 1.4 volts, and the ANALOGIN values range from 0 volts to 2x
    the threshold level, this will result in 0 for 0 volts and 255 for 2.8 volts. Wider voltage ranges can be read by
    using asymmetrical resistor values. If you make the resistor connected to the measured voltage a larger value
    than the resistor connected to the SX output pin, you can read voltages greater than 5 volts.
    Note that the values returned by ANALOGIN will be dependent on the impedance of the voltage being
    measured. The resistors used should be several times larger than the impedance of the input voltage. For
    example if you were using a 10K pot to create voltage from 0 to 5 volts the resistance of the pot would effectively
    be added to the 10K resistor. This would make the resistor values unequal. Let’s suppose the pot was centered.
    The output of the pot would equate to a 2.5K resistor connected directly to a 2.5 volt supply. This 2.5K resistance
    would be in series with the 10 k resistor connected from the pot to the cap. Since the pot would have effectively
    zero resistance when turned to each end point, you would still get the full range of values, but the values would
    not be linear through the range of the pot.

    "

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-26 01:03
    Thanks, Zoot!
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
Sign In or Register to comment.