;{CR10X}
;
;PROGRAM C7_datalogger_with_autogain
;Cyclops 7 (C7) Datalogger with Auto Gain Program example
;July 15, 2004
;Jim Crawford, Turner Designs
;
;Disclaimer: As far as I can tell, this program works properly, but it has not been
; extensively field tested. This was my first Campbell program, so my apologies
; if it is done awkwardly. If there are better ways, or you have enhancements, please
; let us know!
;
;Many thanks to Rhys McDonald of Brown and Caldwell for his technical support and the
;use of his datalogger!
;
;Program Notes
; Use 2:1 voltage divider on Differential Input channel 1 to get full output range from C7
;
; Auto Gain Control Strategy:
; CR10X programmed to read 0 to 2500 mv from Cyclops 7
; If on Gain X1
; If the millivolts drops below 150 mv, change to X10 gain
; If on Gain X10
; If the millivolts drops below 150 mv, change to X100 gain
; If the millivolts rises above 2250 mv, change to X1 gain
; If on Gain X100
; If the millivolts rises above 2250 mv, change to X10 gain
; Program delays calculation of concentration values after a range change (default 3 seconds)
;
;
;CR10X Panel Wiring
; Panel Cyclops 7 wire color
; ---------------------------
; +12 - Red (plus input)
; G - black (Input Ground)
; 1H - Red (plus output) (see note below)
; 1L - white (minus output)
; C1 - Blue (X10 gain)
; C2 - Brown (X100 gain)
; NOTE: Recommend 2:1 voltage divider terminal input module (CSI part# VDIV2:1)
;
;C7 Gain CR10X control Ports
;Gain C1 state C2 state
;-------------------------
; X1 high high
; X10 low high
; X100 high low
;
;Subroutines
; 1 - Initialization subroutine
; 2 - Increase gain - Set CR10X control ports
; 3 - Decrease gain - Set CR10X control ports
;
;Input Location Descriptions
; Output - C7 output adjusted by gain setting - range from 0 to 2500
; C7_output - C7 analog output, raw millivolts (mv) (after voltage divider)
; Gain - current gain range setting (1, 10, or 100)
; gain_min - C7 millivolt value at which the gain will be increased
; gain_max - C7 millivolt value at which the gain will be decreased
; gaindelay - how many seconds to wait after range change
; delaycntr - the delay timer after a range change
;
;
*Table 1 Program
01: 1.0000 Execution Interval (seconds)

1: If Flag/Port (P91) ; Call initialization program first time program is run
1: 28 Do if Flag 8 is Low
2: 01 Call Subroutine 1

; If we have to change the gain, we need to give the C7 time to settle
; When the gain changes, we set a delay counter to count down
; If the delay cntr is not equal to zero, then we need to wait until we can take the next valid measurement

2: If (X<=>F) (P89) ; Check if delay counter is zero. If so, we can take a measurement
1: 7 X Loc [ delaycntr ]
2: 4 <
3: .1 F
4: 30 Then Do


3: Volt (Diff) (P2) ; Read C7 output...NOTE! maximum CR10X input voltage = 2.5 volts (need resistor divider for C7 output)
1: 1 Reps
2: 25 2500 mV 60 Hz Rejection Range
3: 01 DIFF Channel
4: 2 Loc [ C7_output ]
5: 1.0 Mult
6: 0.0 Offset


4: Z=X/Y (P38) ; We scale the output depending on the gain setting. Maximum output = 2500
1: 2 X Loc [ C7_output ]
2: 3 Y Loc [ Gain ]
3: 1 Z Loc [ Output ]

5: Do (P86)
1: 10 Set Output Flag High (Flag 0)

6: Real Time (P77) ^8270; Store the time
1: 1121 (Same as 1221) Y,D,Hr/Mn,Sec

7: Sample (P70) ^25814; Store the output
1: 1 Reps
2: 1 Loc [ Output ]


8: If (X<=>Y) (P88) ; Check if the C7 output is small enough that we should increase the gain
1: 2 X Loc [ C7_output ]
2: 4 <
3: 4 Y Loc [ gain_min ]
4: 30 Then Do


9: If (X<=>F) (P89) ; If the output is small, only change gain if we are not already at the maximum gain
1: 3 X Loc [ Gain ]
2: 4 <
3: 99 F
4: 2 Call Subroutine 2


10: End (P95)

11: If (X<=>Y) (P88) ; Check if the C7 output is large enough that we should decrease the gain
1: 2 X Loc [ C7_output ]
2: 3 >=
3: 5 Y Loc [ gain_max ]
4: 30 Then Do


12: If (X<=>F) (P89) ; If the output is large, only change gain if we are not already at the minimum gain
1: 3 X Loc [ Gain ]
2: 3 >=
3: 1.1 F
4: 3 Call Subroutine 3


13: End (P95)

14: Else (P94) ; This is where we come if the gain changed and we need to wait for the C7 to settle

15: Z=X+F (P34) ; We decrement the delay counter
1: 7 X Loc [ delaycntr ]
2: -1 F
3: 7 Z Loc [ delaycntr ]


16: End (P95)

*Table 2 Program
02: 0.0000 Execution Interval (seconds)


*Table 3 Subroutines

1: Beginning of Subroutine (P85) ; This is the initialization subroutine to set all variables
1: 01 Subroutine 1

2: Set Port(s) (P20)
1: 9999 C8..C5 = nc/nc/nc/nc
2: 9977 C4..C1 = nc/nc/output/output ; Use I/O C1 and C2 as outputs to control the gain of the C7

3: Z=F x 10^n (P30) ; Start at gain 10 (the middle range)
1: 10 F
2: 00 n, Exponent of 10
3: 3 Z Loc [ Gain ]

4: Z=F x 10^n (P30) ; Increase gain if C7 output is less than 150 mv
1: 150 F
2: 00 n, Exponent of 10
3: 4 Z Loc [ gain_min ]

5: Z=F x 10^n (P30) ; Decrease gain if C7 output is more than 2250 mv
1: 2250 F
2: 00 n, Exponent of 10
3: 5 Z Loc [ gain_max ]

6: Z=F x 10^n (P30) ; Don't measure for 2 cycles after a gain change
1: 2 F
2: 00 n, Exponent of 10
3: 6 Z Loc [ gaindelay ]

7: Z=F x 10^n (P30) ; Initialize gain delay counter to zero, so we take a measurement right away
1: 0.0 F
2: 00 n, Exponent of 10
3: 7 Z Loc [ delaycntr ]

8: Do (P86) ; Port 1 low = gain 10
1: 51 Set Port 1 Low
9: Do (P86)
1: 42 Set Port 2 High

10: Do (P86) ; Set flag so we know we initialized and don't need to repeat
1: 18 Set Flag 8 High

11: End (P95)


12: Beginning of Subroutine (P85) ; This is where we increase gain if needed
1: 02 Subroutine 2


13: If (X<=>F) (P89) ; Check to see if we're on gain 10
1: 3 X Loc [ Gain ]
2: 3 >=
3: 9.9 F
4: 30 Then Do ; We're on gain 10

14: Do (P86) ; Set the gain control ports to the proper gain
1: 41 Set Port 1 High

15: Do (P86)
1: 52 Set Port 2 Low

16: Z=F x 10^n (P30) ; Set the gain to 100
1: 100 F
2: 00 n, Exponent of 10
3: 3 Z Loc [ Gain ]

17: End (P95)

18: If (X<=>F) (P89) ; Should be on gain 1
1: 3 X Loc [ Gain ]
2: 4 <
3: 1.1 F
4: 30 Then Do ; If we get here, we're on gain 1, need to increase to gain 10

19: Do (P86) ; Set the gain control ports to the proper gain
1: 51 Set Port 1 Low

20: Do (P86)
1: 42 Set Port 2 High

21: Z=F x 10^n (P30) ; Set the gain to 10
1: 10 F
2: 00 n, Exponent of 10
3: 3 Z Loc [ Gain ]

22: End (P95)

23: Z=X*F (P37) ; Start the delay counter
1: 6 X Loc [ gaindelay ]
2: 1 F
3: 7 Z Loc [ delaycntr ]

24: End (P95)


25: Beginning of Subroutine (P85) ; This is where we decrease gain if needed
1: 03 Subroutine 3

26: If (X<=>F) (P89) ; Check to see if we're on gain 10
1: 3 X Loc [ Gain ]
2: 4 <
3: 10.1 F
4: 30 Then Do ; We're on gain 10, need to go to gain 1

27: Do (P86) ; Set the gain control ports to the proper gain
1: 41 Set Port 1 High

28: Do (P86)
1: 42 Set Port 2 High

29: Z=F x 10^n (P30) ; Set the gain to 1
1: 1 F
2: 00 n, Exponent of 10
3: 3 Z Loc [ Gain ]

30: End (P95)

31: If (X<=>F) (P89) ; Should be on gain 100
1: 3 X Loc [ Gain ]
2: 3 >=
3: 99 F
4: 30 Then Do ; If we get here, we're on gain 100

32: Do (P86) ; Set the gain control ports to the proper gain
1: 42 Set Port 2 High

33: Do (P86)
1: 51 Set Port 1 Low

34: Z=F x 10^n (P30) ; Set the gain to 10
1: 10 F
2: 00 n, Exponent of 10
3: 3 Z Loc [ Gain ]

35: End (P95)


36: Z=X*F (P37) ; Start the delay counter
1: 6 X Loc [ gaindelay ]
2: 1 F
3: 7 Z Loc [ delaycntr ]

37: End (P95)

End Program

-Input Locations-
1 Output 1 1 1
2 C7_output 1 3 1
3 Gain 1 7 5
4 gain_min 1 1 1
5 gain_max 1 1 1
6 gaindelay 1 2 1
7 delaycntr 1 2 4
8 _________ 0 0 0
9 _________ 0 0 0
10 _________ 0 0 0
11 _________ 0 0 0
12 _________ 0 0 0
13 _________ 0 0 0
14 _________ 0 0 0
15 _________ 0 0 0
16 _________ 0 0 0
17 _________ 0 0 0
18 _________ 0 0 0
19 _________ 0 0 0
20 _________ 0 0 0
21 _________ 0 0 0
22 _________ 0 0 0
23 _________ 0 0 0
24 _________ 0 0 0
25 _________ 0 0 0
26 _________ 0 0 0
27 _________ 0 0 0
28 _________ 0 0 0
-Program Security-
0000
0000
0000
-Mode 4-
-Final Storage Area 2-
0
-CR10X ID-
0
-CR10X Power Up-
3
-CR10X Compile Setting-
3
-CR10X RS-232 Setting-
-1
-DLD File Labels-
0
-Final Storage Labels-
0,_RTM,8270
0,_RTM
0,_RTM
0,_RTM
1,Output~1,25814