[r16]: / trunk / gpiomod / GPIOMods / SoftAIN / svnobj  Maximize  Restore  History

Download this file

32 lines (30 with data), 7.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// 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;