RCCircuitTurning the value of a potentiometer (variable resistor) into something the Propeller can use is done via an RC Circuit. An RC circuit is a"Resistor / Capacitor" circuit that charge a capacitor and then allow the capacitor to drain through the variable resistor and the time it takes to drain is recorded as the "value" - so if the resistor is turned all the way up - higest resistance - then it takes the capacitor a long time to drain, thus returning a higer value (because that value is the time it take to drain)

With the propeller, we can use the same pin to both charge and then read the decay value. We do this by setting the pin to output mode and taking it high, that will charge the capacitor to 3.3V. After a short time (enough for the capacitor to be charged) we will switch the pin to input mode, this will cause the capacitor to drain through the potetiomenter, and we clock the time it takes until the pin is no longer high (somewhere around 1.65V)

The 100 ohm resistor is in place in case the potentiometer is turned all the way down to 0, we don't want a short circuit ever, even though the Propeller might handle it fine, there are other things that won't, so it's a good habit.

 

CODE

CON
    _clkmode = xtal1 + pll16x ' System clock → 80 MHz
    _xinfreq = 5_000_000

PUB Main
    ctra[30..26] := %01000 ' Set mode to "POS detector"
    ctra[5..0] := 17 ' Set APIN to 17 (P17)
    frqa := 1 ' Increment phsa by 1 for each clock tick

    repeat
        ' Charge RC circuit.
        dira[17] := outa[17] := 1 ' Set pin to output-high    
        waitcnt(clkfreq/100_000 + cnt) ' Wait for circuit to charge

        ' Start RC decay measurement. It's automatic after this...
        phsa~ ' Clear the phsa register
        dira[17]~ ' Pin to input stops charging circuit

    repeat 22
        pst.Char(".")
        waitcnt(clkfreq/60 + cnt)
        
        ' Measurement is ready - Adjust ticks between phsa~ & dira[17]~.

    time := (phsa - 624) #> 0