/// This FB can be used to sample analog data by using two
/// GPIO Pins:
/// - one digital input
/// - one digital output
///
/// There are also other circuits out there, which only
/// need one single GPIO. But they have the clear disadvantage
/// you have to take care about the VRef, which you are using, as it
/// will be pulled to ground by the GPIO acting as an output.
/// Circuit, recommended here, it is to my understanding much safer,
/// The accuracy of the measurement relies in some aspects on the scheduling.
/// frequent as possible. It is generally OK to combine this FB with the SoftPWM.
SysTimeGetUs(tCurrent);
IF xInit THEN
xInit := FALSE;
tStart := tCurrent;
END_IF
IF xSampling THEN
// Reading the sampling pin, and measure the time
IF xInput THEN
/// capacitor is full, stop sampling, and discharge
xSampling := FALSE;
rVal := LWORD_TO_REAL(tCurrent - tStart) * crScale;
tStart := tCurrent;
END_IF
ELSE
// Discharge the capacitor
IF tCurrent - tStart > ctDischarge THEN
// start sampling
xOutput := TRUE;