Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
212 | f9daq | 1 | // NativeExportsConsumerApp.cpp : Defines the entry point for the console application. |
2 | // |
||
3 | #include <utility.h> |
||
4 | #include <stdlib.h> |
||
5 | #include <stdio.h> |
||
6 | #include "AitMduManager.h" |
||
7 | #include "AitMduManager_DEF.h" |
||
8 | |||
9 | //Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: |
||
10 | //Cannot marshal 'return value': Invalid managed/unmanaged type combination. |
||
11 | #ifdef _CVI_ |
||
12 | #include <ansi_c.h> |
||
13 | |||
14 | #define AddMessageI(x) AddMessage("%s %d\n", #x, x); |
||
15 | #define AddMessageF(x) AddMessage("%s %f\n", #x, x); |
||
16 | #define AddMessageX(x) AddMessage("%s 0x%x\n", #x, x); |
||
17 | #define AddMessageS(x) AddMessage("%s %s\n", #x, x); |
||
18 | |||
19 | int sprintf_s(const char *dest, int len, const char *format, ...) { |
||
20 | va_list aptr; |
||
21 | int ret; |
||
22 | |||
23 | |||
24 | va_start(aptr, format); |
||
25 | ret = vsnprintf(dest, len, format, aptr); |
||
26 | va_end(aptr); |
||
27 | return(ret); |
||
28 | } |
||
29 | |||
30 | |||
31 | #endif |
||
32 | |||
33 | typedef unsigned short ushort; |
||
34 | typedef unsigned int uint; |
||
35 | #ifdef __cplusplus |
||
36 | extern "C" |
||
37 | { |
||
38 | #endif |
||
39 | __declspec(dllimport) void NE_Rainbow(); |
||
40 | #ifdef __cplusplus |
||
41 | } |
||
42 | #endif |
||
43 | |||
44 | #ifndef _CVI_ |
||
45 | |||
46 | #ifdef _DEBUG |
||
47 | #pragma comment(lib, "../NativeExports/bin/Debug/NativeExports.lib") |
||
48 | #else |
||
49 | #pragma comment(lib, "../NativeExports/bin/Release/NativeExports.lib") |
||
50 | #endif |
||
51 | |||
52 | #endif |
||
53 | |||
54 | //private MduManager SD4; |
||
55 | uint deviceId = 0; |
||
56 | int deviceIndex = -1; |
||
57 | int adcBufferLen; |
||
58 | ushort *adcBuffer; |
||
59 | int histograms[4][0xFFF]; |
||
60 | int histogramtotal[0xFFF * 4]; |
||
61 | |||
62 | int histogramTotalEvents = 0; |
||
63 | |||
64 | const double MONADC_SCALE = 65535.0; // 16-bit monitor ADC |
||
65 | const double DAC_SCALE = 65535.0; // 16-bit DAC |
||
66 | const double MONADC_VRANGE = 2.5; // 2.5V ADC voltage range |
||
67 | const double DISCR_OFFSETRANGE = 250.0; // +/- 250mV discriminator offset range |
||
68 | const double DISCR_THRESHOLDRANGE = 1000.0; // 0-1000mV discriminator threshold range |
||
69 | const double HV_IRANGE = 5.0; // 0-5.0mA HV current monitor range |
||
70 | const double HV_VRANGE = 80.0; // 0-80V HV voltage control range |
||
71 | const int INTEGRATOR_LSB = 10; // 10ns integrator time resolution |
||
72 | const double RAMPRATESCALE = 61036.0; // Ramp rate register setting = (rate in V/s) / RAMPRATESCALE |
||
73 | const char statusNoDevice[0xFF] = "No Device Connected"; |
||
74 | const char statusReconfig[0xFF] = "Reconfiguring Device ... "; |
||
75 | const char statusReset[0xFF] = "Resetting Device ... "; |
||
76 | const char statusReconnect[0xFF] = "Reconnecting Device ... "; |
||
77 | const char statusDone[0xFF] = "Done"; |
||
78 | |||
79 | |||
80 | const int CHANNELS = 4; // 4-Channel DAQ |
||
81 | const int ADCMAX = 4096; // 12-bit ADC |
||
82 | |||
83 | bool addTimeStamp = false; // updated by ReadRegisters |
||
84 | bool addEventCount = false; // updated by ReadRegisters |
||
85 | bool addChannelNum = false; // updated by ReadRegisters |
||
86 | |||
87 | int TIMESTAMP_LSB = 10; // 10ns time stamp resolution |
||
88 | |||
89 | |||
90 | |||
91 | void Sleep(int musec) { |
||
92 | Delay(musec/1000.); |
||
93 | } |
||
94 | |||
95 | /* |
||
96 | void AddMessage( const char * message) |
||
97 | { |
||
98 | printf("%s\n", message); |
||
99 | } |
||
100 | */ |
||
101 | |||
102 | int AddMessage(const char *format, ...) { |
||
103 | va_list aptr; |
||
104 | int ret; |
||
105 | |||
106 | char strbuf[0xFF]; |
||
107 | |||
108 | va_start(aptr, format); |
||
109 | ret = vsprintf(strbuf, format, aptr); |
||
110 | va_end(aptr); |
||
111 | // SetCtrlVal(p1h,P1_STDIO,strbuf); |
||
112 | |||
113 | printf("%s", strbuf); |
||
114 | |||
115 | return(ret); |
||
116 | } |
||
117 | |||
118 | |||
119 | //---------------------------------------- |
||
120 | // Utilities |
||
121 | //---------------------------------------- |
||
122 | ushort SetBit(ushort data, int bitIndex, bool bitValue) { |
||
123 | if (bitValue == true) |
||
124 | return (ushort)(data | (1 << bitIndex)); |
||
125 | else |
||
126 | return (ushort)(data & ~(1 << bitIndex)); |
||
127 | } |
||
128 | |||
129 | bool GetBit(int data, int bitIndex) { |
||
130 | return (data & (1 << bitIndex)) != 0; |
||
131 | } |
||
132 | |||
133 | ushort ReadSD4Register(int reg) { |
||
134 | return SD4_Read(deviceId, reg); |
||
135 | } |
||
136 | |||
137 | uint ReadSD4Register32(int addrHi, int addrLo) { |
||
138 | uint dataHi = (uint)SD4_Read(deviceId, addrHi); |
||
139 | uint dataLo = (uint)SD4_Read(deviceId, addrLo); |
||
140 | return (dataHi << 16) | dataLo; |
||
141 | } |
||
142 | |||
143 | void WriteSD4Register(int address, ushort data) { |
||
144 | SD4_Write(deviceId, address, data); |
||
145 | } |
||
146 | |||
147 | void WriteSD4Register32(int addrHi, int addrLo, uint data) { |
||
148 | WriteSD4Register(addrHi, (ushort)(data >> 16)); |
||
149 | WriteSD4Register(addrLo, (ushort)data); |
||
150 | } |
||
151 | |||
152 | |||
153 | void ReadModifyWrite(int reg, int bitIndex, bool bitValue) { |
||
154 | ushort regData = ReadSD4Register(reg); |
||
155 | regData = SetBit(regData, bitIndex, bitValue); |
||
156 | WriteSD4Register(reg, regData); |
||
157 | } |
||
158 | |||
159 | double LimitSetting(double value, double min, double max) { |
||
160 | double limited = value; |
||
161 | if (limited > max) limited = max; |
||
162 | if (limited < min) limited = min; |
||
163 | return limited; |
||
164 | } |
||
165 | |||
166 | |||
167 | |||
168 | int ReadAdcBuffer() |
||
169 | { |
||
170 | //if (adcBuffer == NULL) |
||
171 | // return -1; |
||
172 | int bytes = 0; |
||
173 | //printf("In\n%d\n%d\n%d\n", deviceId, ADDR_BUFFER, adcBufferLen); |
||
174 | adcBuffer = SD4_ReadUShortArray(deviceId, ADDR_BUFFER, adcBufferLen, &bytes); |
||
175 | return bytes/2; |
||
176 | } |
||
177 | int ReadAdcByteBuffer() |
||
178 | { |
||
179 | if (adcBuffer == NULL) |
||
180 | return -1; |
||
181 | int bytes = 0; |
||
182 | char * byteBuffer = SD4_ReadByteArray(deviceId, ADDR_BUFFER, adcBufferLen * 2, &bytes); |
||
183 | int byteIndex = 0; |
||
184 | int words = bytes / 2; |
||
185 | ushort dataword = 0; |
||
186 | for (int i = 0; i < words; i++) |
||
187 | { |
||
188 | dataword = (ushort)(byteBuffer[byteIndex++] << 8); |
||
189 | adcBuffer[i] = (ushort)(dataword | byteBuffer[byteIndex++]); |
||
190 | } |
||
191 | return words; |
||
192 | } |
||
193 | |||
194 | |||
195 | |||
196 | |||
197 | |||
198 | //---------------------------------------- |
||
199 | // Monitor ADC |
||
200 | //---------------------------------------- |
||
201 | |||
202 | double GetVoltage(int reg) { |
||
203 | return ReadSD4Register(reg) * MONADC_VRANGE / MONADC_SCALE; |
||
204 | } |
||
205 | |||
206 | double GetDetectorTemperature() { |
||
207 | // Temperature = 500mV + 1mV per degree C |
||
208 | return (GetVoltage(ADDR_BASE_TEMPERATURE) - 0.5) / 0.01; |
||
209 | } |
||
210 | |||
211 | double GetHvVoltage(int reg) { |
||
212 | return GetVoltage(reg) / MONADC_VRANGE * HV_VRANGE; |
||
213 | } |
||
214 | |||
215 | double GetHvCurrentInUa() { |
||
216 | // 2.5V ADC input voltage = 5000 microamps |
||
217 | return GetVoltage(ADDR_HV_IMON) * 2000.0; |
||
218 | } |
||
219 | |||
220 | double GetVoltage75(int reg) { |
||
221 | // 2.5V ADC input voltage = 7.5V monitor voltage |
||
222 | return GetVoltage(reg) * 3.0; |
||
223 | } |
||
224 | |||
225 | |||
226 | |||
227 | //---------------------------------------- |
||
228 | // Discriminator, Sum |
||
229 | //---------------------------------------- |
||
230 | |||
231 | void SetSumGain(int gain) { |
||
232 | ushort csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
233 | bool g1 = GetBit(gain, 0); |
||
234 | bool g2 = GetBit(gain, 1); |
||
235 | bool g3 = GetBit(gain, 2); |
||
236 | bool g4 = GetBit(gain, 3); |
||
237 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_SUMGAIN1, g1); |
||
238 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_SUMGAIN2, g2); |
||
239 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_SUMGAIN3, g3); |
||
240 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_SUMGAIN4, g4); |
||
241 | WriteSD4Register(ADDR_CSR_DETECTOR, (ushort)csrDetector); |
||
242 | } |
||
243 | |||
244 | void SetSumCoupling(bool acCoupling) { |
||
245 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_SUMCOUPLING, acCoupling); |
||
246 | } |
||
247 | |||
248 | void SetThreshold(double thresholdInMillivolts) { |
||
249 | thresholdInMillivolts = LimitSetting(thresholdInMillivolts, -DISCR_THRESHOLDRANGE, DISCR_THRESHOLDRANGE); |
||
250 | WriteSD4Register(ADDR_DISCR_THRESHOLD, (ushort)(thresholdInMillivolts * DAC_SCALE / DISCR_THRESHOLDRANGE)); |
||
251 | } |
||
252 | |||
253 | double GetThreshold() { |
||
254 | return ReadSD4Register(ADDR_DISCR_THRESHOLD) / DAC_SCALE * DISCR_THRESHOLDRANGE; |
||
255 | } |
||
256 | |||
257 | double GetSumOffsetInMv() { |
||
258 | // Sum offsets are with respect to the positive sum output polarity. |
||
259 | return (ReadSD4Register(ADDR_DISCR_OFFSET) / DAC_SCALE * (DISCR_OFFSETRANGE * 2)) - DISCR_OFFSETRANGE; |
||
260 | } |
||
261 | |||
262 | void SetSumOffset(double milliVolts) { |
||
263 | // Sum offsets are with respect to the positive sum output polarity. |
||
264 | // Actual output offset is multiplied by the selectable gain stage. |
||
265 | WriteSD4Register(ADDR_DISCR_OFFSET, (ushort)((milliVolts + DISCR_OFFSETRANGE) * DAC_SCALE / (DISCR_OFFSETRANGE * 2))); |
||
266 | } |
||
267 | |||
268 | |||
269 | |||
270 | //---------------------------------------- |
||
271 | // Trigger |
||
272 | //---------------------------------------- |
||
273 | |||
274 | void SetTriggerInterval(int triggerInterval) { |
||
275 | WriteSD4Register32(ADDR_TG_INTERVALHI, ADDR_TG_INTERVALLO, (uint)(triggerInterval / INTEGRATOR_LSB)); |
||
276 | } |
||
277 | |||
278 | void SetTriggerBurst(int triggerBurst) { |
||
279 | WriteSD4Register32(ADDR_TG_COUNTHI, ADDR_TG_COUNTLO, (uint)triggerBurst); |
||
280 | } |
||
281 | |||
282 | void EnableTrigger(int bitIndex) { |
||
283 | WriteSD4Register(ADDR_CSR_TRIGGER, SetBit(0, bitIndex, true)); |
||
284 | } |
||
285 | |||
286 | void DisableTriggers() { |
||
287 | WriteSD4Register(ADDR_CSR_TRIGGER, 0); |
||
288 | } |
||
289 | |||
290 | void SetDeadTime(int deadTimeInNs) { |
||
291 | WriteSD4Register(ADDR_DEADTIME, (ushort)(deadTimeInNs / INTEGRATOR_LSB)); |
||
292 | } |
||
293 | |||
294 | |||
295 | |||
296 | //---------------------------------------- |
||
297 | // Integrators, ADCs |
||
298 | //---------------------------------------- |
||
299 | |||
300 | void SetIntegrationTime(int integrationTime) { |
||
301 | WriteSD4Register(ADDR_ITIME, (ushort)(integrationTime / INTEGRATOR_LSB)); |
||
302 | } |
||
303 | |||
304 | int GetIntegrationTime() { |
||
305 | return ReadSD4Register(ADDR_ITIME) * INTEGRATOR_LSB; |
||
306 | } |
||
307 | |||
308 | void SetAdcCoupling(bool acCoupling) { |
||
309 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_ADCCOUPLING, acCoupling); |
||
310 | } |
||
311 | |||
312 | void ResetAdc(bool reset) { |
||
313 | ushort cmd = 0; |
||
314 | cmd = SetBit(cmd, BIT_CSR_MAIN_ADCRESET, reset); |
||
315 | WriteSD4Register(ADDR_CSR_MAIN, cmd); |
||
316 | } |
||
317 | |||
318 | double GetAdcOffsetInMv(int reg) { |
||
319 | // ADC offset polarity is with respect to the positive ADC polarity (integrator output), not the main negative input signal polarity. |
||
320 | // Actual output offset is multiplied by the selectable gain stage. |
||
321 | return DISCR_OFFSETRANGE - (ReadSD4Register(reg) / DAC_SCALE * (DISCR_OFFSETRANGE * 2)); |
||
322 | } |
||
323 | |||
324 | void SetAdcOffset(int reg, double milliVolts) { |
||
325 | // ADC offset polarity is with respect to the positive ADC polarity (integrator output), not the main negative input signal polarity. |
||
326 | // Actual output offset is multiplied by the selectable gain stage. |
||
327 | WriteSD4Register(reg, (ushort)((DISCR_OFFSETRANGE - milliVolts) * DAC_SCALE / (DISCR_OFFSETRANGE * 2))); |
||
328 | } |
||
329 | |||
330 | |||
331 | |||
332 | //---------------------------------------- |
||
333 | // Event ID |
||
334 | //---------------------------------------- |
||
335 | |||
336 | void AddEventCount(bool addEventCount) { |
||
337 | ReadModifyWrite(ADDR_EVENTID, BIT_EVENTID_COUNT, addEventCount); |
||
338 | } |
||
339 | |||
340 | void AddTimeStamp(bool addTimeStamp) { |
||
341 | ReadModifyWrite(ADDR_EVENTID, BIT_EVENTID_TIME, addTimeStamp); |
||
342 | } |
||
343 | |||
344 | void AddChannelNumber(bool addChannelNumber) { |
||
345 | ReadModifyWrite(ADDR_EVENTID, BIT_EVENTID_CHNUM, addChannelNumber); |
||
346 | } |
||
347 | |||
348 | |||
349 | |||
350 | //---------------------------------------- |
||
351 | // Detector VA |
||
352 | //---------------------------------------- |
||
353 | |||
354 | void SetDetectorVaOn(bool on) { |
||
355 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_VAENABLE, on); |
||
356 | } |
||
357 | |||
358 | void SetDetectorVaHiRange(bool range) { |
||
359 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_VAHIRNG, range); |
||
360 | } |
||
361 | |||
362 | |||
363 | |||
364 | //---------------------------------------- |
||
365 | // High Voltage |
||
366 | //---------------------------------------- |
||
367 | |||
368 | void SetHvCurrentLimit(double milliamps) { |
||
369 | milliamps = LimitSetting(milliamps, 0, HV_IRANGE); |
||
370 | WriteSD4Register(ADDR_HV_ILIMIT, (ushort)(milliamps * DAC_SCALE / HV_IRANGE)); |
||
371 | } |
||
372 | |||
373 | void SetHvVoltage(int reg, double volts) { |
||
374 | volts = LimitSetting(volts, 0, HV_VRANGE); |
||
375 | WriteSD4Register(reg, (ushort)(volts * DAC_SCALE / HV_VRANGE)); |
||
376 | } |
||
377 | |||
378 | double GetHvIlimitSetting() { |
||
379 | return ReadSD4Register(ADDR_HV_ILIMIT) / DAC_SCALE * HV_IRANGE; |
||
380 | } |
||
381 | |||
382 | double GetHvVoltageSetting() { |
||
383 | return ReadSD4Register(ADDR_HV_CTRL) / DAC_SCALE * HV_VRANGE; |
||
384 | } |
||
385 | |||
386 | void SetHvOn(bool hvOn) { |
||
387 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_HVENABLE, hvOn); |
||
388 | } |
||
389 | |||
390 | void SetMaximumHvSetting(double volts) { |
||
391 | SetHvVoltage(ADDR_HV_MAX, volts); |
||
392 | } |
||
393 | |||
394 | void CycleHvReset() { |
||
395 | // CAUTION: Holding HV in reset will force it to ignore the current limit |
||
396 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_HVRESET, true); |
||
397 | ReadModifyWrite(ADDR_CSR_DETECTOR, BIT_CSR_DET_HVRESET, false); |
||
398 | } |
||
399 | |||
400 | void SetRampRate(double voltsPerSecond) { |
||
401 | WriteSD4Register(ADDR_HV_RAMPRATE, (ushort)(RAMPRATESCALE / voltsPerSecond)); |
||
402 | } |
||
403 | |||
404 | double GetRampRate() { |
||
405 | return RAMPRATESCALE / (ReadSD4Register(ADDR_HV_RAMPRATE) + 1); |
||
406 | } |
||
407 | |||
408 | void SetAdcGain(int gain) { |
||
409 | ushort csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
410 | bool g1 = GetBit(gain, 0); |
||
411 | bool g2 = GetBit(gain, 1); |
||
412 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_ADCGAIN1, g1); |
||
413 | csrDetector = SetBit(csrDetector, BIT_CSR_DET_ADCGAIN2, g2); |
||
414 | WriteSD4Register(ADDR_CSR_DETECTOR, (ushort)csrDetector); |
||
415 | } |
||
416 | |||
417 | void SetAdcOffsets(int offsetInMv) { |
||
418 | //nudOffset1.Value = offsetInMv; |
||
419 | //nudOffset2.Value = offsetInMv; |
||
420 | //nudOffset3.Value = offsetInMv; |
||
421 | //nudOffset4.Value = offsetInMv; |
||
422 | SetAdcOffset(ADDR_OFFSET1, offsetInMv); |
||
423 | SetAdcOffset(ADDR_OFFSET2, offsetInMv); |
||
424 | SetAdcOffset(ADDR_OFFSET3, offsetInMv); |
||
425 | SetAdcOffset(ADDR_OFFSET4, offsetInMv); |
||
426 | } |
||
427 | |||
428 | void QuickSetupContinuous() { |
||
429 | // Set ADC reset |
||
430 | ResetAdc(true); |
||
431 | |||
432 | // Clear ADC reset |
||
433 | ResetAdc(false); |
||
434 | |||
435 | // Set trigger dead time to 200ns |
||
436 | SetDeadTime(200); |
||
437 | |||
438 | // Set event ID |
||
439 | AddTimeStamp(true); |
||
440 | AddEventCount(true); |
||
441 | AddChannelNumber(true); |
||
442 | |||
443 | // Enable detector amplifier voltage |
||
444 | SetDetectorVaHiRange(false); |
||
445 | SetDetectorVaOn(true); |
||
446 | |||
447 | /* |
||
448 | // Set HV current limit |
||
449 | double iLimit = 2.5; |
||
450 | nudHvIlimit.Value = (decimal)iLimit; |
||
451 | SetHvCurrentLimit(iLimit); |
||
452 | |||
453 | // Reset any HV faults |
||
454 | CycleHvReset(); |
||
455 | |||
456 | // Enable bias voltage |
||
457 | SetHvOn(false); |
||
458 | SetHvOn(true); |
||
459 | |||
460 | // Set bias voltage |
||
461 | double hvVoltage = 30.0; |
||
462 | nudHvVoltage.Value = (decimal)hvVoltage; |
||
463 | SetHvVoltage(ADDR_HV_CTRL, hvVoltage); |
||
464 | */ |
||
465 | |||
466 | // Set integration time |
||
467 | int integrationTime = 350; |
||
468 | //nudIntegrationTime.Value = integrationTime; |
||
469 | SetIntegrationTime(integrationTime); |
||
470 | |||
471 | // Set discriminator threshold |
||
472 | int threshold = 50; |
||
473 | //nudThreshold.Value = threshold; |
||
474 | SetThreshold(threshold); |
||
475 | |||
476 | // Set sum coupling |
||
477 | SetSumCoupling(true); |
||
478 | |||
479 | // Set sum gain |
||
480 | int sumGain = 3; |
||
481 | //nudSumGain.Value = sumGain; |
||
482 | SetSumGain(sumGain); |
||
483 | |||
484 | // Set sum offset |
||
485 | int offset = 0; |
||
486 | //nudSumOffset.Value = offset; |
||
487 | SetSumOffset(offset); |
||
488 | |||
489 | // Set ADC coupling |
||
490 | SetAdcCoupling(false); |
||
491 | |||
492 | // Set ADC gains |
||
493 | int gain = 1; |
||
494 | //nudAdcGain.Value = gain; |
||
495 | SetAdcGain(gain); |
||
496 | |||
497 | // Set all ADC offsets |
||
498 | SetAdcOffsets(5); |
||
499 | |||
500 | // Set internal trigger interval |
||
501 | int triggerInterval = 1000; |
||
502 | //nudTriggerInterval.Value = triggerInterval; |
||
503 | SetTriggerInterval(triggerInterval); |
||
504 | |||
505 | // Enable continuous trigger |
||
506 | EnableTrigger(BIT_CSR_TRIG_CONTINUOUS); |
||
507 | |||
508 | // Begin continuous acquisition |
||
509 | //nudEventsPerAcquisition.Value = 50000; |
||
510 | //cbAcquireContinuous.Checked = true; |
||
511 | |||
512 | // Update histogram offsets |
||
513 | //SetAllHistogramOffsets(0); |
||
514 | } |
||
515 | |||
516 | |||
517 | void QuickSetupDiscriminator() { |
||
518 | // Set ADC reset |
||
519 | ResetAdc(true); |
||
520 | |||
521 | // Clear ADC reset |
||
522 | ResetAdc(false); |
||
523 | |||
524 | // Set trigger dead time to 300ns |
||
525 | SetDeadTime(500); |
||
526 | |||
527 | // Set event ID |
||
528 | AddTimeStamp(true); |
||
529 | AddEventCount(true); |
||
530 | AddChannelNumber(true); |
||
531 | |||
532 | // Enable detector amplifier voltage |
||
533 | SetDetectorVaHiRange(true); |
||
534 | SetDetectorVaOn(true); |
||
535 | |||
536 | // Set HV current limit |
||
537 | double iLimit = 2.5; |
||
538 | //nudHvIlimit.Value = (decimal)iLimit; |
||
539 | SetHvCurrentLimit(iLimit); |
||
540 | |||
541 | // Reset any HV faults |
||
542 | CycleHvReset(); |
||
543 | |||
544 | // Enable bias voltage |
||
545 | SetHvOn(false); |
||
546 | SetHvOn(true); |
||
547 | |||
548 | // Set bias voltage |
||
549 | double hvVoltage = 28.0; |
||
550 | //nudHvVoltage.Value = (decimal)hvVoltage; |
||
551 | SetHvVoltage(ADDR_HV_CTRL, hvVoltage); |
||
552 | |||
553 | // Set integration time |
||
554 | int integrationTime = 350; |
||
555 | //nudIntegrationTime.Value = integrationTime; |
||
556 | SetIntegrationTime(integrationTime); |
||
557 | |||
558 | // Set discriminator threshold |
||
559 | int threshold = 50; |
||
560 | //nudThreshold.Value = threshold; |
||
561 | SetThreshold(threshold); |
||
562 | |||
563 | // Set sum coupling |
||
564 | SetSumCoupling(true); |
||
565 | |||
566 | // Set sum gain |
||
567 | int sumGain = 3; |
||
568 | //nudSumGain.Value = sumGain; |
||
569 | SetSumGain(sumGain); |
||
570 | |||
571 | // Set sum offset |
||
572 | int offset = 0; |
||
573 | //nudSumOffset.Value = offset; |
||
574 | SetSumOffset(offset); |
||
575 | |||
576 | // Set ADC coupling |
||
577 | SetAdcCoupling(false); |
||
578 | |||
579 | // Set ADC gains |
||
580 | int gain = 1; |
||
581 | //nudAdcGain.Value = gain; |
||
582 | SetAdcGain(gain); |
||
583 | |||
584 | // Set all ADC offsets |
||
585 | SetAdcOffsets(5); |
||
586 | |||
587 | // Clear histograms |
||
588 | //ClearHistograms(); |
||
589 | |||
590 | // Enable discriminator trigger |
||
591 | EnableTrigger(BIT_CSR_TRIG_DISCR); |
||
592 | |||
593 | // Begin continuous acquisition |
||
594 | //nudEventsPerAcquisition.Value = 1000; |
||
595 | //cbAcquireContinuous.Checked = true; |
||
596 | |||
597 | // Update histogram offsets |
||
598 | //SetAllHistogramOffsets(0); |
||
599 | } |
||
600 | |||
601 | void QuickSetupTriggerOff() { |
||
602 | //cbAcquireContinuous.Checked = false; |
||
603 | DisableTriggers(); |
||
604 | SetHvOn(false); |
||
605 | SetDetectorVaOn(false); |
||
606 | } |
||
607 | |||
608 | void ReadRegisters() { |
||
609 | if (deviceIndex < 0) |
||
610 | return; |
||
611 | |||
612 | // Voltages |
||
613 | double lHvImon = GetHvCurrentInUa(); |
||
614 | double HvVmon = GetHvVoltage(ADDR_HV_VMON); |
||
615 | double HvVmax = GetHvVoltage(ADDR_HV_MAX); |
||
616 | double lDetPva = GetVoltage75(ADDR_BASE_PVA); |
||
617 | double lDetNva = GetVoltage75(ADDR_BASE_NVA); |
||
618 | double lPva = GetVoltage75(ADDR_MAIN_PVA); |
||
619 | double lNva = GetVoltage75(ADDR_MAIN_NVA); |
||
620 | double l12V = GetVoltage(ADDR_MAIN_P12); |
||
621 | double l33V = GetVoltage75(ADDR_MAIN_P33); |
||
622 | double l50V = GetVoltage75(ADDR_MAIN_P50); |
||
623 | double Temperature = GetDetectorTemperature(); |
||
624 | double lRampRate = GetRampRate(); |
||
625 | |||
626 | |||
627 | AddMessageF(lHvImon); |
||
628 | AddMessageF(HvVmon); |
||
629 | AddMessageF(HvVmax); |
||
630 | AddMessageF(lDetPva); |
||
631 | AddMessageF(lDetNva); |
||
632 | AddMessageF(lPva); |
||
633 | AddMessageF(lNva); |
||
634 | AddMessageF(l12V); |
||
635 | AddMessageF(l33V); |
||
636 | AddMessageF(l50V); |
||
637 | AddMessageF(Temperature); |
||
638 | AddMessageF(lRampRate); |
||
639 | |||
640 | |||
641 | |||
642 | |||
643 | |||
644 | // Integration time |
||
645 | int lItime = GetIntegrationTime(); |
||
646 | |||
647 | // Buffer memory used |
||
648 | int lBufferWords = ReadSD4Register(ADDR_BUFFERWORDS); |
||
649 | |||
650 | // Offsets |
||
651 | double lOffset1 = GetAdcOffsetInMv(ADDR_OFFSET1); |
||
652 | double lOffset2 = GetAdcOffsetInMv(ADDR_OFFSET2); |
||
653 | double lOffset3 = GetAdcOffsetInMv(ADDR_OFFSET3); |
||
654 | double lOffset4 = GetAdcOffsetInMv(ADDR_OFFSET4); |
||
655 | double lSumOffset = GetSumOffsetInMv(); |
||
656 | |||
657 | // Main CSR |
||
658 | int csrMain = ReadSD4Register(ADDR_CSR_MAIN); |
||
659 | int lPvaStatus = GetBit(csrMain, BIT_CSR_MAIN_PVA); |
||
660 | int lNvaStatus = GetBit(csrMain, BIT_CSR_MAIN_NVA); |
||
661 | int l12vStatus = GetBit(csrMain, BIT_CSR_MAIN_P12); |
||
662 | int l33vStatus = GetBit(csrMain, BIT_CSR_MAIN_P33); |
||
663 | int l5vStatus = GetBit(csrMain, BIT_CSR_MAIN_P50); |
||
664 | |||
665 | |||
666 | AddMessageF(lOffset1); |
||
667 | AddMessageF(lOffset2); |
||
668 | AddMessageF(lOffset3); |
||
669 | AddMessageF(lOffset4); |
||
670 | AddMessageF(lSumOffset); |
||
671 | AddMessageX(csrMain); |
||
672 | AddMessageX(lNvaStatus); |
||
673 | AddMessageX(lNvaStatus); |
||
674 | AddMessageX(l12vStatus); |
||
675 | AddMessageX(l33vStatus); |
||
676 | AddMessageX(l5vStatus); |
||
677 | |||
678 | AddMessageI(lBufferWords); |
||
679 | AddMessageI(lItime); |
||
680 | |||
681 | // Trigger CSR |
||
682 | // Assumes only one trigger source is active |
||
683 | char lTriggerSource[0xFF]=""; |
||
684 | int csrTrigger = ReadSD4Register(ADDR_CSR_TRIGGER); |
||
685 | if (GetBit(csrTrigger, BIT_CSR_TRIG_CONTINUOUS)) |
||
686 | sprintf_s(lTriggerSource, 0xFF, "CONTINUOUS"); |
||
687 | else if (GetBit(csrTrigger, BIT_CSR_TRIG_BURST)) |
||
688 | sprintf_s(lTriggerSource, 0xFF, "BURST"); |
||
689 | else if (GetBit(csrTrigger, BIT_CSR_TRIG_DISCR)) |
||
690 | sprintf_s(lTriggerSource, 0xFF, "DISCRIMINATOR"); |
||
691 | else if (GetBit(csrTrigger, BIT_CSR_TRIG_EXTERNAL)) |
||
692 | sprintf_s(lTriggerSource, 0xFF, "EXTERNAL"); |
||
693 | else |
||
694 | sprintf_s(lTriggerSource, 0xFF, "OFF"); |
||
695 | |||
696 | // Event ID CSR |
||
697 | int csrEventId = ReadSD4Register(ADDR_EVENTID); |
||
698 | addTimeStamp = GetBit(csrEventId, BIT_EVENTID_TIME); |
||
699 | addEventCount = GetBit(csrEventId, BIT_EVENTID_COUNT); |
||
700 | addChannelNum = GetBit(csrEventId, BIT_EVENTID_CHNUM); |
||
701 | //lTimeStamp.Text = addTimeStamp ? "+" : ""; |
||
702 | //lEventCount.Text = addEventCount ? "+" : ""; |
||
703 | //lChannelNum.Text = addChannelNum ? "+" : ""; |
||
704 | AddMessageS(lTriggerSource); |
||
705 | AddMessageI(addTimeStamp); |
||
706 | AddMessageI(addEventCount); |
||
707 | AddMessageI(addEventCount); |
||
708 | |||
709 | // Trigger generator |
||
710 | int lTriggerInterval = ReadSD4Register32(ADDR_TG_INTERVALHI, ADDR_TG_INTERVALLO) * 10; |
||
711 | int lTrigCount = ReadSD4Register32(ADDR_TG_COUNTHI, ADDR_TG_COUNTLO); |
||
712 | ushort dum = 0; |
||
713 | WriteSD4Register(ADDR_TC_COUNTLO, dum); // latch counters by writing to any counter register |
||
714 | int lTriggers = ReadSD4Register32(ADDR_TC_COUNTHI, ADDR_TC_COUNTLO); |
||
715 | int lTriggerRate = ReadSD4Register32(ADDR_TC_RATEHI, ADDR_TC_RATELO); |
||
716 | int lAdcConversions = ReadSD4Register32(ADDR_CONV_COUNTHI, ADDR_CONV_COUNTLO); |
||
717 | double lThreshold = GetThreshold(); |
||
718 | double lHvIlimit = GetHvIlimitSetting(); |
||
719 | double lHvVoltage = GetHvVoltageSetting(); |
||
720 | |||
721 | |||
722 | AddMessageI(lTriggerInterval); |
||
723 | AddMessageI(lTrigCount); |
||
724 | AddMessageI(lTriggers); |
||
725 | AddMessageI(lTriggerRate); |
||
726 | AddMessageI(lAdcConversions); |
||
727 | AddMessageF(lThreshold); |
||
728 | AddMessageF(lHvIlimit); |
||
729 | AddMessageF(lHvVoltage); |
||
730 | |||
731 | |||
732 | |||
733 | // Detector CSR |
||
734 | int csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
735 | int lHvEnabled = GetBit(csrDetector, BIT_CSR_DET_HVENABLE); |
||
736 | int lHvOn = GetBit(csrDetector, BIT_CSR_DET_HVGOOD); |
||
737 | int lSumCoupling = GetBit(csrDetector, BIT_CSR_DET_SUMCOUPLING); |
||
738 | int lSumGain = |
||
739 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN4) ? 1 : 0) << 3) | |
||
740 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN3) ? 1 : 0) << 2) | |
||
741 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN2) ? 1 : 0) << 1) | |
||
742 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN1) ? 1 : 0) << 0); |
||
743 | int lAdcCoupling = GetBit(csrDetector, BIT_CSR_DET_ADCCOUPLING); |
||
744 | int lAdcGain = ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN2) ? 1 : 0) << 1) | |
||
745 | ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN1) ? 1 : 0) << 0); |
||
746 | int lAmpEnabled = GetBit(csrDetector, BIT_CSR_DET_VAENABLE); |
||
747 | int lAmpLevel = GetBit(csrDetector, BIT_CSR_DET_VAHIRNG); |
||
748 | int lDetPvaStatus = GetBit(csrDetector, BIT_CSR_DET_PVAGOOD); |
||
749 | int lDetNvaStatus = GetBit(csrDetector, BIT_CSR_DET_NVAGOOD); |
||
750 | int lTemperatureOn = GetBit(csrDetector, BIT_CSR_DET_TEMPVALID); |
||
751 | AddMessageI(lHvEnabled); |
||
752 | AddMessageI(lHvOn); |
||
753 | AddMessageI(lSumCoupling); |
||
754 | AddMessageI(lSumGain); |
||
755 | AddMessageI(lAdcCoupling); |
||
756 | AddMessageI(lAdcGain ); |
||
757 | AddMessageI(lAmpEnabled); |
||
758 | AddMessageI(lAmpLevel); |
||
759 | AddMessageI(lDetPvaStatus ); |
||
760 | AddMessageI(lDetNvaStatus); |
||
761 | AddMessageI(lTemperatureOn); |
||
762 | |||
763 | } |
||
764 | |||
765 | |||
766 | |||
767 | //---------------------------------------- |
||
768 | // Message list |
||
769 | //---------------------------------------- |
||
770 | |||
771 | |||
772 | |||
773 | |||
774 | //---------------------------------------- |
||
775 | // MDU Events |
||
776 | //---------------------------------------- |
||
777 | /* |
||
778 | private void AitDeviceAttached(object sender, EventArgs e) |
||
779 | { |
||
780 | MduManager.DeviceChangedEventArgs aitEventArgs = e as MduManager.DeviceChangedEventArgs; |
||
781 | AddMessage(String.Format("Attached : Index={0}, ID={1:X8}", aitEventArgs.DeviceIndex, aitEventArgs.DeviceId)); |
||
782 | UpdateDeviceList(); |
||
783 | } |
||
784 | |||
785 | private void AitDeviceRemoved(object sender, EventArgs e) |
||
786 | { |
||
787 | MduManager.DeviceChangedEventArgs aitEventArgs = e as MduManager.DeviceChangedEventArgs; |
||
788 | AddMessage(String.Format("Removed : Index={0}, ID={1:X8}", aitEventArgs.DeviceIndex, aitEventArgs.DeviceId)); |
||
789 | UpdateDeviceList(); |
||
790 | } |
||
791 | |||
792 | */ |
||
793 | |||
794 | //---------------------------------------- |
||
795 | // Device Control |
||
796 | //---------------------------------------- |
||
797 | |||
798 | void GetDeviceId() { |
||
799 | deviceIndex = 0; |
||
800 | deviceId = 1; |
||
801 | /* |
||
802 | if (tscbDeviceList.SelectedIndex >= 0) |
||
803 | { |
||
804 | deviceIndex = 0; // tscbDeviceList.SelectedIndex; |
||
805 | deviceId = 1; //UInt32.Parse((string)tscbDeviceList.SelectedItem, System.Globalization.NumberStyles.HexNumber); |
||
806 | } |
||
807 | else |
||
808 | { |
||
809 | deviceIndex = -1; |
||
810 | deviceId = 0; |
||
811 | } |
||
812 | */ |
||
813 | } |
||
814 | |||
815 | int UpdateDeviceList() |
||
816 | { |
||
817 | |||
818 | deviceIndex = -1; |
||
819 | deviceId = 0; |
||
820 | int deviceCount = 0; |
||
821 | uint * devIds = SD4_GetDeviceList(&deviceCount); |
||
822 | for (int i = 0; i < deviceCount; i++) { |
||
823 | printf("Device %d = 0x%8x\t", i, devIds[i]); |
||
824 | deviceId = devIds[i]; |
||
825 | deviceIndex = i; |
||
826 | } |
||
827 | return deviceCount; |
||
828 | } |
||
829 | |||
830 | |||
831 | void ResetDevice() { |
||
832 | if (deviceIndex >= 0) { |
||
833 | AddMessage(statusReset); |
||
834 | SD4_ResetFpga(deviceIndex); |
||
835 | Sleep(500); |
||
836 | UpdateDeviceList(); |
||
837 | AddMessage(statusReset); |
||
838 | AddMessage(statusDone); |
||
839 | } |
||
840 | else |
||
841 | AddMessage(statusNoDevice); |
||
842 | } |
||
843 | |||
844 | void ReconnectDevice() { |
||
845 | if (deviceIndex >= 0) { |
||
846 | AddMessage(statusReconnect); |
||
847 | SD4_Reconnect(deviceIndex); |
||
848 | Sleep(500); |
||
849 | AddMessage(statusReconnect); |
||
850 | AddMessage(statusDone); |
||
851 | } |
||
852 | else |
||
853 | AddMessage(statusNoDevice); |
||
854 | } |
||
855 | |||
856 | void ReconfigureDevice() { |
||
857 | if (deviceIndex >= 0) { |
||
858 | AddMessage(statusReconfig); |
||
859 | SD4_Reconfigure(deviceIndex); |
||
860 | Sleep(500); |
||
861 | SD4_ResetFpga(deviceIndex); |
||
862 | UpdateDeviceList(); |
||
863 | AddMessage(statusReconfig); |
||
864 | AddMessage(statusDone); |
||
865 | } |
||
866 | else |
||
867 | AddMessage(statusNoDevice); |
||
868 | } |
||
869 | |||
870 | //---------------------------------------- |
||
871 | // Initialize Device Connection |
||
872 | //---------------------------------------- |
||
873 | |||
874 | void InitializeConnection() { |
||
875 | int ndevices = 0; |
||
876 | unsigned int * devlist = SD4_FindDevices(&ndevices); |
||
877 | printf("Found %d devices\t", ndevices); |
||
878 | //SD4.DeviceAttached += new EventHandler(AitDeviceAttached); |
||
879 | //SD4.DeviceRemoved += new EventHandler(AitDeviceRemoved); |
||
880 | //tscbDeviceList.SelectedIndexChanged += new EventHandler(tscbDeviceList_SelectedIndexChanged); |
||
881 | printf("Updated devices %d\n", UpdateDeviceList()); |
||
882 | |||
883 | |||
884 | |||
885 | |||
886 | // Set above the maximum bias voltage to prevent accidental over-bias. |
||
887 | // For example for 30V bias, set to 32V. For 70V bias, set to 72V |
||
888 | SetMaximumHvSetting(32.0); |
||
889 | |||
890 | // Set TTL port to 0 |
||
891 | WriteSD4Register(ADDR_TTLPORT, 0); |
||
892 | |||
893 | // Set HV ramp rate to 20 V/s |
||
894 | SetRampRate(20); |
||
895 | |||
896 | // 1 second register scan interval |
||
897 | //SetupTimer(1.0); |
||
898 | |||
899 | } |
||
900 | |||
901 | |||
902 | int GetAdcEventSize() { |
||
903 | int eventSize = CHANNELS; |
||
904 | if (addTimeStamp == true) |
||
905 | eventSize += 3; // 3 16-bit words for time stamp |
||
906 | if (addEventCount == true) |
||
907 | eventSize += 2; // 2 16-bit words for event count |
||
908 | return eventSize; |
||
909 | } |
||
910 | |||
911 | int GetEventPosition(int eventNum) { |
||
912 | int adcBoardEventSize = GetAdcEventSize(); |
||
913 | return adcBoardEventSize * eventNum; |
||
914 | } |
||
915 | |||
916 | ushort ExtractAdcData(int eventNum, int channel) { |
||
917 | int index = GetEventPosition(eventNum) + channel; |
||
918 | return adcBuffer[index]; |
||
919 | } |
||
920 | |||
921 | long ExtractTimeStamp(int eventNum) { |
||
922 | int index = GetEventPosition(eventNum) + CHANNELS; |
||
923 | long timestamp = 0; |
||
924 | for (int i = 0; i < 3; i++) |
||
925 | timestamp = (timestamp << 16) | adcBuffer[index++]; |
||
926 | return timestamp; |
||
927 | } |
||
928 | |||
929 | int ExtractEventCount(int eventNum) { |
||
930 | int offset = 0; |
||
931 | if (addTimeStamp == true) |
||
932 | offset += 3; |
||
933 | int index = GetEventPosition(eventNum) + CHANNELS + offset; |
||
934 | int eventCount = adcBuffer[index]; |
||
935 | eventCount = (eventCount << 16) | adcBuffer[index + 1]; |
||
936 | return eventCount; |
||
937 | } |
||
938 | |||
939 | void ShowEvent(int eventNumber) { |
||
940 | if (eventNumber < 1) |
||
941 | return; |
||
942 | eventNumber--; |
||
943 | char lAdcTimeStamp[0xFF]=""; |
||
944 | char lAdcAbsTimeStamp[0xFF]=""; |
||
945 | char lAdcEventCount[0xFF]=""; |
||
946 | //tbAdcData.Clear(); |
||
947 | int eventPosition = GetEventPosition(eventNumber); |
||
948 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
949 | int data = ExtractAdcData(eventNumber, channel); |
||
950 | AddMessage("AdcData event %d channel:%d data:%d\n", eventNumber, channel + 1, data); |
||
951 | } |
||
952 | if (addTimeStamp == true) { |
||
953 | long reference = ExtractTimeStamp(0); |
||
954 | long timeStamp = ExtractTimeStamp(eventNumber); |
||
955 | sprintf_s(lAdcTimeStamp, 0xFF, "%d", ((timeStamp - reference) * TIMESTAMP_LSB)); |
||
956 | sprintf_s(lAdcAbsTimeStamp, 0xFF, "%d", (timeStamp * TIMESTAMP_LSB)); |
||
957 | } |
||
958 | else { |
||
959 | sprintf_s(lAdcTimeStamp, 0xFF, "%s", "----"); |
||
960 | sprintf_s(lAdcAbsTimeStamp, 0xFF, "%s", "----"); |
||
961 | } |
||
962 | if (addEventCount == true) { |
||
963 | sprintf_s(lAdcEventCount, 0xFF, "%d", ExtractEventCount(eventNumber)); |
||
964 | } |
||
965 | else { |
||
966 | sprintf_s(lAdcEventCount, 0xFF, "%s", "----"); |
||
967 | } |
||
968 | //SetTextboxToTop(tbAdcData); |
||
969 | } |
||
970 | |||
971 | int BlockTransferTime() { |
||
972 | return SD4_BlockTransferTime(); |
||
973 | } |
||
974 | void InitInterface(int events) { |
||
975 | adcBufferLen = GetAdcEventSize() * events; |
||
976 | //adcBuffer = (ushort *)malloc(sizeof(ushort) * adcBufferLen); |
||
977 | /* |
||
978 | for (int i = 0; i < 0xFFF; i++) { |
||
979 | for (int j = 0; j < 4; j++) histograms[j][i] = 0; |
||
980 | } |
||
981 | for (int i = 0; i < 4 * 0xFFF; i++) { |
||
982 | histogramtotal[i] = 0; |
||
983 | } |
||
984 | */ |
||
985 | histogramTotalEvents = 0; |
||
986 | } |
||
987 | |||
988 | int Acquire(int events) { |
||
989 | //InitializeConnection(); |
||
990 | // Acquire |
||
991 | InitInterface(events); |
||
992 | //int wordsAcquired = ReadAdcByteBuffer(); // use only to test byte buffer read operation |
||
993 | int wordsAcquired = ReadAdcBuffer(); |
||
994 | if (wordsAcquired != adcBufferLen) { |
||
995 | char msg[0xFF]; |
||
996 | AddMessage("ERROR: Timeout with possible data loss (%d/%d words received; Block Error = %d)\n", wordsAcquired, adcBufferLen, SD4_BlockError()); |
||
997 | return 0; |
||
998 | } |
||
999 | |||
1000 | |||
1001 | |||
1002 | /* |
||
1003 | lTransferRate.Text = String.Format("{0:f2}", transferRate); |
||
1004 | lAcquisitionSize.Text = String.Format("{0:f2}", blocksize / 1e6); // block size in MB |
||
1005 | lAcquisitionTime.Text = String.Format("{0:f2}", microseconds / 1e6); |
||
1006 | */ |
||
1007 | return events; |
||
1008 | } |
||
1009 | |||
1010 | void GetStatistics(int events) { |
||
1011 | ushort rawAdcData = 0; |
||
1012 | ushort adcData = 0; |
||
1013 | int channelReceived = 0; |
||
1014 | int channelExpected = 0; |
||
1015 | bool includeSaturated = 0; // cbIncludeSaturated.Checked; |
||
1016 | ushort max[CHANNELS]; |
||
1017 | ushort min[CHANNELS]; |
||
1018 | ushort average[CHANNELS]; |
||
1019 | int total[CHANNELS]; |
||
1020 | for (int i = 0; i < CHANNELS; i++) { |
||
1021 | min[i] = 0xFFFF; |
||
1022 | max[i] = 0; |
||
1023 | total[i] = 0; |
||
1024 | } |
||
1025 | for (int eventNum = 0; eventNum < events; eventNum++) { |
||
1026 | |||
1027 | int sum = 0; |
||
1028 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
1029 | rawAdcData = ExtractAdcData(eventNum, channel); |
||
1030 | if (addChannelNum == true) { |
||
1031 | channelReceived = (rawAdcData >> 12) & 0xF; |
||
1032 | channelExpected = (channel & 0xF); |
||
1033 | if (channelReceived != channelExpected) { |
||
1034 | |||
1035 | AddMessage("ERROR in Event %d : Expected channel %d, received %d -ANALYSIS STOPPED", eventNum, channelExpected, channelReceived); |
||
1036 | |||
1037 | //EnableRegisterScanTimer(true); |
||
1038 | return; |
||
1039 | } |
||
1040 | } |
||
1041 | adcData = (ushort)(rawAdcData & 0x0FFF); // remove channel number |
||
1042 | if ((includeSaturated == true) || ((includeSaturated == false) && (adcData < (ADCMAX - 2)))) |
||
1043 | histograms[channel][adcData]++; |
||
1044 | total[channel] = total[channel] + adcData; |
||
1045 | if (adcData > max[channel]) |
||
1046 | max[channel] = adcData; |
||
1047 | else if (adcData < min[channel]) |
||
1048 | min[channel] = adcData; |
||
1049 | |||
1050 | sum += adcData; |
||
1051 | } |
||
1052 | histogramtotal[sum]++; |
||
1053 | histogramTotalEvents++; |
||
1054 | } |
||
1055 | //tbAdcStatistics.Clear(); |
||
1056 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
1057 | average[channel] = (ushort)(total[channel] / events); |
||
1058 | |||
1059 | AddMessage(" {channel:%d} : {min:0x%d} {max:0x%d} {max-min:%d} {average:%d}\n", channel + 1, min[channel], max[channel], max[channel] - min[channel], average[channel]); |
||
1060 | //if (channel < (CHANNELS - 1)) |
||
1061 | // tbAdcStatistics.AppendText(Environment.NewLine); |
||
1062 | |||
1063 | } |
||
1064 | //SetTextboxToTop(tbAdcStatistics); |
||
1065 | } |
||
1066 | |||
1067 | void SetupAcquisition(int neve) { |
||
1068 | //EnableRegisterScanTimer(false); |
||
1069 | //nudEventSelect.Minimum = 0; |
||
1070 | //nudEventSelect.Maximum = 0; |
||
1071 | int eventsAcquired = Acquire(neve); |
||
1072 | if (eventsAcquired < 1){ |
||
1073 | printf("Acquired events %d\n", eventsAcquired); |
||
1074 | return; |
||
1075 | } |
||
1076 | GetStatistics(eventsAcquired); |
||
1077 | ShowEvent(1); |
||
1078 | //ShowHistograms(); |
||
1079 | //nudEventSelect.Minimum = 1; |
||
1080 | //nudEventSelect.Maximum = eventsAcquired; |
||
1081 | //EnableRegisterScanTimer(true); |
||
1082 | } |
||
1083 | |||
1084 | |||
1085 | |||
1086 | |||
1087 | |||
1088 | int _tmain(int argc, char **argv) { |
||
1089 | |||
1090 | |||
1091 | int events = 1000; |
||
1092 | |||
1093 | InitInterface(events); |
||
1094 | SetupAcquisition(1); |
||
1095 | |||
1096 | //NE_Rainbow(); |
||
1097 | printf("xxx\n"); |
||
1098 | //NE_Call(9); |
||
1099 | int b = 9; |
||
1100 | int k = 0;//NE_Func(b); |
||
1101 | printf("yyy %d %d\n", b, k); |
||
1102 | return 0; |
||
1103 | } |