Rev 33 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
29 | f9daq | 1 | #define USE_DAQ |
2 | #define USE_MIKRO |
||
3 | |||
4 | // Izberi ustrezni interface v meniju projektnega okna |
||
5 | // Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME) |
||
6 | #ifdef USE_DAQ |
||
7 | # define USE_CAMAC |
||
32 | f9daq | 8 | #include "c:\HOME\dino\sipmScan\include\camac.h" |
29 | f9daq | 9 | # ifdef SISVME |
10 | # include "sisvme_dll.h" |
||
11 | # endif |
||
12 | # ifdef WIENVME |
||
13 | # include "wienvme_dll.h" |
||
14 | # endif |
||
15 | # include "CAENV673A.h" |
||
34 | f9daq | 16 | # include "CAENV462.h" |
17 | |||
29 | f9daq | 18 | #endif |
19 | |||
20 | #ifdef USE_MIKRO |
||
21 | # include "MIKRO.h" |
||
22 | #endif |
||
23 | |||
24 | #include <userint.h> |
||
25 | #include <ansi_c.h> |
||
26 | #include <utility.h> |
||
27 | #include <analysis.h> |
||
28 | |||
33 | f9daq | 29 | #include "c:\HOME\dino\sipmScan\include\sipmBias_ui.h" |
29 | f9daq | 30 | |
31 | #ifdef USE_DAQ |
||
32 | # define USE_CAMAC |
||
33 | # include "camac.h" |
||
34 | f9daq | 34 | # define NDAC 1 // CAEN C221 |
35 | # define ASD8 0 // channels |
||
36 | # define AMPDISSH 1 |
||
37 | # define SIPM 2 |
||
29 | f9daq | 38 | # define CAEN_V673A 0x22220000 // IJS |
39 | //#define CAEN_V673A 0x10110000 // FMF |
||
40 | # define CAEN_V462 0x100300 |
||
41 | #endif |
||
42 | |||
43 | #ifdef USE_MIKRO |
||
44 | # define MIKRO_COM 5 |
||
45 | # define MIKRO_X 1 |
||
46 | # define USE_MIKRO_Y |
||
47 | # define MIKRO_Y 2 |
||
48 | # define USE_MIKRO_Z |
||
49 | # define MIKRO_Z 3 |
||
50 | # define STEP_TOLERANCE 50 |
||
51 | #endif |
||
52 | |||
53 | #define MAXCH 512 |
||
54 | #define MAX_THREADS 10 |
||
55 | |||
56 | #define IWAIT 200 |
||
57 | |||
58 | #define NCH 64 |
||
34 | f9daq | 59 | #define POWERSUPPLY 66 // Power supply voltage for SiPM bias |
29 | f9daq | 60 | |
61 | static int p1h, pID, rID, tfID; |
||
62 | static int ph_tdc, ph_adc; |
||
63 | static int dtdc[NCH][2][MAXCH]; |
||
64 | static int something[NCH][2][MAXCH]; |
||
65 | |||
66 | static int daq_on; |
||
67 | static int poolHandle = 0; |
||
68 | static int ntics,dummy; |
||
69 | |||
70 | /************************************************************/ |
||
71 | void wait_loop(unsigned long iloop) |
||
72 | |||
73 | { |
||
74 | int i; |
||
75 | |||
76 | for (i=0;i<iloop;i++); |
||
77 | return; |
||
78 | } |
||
79 | |||
80 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
81 | int eventData1, int eventData2) |
||
82 | { |
||
83 | QueueUserEvent (9000, p1h, P1_TIMER); |
||
84 | return (0); |
||
85 | } |
||
86 | |||
87 | int update_plots (void) |
||
88 | { |
||
89 | int irange, ch; |
||
90 | |||
91 | GetCtrlVal (p1h, P1_PLCH, &ch); |
||
92 | |||
93 | if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW); |
||
94 | GetCtrlVal (p1h, P1_TDCHL, &irange); |
||
95 | ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH, VAL_INTEGER, |
||
96 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); |
||
97 | |||
98 | if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW); |
||
99 | GetCtrlVal (p1h, P1_ADCHL, &irange); |
||
100 | ph_adc = PlotY (p1h, P1_ADC, &something[ch][irange], MAXCH, VAL_INTEGER, |
||
101 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE); |
||
102 | return (0); |
||
103 | } |
||
104 | |||
105 | |||
106 | int module_header(int recid,unsigned long *data,int len){ |
||
107 | data[0] = recid; |
||
108 | data[1] = (len >0)? len : 0 ; |
||
109 | return data[1]+2; |
||
110 | } |
||
111 | |||
112 | int SetDac(int, double); |
||
113 | |||
114 | int CVICALLBACK daq_run(void *functionData) |
||
115 | { |
||
116 | // *** |
||
117 | // variable declarations |
||
118 | int i,j; |
||
119 | int dtype,ch,rg,adc,cres,bsr; |
||
120 | |||
121 | //unsigned short aa[NCH][4]; |
||
122 | unsigned long tdc[NCH]; |
||
123 | unsigned long step_minutes, end_time_s, cur_time_s; |
||
124 | int start_hours, start_minutes, start_seconds; |
||
125 | |||
126 | int status,fmax,fcount,fev; |
||
127 | int popupret; |
||
128 | int mikroX; // check the position |
||
129 | int mikroY; // check the position |
||
130 | |||
131 | char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN]; |
||
132 | int supr0,tdcmin,fseed; |
||
133 | float frac; |
||
134 | double fracg; |
||
135 | unsigned short dum16; |
||
136 | int count; |
||
137 | int print = 0; |
||
138 | int timer_out = 0; |
||
139 | //time_t t,told, tstart; |
||
140 | int ntrig; |
||
141 | int chip, len; |
||
142 | unsigned long *pdata; |
||
143 | |||
144 | FILE *fp; |
||
34 | f9daq | 145 | #define RUNREC_ID 1 |
146 | #define ENDREC_ID 2 |
||
147 | #define POSREC_ID 3 |
||
148 | #define EVTREC_ID 4 |
||
149 | #define THRREC_ID 5 |
||
150 | #define BIASREC_ID 6 |
||
29 | f9daq | 151 | |
152 | typedef struct { |
||
153 | unsigned long id,len; |
||
154 | unsigned long fver,time; |
||
34 | f9daq | 155 | unsigned long thLow, thUp, thStep; |
156 | unsigned long biasLow, biasUp, biasStep; |
||
29 | f9daq | 157 | unsigned long nev,nch,ped,xy; |
158 | long nx,x0,dx,ny,y0,dy; |
||
159 | } RUNREC; |
||
160 | RUNREC runrec; |
||
161 | |||
162 | typedef struct { |
||
163 | unsigned long id,len; |
||
164 | unsigned long time; |
||
165 | } ENDREC; |
||
166 | ENDREC endrec; |
||
167 | |||
168 | typedef struct { |
||
169 | unsigned long id,len; |
||
170 | unsigned long time; |
||
171 | long ix,x,xset,iy,y,yset; |
||
172 | } POSREC; |
||
173 | POSREC posrec; |
||
174 | |||
175 | typedef struct { |
||
176 | unsigned long id,len; |
||
177 | unsigned long nev; |
||
178 | //unsigned short data[NCH]; |
||
179 | } EVTREC; |
||
180 | EVTREC evtrec; |
||
181 | |||
182 | typedef struct { |
||
183 | unsigned long id; |
||
184 | unsigned long len; |
||
185 | unsigned long threshold; |
||
186 | } THRREC; |
||
187 | THRREC thrrec; |
||
34 | f9daq | 188 | |
189 | typedef struct { |
||
190 | unsigned long id; |
||
191 | unsigned long len; |
||
192 | unsigned long bias; |
||
193 | } BIASREC; |
||
194 | BIASREC biasrec; |
||
29 | f9daq | 195 | |
196 | double thresholdLow; |
||
197 | double thresholdUp; |
||
198 | double thresholdStep; |
||
34 | f9daq | 199 | double biasLow; |
200 | double biasUp; |
||
201 | double biasStep; |
||
29 | f9daq | 202 | |
203 | #define BSIZE 20000 |
||
204 | |||
205 | int maxn = BSIZE-4; // 2 words x 2 headers |
||
206 | int tdcdata, edge_type, overflow, tdc_num, channel, ev_dat, last_dat, nval_dat; |
||
207 | // end of declarations |
||
208 | // *** |
||
209 | |||
210 | runrec.id = RUNREC_ID; |
||
211 | runrec.len = sizeof(runrec); |
||
212 | //runrec.fver = 0x10000; |
||
213 | runrec.nch = NCH; |
||
214 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1; |
||
215 | GetCtrlVal(p1h, P1_DEBUG, &print); |
||
216 | endrec.id = ENDREC_ID; |
||
217 | endrec.len = sizeof(endrec); |
||
218 | posrec.id = POSREC_ID; |
||
219 | posrec.len = sizeof(posrec); |
||
220 | evtrec.id = EVTREC_ID; |
||
221 | evtrec.len = sizeof(evtrec); |
||
222 | thrrec.id = THRREC_ID; |
||
223 | thrrec.len = sizeof(thrrec); |
||
34 | f9daq | 224 | biasrec.id = BIASREC_ID; |
225 | biasrec.len = sizeof(biasrec); |
||
29 | f9daq | 226 | |
227 | |||
228 | cres = 0; |
||
229 | |||
230 | GetCtrlVal (p1h, P1_NEVE, &runrec.nev); |
||
231 | GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped); |
||
232 | |||
233 | GetCtrlVal (p1h, P1_NX, &runrec.nx); |
||
234 | GetCtrlVal (p1h, P1_XSTEP, &runrec.dx); |
||
235 | GetCtrlVal (p1h, P1_XMIN, &runrec.x0); |
||
236 | GetCtrlVal (p1h, P1_NY, &runrec.ny); |
||
237 | GetCtrlVal (p1h, P1_YSTEP, &runrec.dy); |
||
238 | GetCtrlVal (p1h, P1_YMIN, &runrec.y0); |
||
239 | |||
240 | GetCtrlVal (p1h, P1_NMIN, &step_minutes); |
||
241 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
242 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
243 | time(&cur_time_s); |
||
244 | end_time_s = cur_time_s + step_minutes*60; |
||
245 | printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, cur_time_s, end_time_s); |
||
246 | |||
247 | //GetCtrlVal (p1h, P1_DSAVE, &dsave); |
||
248 | //if (dsave) { |
||
249 | GetCtrlVal (p1h, P1_DFILE, dfile0); |
||
250 | |||
251 | fev=0; |
||
252 | fcount=1; |
||
253 | GetCtrlVal (p1h, P1_NEWF, &fmax); |
||
254 | fmax*=1000000;//fmax in Mega Bytes |
||
255 | //} |
||
256 | GetCtrlVal (p1h, P1_SUPR, &supr0); |
||
257 | if (supr0) { |
||
258 | GetCtrlVal (p1h, P1_TDCMIN, &tdcmin); |
||
259 | GetCtrlVal (p1h, P1_FRAC, &frac); |
||
260 | } |
||
261 | |||
34 | f9daq | 262 | GetCtrlVal (p1h, P1_SASD8THR, &thresholdLow); |
29 | f9daq | 263 | runrec.thLow = thresholdLow*1000; // mV |
264 | GetCtrlVal (p1h, P1_STHRUP, &thresholdUp); |
||
265 | runrec.thUp = thresholdUp*1000; // mV |
||
266 | GetCtrlVal (p1h, P1_STEP, &thresholdStep); |
||
267 | runrec.thStep = thresholdStep*1000; // mV |
||
268 | |||
34 | f9daq | 269 | GetCtrlVal (p1h, P1_SBIAS, &biasLow); |
270 | GetCtrlVal (p1h, P1_SBIASUP, &biasUp); |
||
271 | GetCtrlVal (p1h, P1_SBIASSTEP, &biasStep); |
||
272 | runrec.biasLow = biasLow*1000; |
||
273 | runrec.biasUp = biasUp*1000; |
||
274 | runrec.biasStep = biasStep*1000; |
||
29 | f9daq | 275 | |
276 | #ifdef USE_DAQ |
||
277 | V673A_map(0,CAEN_V673A,0); |
||
278 | V673A_init(0); |
||
279 | |||
280 | V462_map(0,CAEN_V462,0); |
||
281 | V462_set0(0,1); |
||
282 | #endif |
||
283 | |||
284 | //if (dsave) { |
||
285 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
286 | |||
287 | fp = fopen (dfile, "wb"); |
||
288 | time (&runrec.time); |
||
289 | status = fwrite (&runrec, 1, runrec.len, fp); |
||
290 | //} |
||
291 | if (supr0) { |
||
292 | fseed = runrec.time & 0x7fffffff; |
||
293 | Uniform (1, fseed, &fracg); |
||
294 | } |
||
34 | f9daq | 295 | |
296 | // Set SiPM bias voltage |
||
297 | //SetDac(SIPM, 1.0); |
||
298 | biasrec.bias = runrec.biasLow; |
||
299 | while (biasrec.bias <= runrec.biasUp) { |
||
300 | SetDac(SIPM, biasrec.bias/1000.0 - POWERSUPPLY); |
||
301 | SetCtrlVal(p1h, P1_SBIAS, biasrec.bias/1000.0); |
||
302 | status = fwrite(&biasrec, 1, biasrec.len, fp); |
||
303 | if (print) printf("BIASREC status %d len %d bias %d\n", status, biasrec.len, biasrec.bias); |
||
304 | |||
305 | // Set ASD threshold voltage |
||
29 | f9daq | 306 | thrrec.threshold = (unsigned long) ceil(thresholdLow*1000); // threshold in mV |
307 | while (thrrec.threshold <= thresholdUp*1000) { |
||
34 | f9daq | 308 | SetDac(ASD8, thrrec.threshold/1000.0); |
309 | SetCtrlVal(p1h, P1_SASD8THR, thrrec.threshold/1000.0); |
||
29 | f9daq | 310 | status = fwrite(&thrrec, 1, thrrec.len, fp); |
311 | if (print) printf("THRREC status %d len %d threshold %d\n", status, thrrec.len, thrrec.threshold); |
||
312 | |||
313 | for (posrec.ix=0; posrec.ix<runrec.nx; posrec.ix++) { |
||
314 | posrec.xset = runrec.x0 + posrec.ix*runrec.dx; |
||
315 | #ifdef USE_MIKRO |
||
316 | //printf("MIKRO_MoveTo (1, x);%d\n",posrec.x); |
||
317 | do { |
||
318 | MIKRO_MoveTo (MIKRO_X, posrec.xset); |
||
319 | MIKRO_GetPosition(MIKRO_X, &mikroX); |
||
320 | } while (abs(posrec.xset - mikroX) > STEP_TOLERANCE); |
||
321 | //printf("->MIKRO_MoveTo (1, x);%d\n",posrec.xset); |
||
322 | #endif |
||
323 | //SetCtrlVal (p1h, P1_X, posrec.xset); |
||
324 | posrec.xset = mikroX; // set the true value |
||
325 | SetCtrlVal(p1h, P1_X, mikroX); |
||
326 | SetCtrlVal (p1h, P1_IX, posrec.ix); |
||
327 | for (posrec.iy=0; posrec.iy<runrec.ny; posrec.iy++) { |
||
328 | posrec.yset = runrec.y0 + posrec.iy*runrec.dy; |
||
329 | #ifdef USE_MIKRO_Y |
||
330 | //printf("MIKRO_MoveTo (2, y);%d\n",y); |
||
331 | do { |
||
332 | MIKRO_MoveTo (MIKRO_Y, posrec.yset); |
||
333 | MIKRO_GetPosition(MIKRO_Y, &mikroY); |
||
334 | } while (abs(posrec.yset - mikroY) > STEP_TOLERANCE); |
||
335 | //printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset); |
||
336 | #endif |
||
337 | //SetCtrlVal (p1h, P1_Y, posrec.yset); |
||
338 | posrec.yset = mikroY; // set the true value |
||
339 | SetCtrlVal (p1h, P1_Y, mikroY); |
||
340 | SetCtrlVal (p1h, P1_IY, posrec.iy); |
||
341 | //if (dsave) { |
||
342 | if (fmax && (ftell(fp) > fmax)) { |
||
343 | fcount+=1; |
||
344 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
345 | fclose(fp); |
||
346 | fp = fopen (dfile, "wb"); |
||
347 | } |
||
348 | |||
349 | time (&posrec.time); |
||
350 | status = fwrite (&posrec, 1, posrec.len, fp); |
||
351 | if (print) printf("POSREC status %d len %d\n", status, posrec.len); |
||
352 | //} |
||
353 | |||
354 | // clear the plots |
||
355 | for (j=0;j<NCH;j++) { |
||
356 | for (i=0;i<MAXCH;i++){ |
||
357 | dtdc[j][0][i]=0; |
||
358 | dtdc[j][1][i]=0; |
||
359 | } |
||
360 | } |
||
361 | |||
362 | evtrec.nev=1; |
||
363 | //time(&t); |
||
364 | //tstart=t; |
||
365 | if (print) printf("RUN START.\n"); |
||
366 | |||
367 | do { |
||
368 | unsigned long word[BSIZE]; |
||
369 | count=0; |
||
370 | ntics=0; |
||
371 | if (print) printf("Event counter: %d\n", evtrec.nev); |
||
372 | |||
373 | //if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t)); |
||
374 | |||
375 | #ifdef USE_DAQ |
||
376 | #ifdef CAEN_V673A |
||
377 | |||
378 | |||
379 | V462_start0(0); |
||
380 | //tmlnk (tout); |
||
381 | do { |
||
382 | ntrig = V673A_ntrig(0); |
||
383 | } while (ntrig==0 &&(ntics<2)); |
||
384 | //tmulk(); |
||
385 | //if (timer_out) { |
||
386 | // fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
387 | // V673A_clallb(0); |
||
388 | // continue; |
||
389 | //} |
||
390 | |||
391 | if (ntics>=2) { |
||
392 | fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
393 | printf("Timer tics %d\n", ntics); |
||
394 | ntics = 0; |
||
395 | evtrec.nev--; |
||
396 | V673A_clallb(0); |
||
397 | continue; |
||
398 | } |
||
399 | |||
400 | if (ntrig>0){ |
||
401 | // chip readout |
||
402 | for (chip=0;chip<2;chip++){ |
||
403 | if (chip) len = V673A_read1 (0, &word[count+2],maxn-count-2); |
||
404 | else len = V673A_read0 (0, &word[count+2],maxn-count-2); |
||
405 | |||
406 | if (len>16*32*4){ |
||
407 | if (print) printf("Length > 2048 per chip!\n"); |
||
408 | chip=2; |
||
409 | V673A_clallb(0); |
||
410 | evtrec.nev--; |
||
411 | continue; |
||
412 | } |
||
413 | pdata=&word[count+2]; |
||
414 | for(i=0;i< len;i++){ |
||
415 | tdcdata = pdata[i] & 0xFFFF ; |
||
416 | edge_type = (pdata[i]>>16)&0x1 ; |
||
417 | overflow = (pdata[i]>>17)&0x1 ; |
||
418 | tdc_num = (pdata[i]>>25)&0x1 ; |
||
419 | channel = ((pdata[i]>>18)&0x1F) |tdc_num<<5 ; |
||
420 | ev_dat = (pdata[i]>>23)&0x1 ; |
||
421 | last_dat = (pdata[i]>>30)&0x1 ; |
||
422 | nval_dat = (pdata[i]>>31)&0x1 ; |
||
423 | if (ev_dat) { |
||
424 | if(print) printf("Event %d\n",tdcdata); |
||
425 | } else { |
||
426 | dtdc[channel][edge_type][tdcdata]++; |
||
427 | |||
428 | if (print) printf |
||
429 | ("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",channel, edge_type,ev_dat,tdcdata,last_dat,nval_dat); |
||
430 | } |
||
431 | } |
||
432 | if (count+2 < maxn) count+=module_header(0x140+chip,&word[count],len); |
||
433 | } |
||
434 | if (print) printf("Received triggers: %d \n", ntrig); |
||
435 | } else { |
||
436 | if (ntrig==0) { |
||
437 | fprintf(stderr,"-----------------> no trigger"); |
||
438 | break; |
||
439 | } else { |
||
440 | if (print) fprintf(stderr,"-----------------> wrong number of triggers in chips V673a !!!\n"); |
||
441 | } |
||
442 | V673A_clallb(0); |
||
443 | evtrec.nev--; |
||
444 | continue; |
||
445 | } |
||
446 | |||
447 | V673A_clallb(0); |
||
448 | #endif |
||
449 | #endif |
||
450 | |||
451 | |||
452 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
||
453 | evtrec.len = count*sizeof(unsigned long) + sizeof(evtrec); |
||
454 | if (print) printf("EVTREC.len = %d bytes.\n", evtrec.len); |
||
455 | |||
456 | status = fwrite (&evtrec, 1, sizeof(evtrec), fp); |
||
457 | status = fwrite (&word, 1, count*sizeof(unsigned long), fp); |
||
458 | |||
459 | |||
460 | if (!(evtrec.nev%1000)) SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
461 | |||
462 | if (!((evtrec.nev+1)%1000)) { |
||
463 | if (fmax && (ftell(fp) > fmax)) { |
||
464 | time (&endrec.time); |
||
465 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
466 | fcount+=1; |
||
467 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
468 | fclose(fp); |
||
469 | fp = fopen (dfile, "wb"); |
||
470 | } |
||
471 | } |
||
472 | |||
473 | if(step_minutes > 0) { |
||
474 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
475 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
476 | time(&cur_time_s); |
||
477 | if(cur_time_s >= end_time_s) { |
||
478 | end_time_s = cur_time_s + step_minutes*60; |
||
479 | printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours, start_minutes, start_seconds, posrec.xset); |
||
480 | break; |
||
481 | } |
||
482 | } |
||
483 | //told=t; |
||
484 | |||
485 | GetCtrlVal(p1h, P1_DAQ, &daq_on); |
||
486 | |||
487 | } while (evtrec.nev++<runrec.nev && daq_on); |
||
488 | if (!daq_on) break; |
||
489 | } // x loop |
||
490 | if (!daq_on) break; |
||
491 | } // y loop |
||
492 | if (!daq_on) break; |
||
493 | thrrec.threshold += thresholdStep*1000; |
||
494 | } // threhsold loop |
||
34 | f9daq | 495 | if (!daq_on) break; |
496 | biasrec.bias += runrec.biasStep; |
||
497 | } // bias loop |
||
29 | f9daq | 498 | |
34 | f9daq | 499 | SetDac(SIPM, 0); // safety |
500 | |||
29 | f9daq | 501 | //if (dsave) { |
502 | time (&endrec.time); |
||
503 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
504 | if (print) printf("ENDREC status %d len %d\n", status, endrec.len); |
||
505 | fclose(fp); |
||
506 | //} |
||
507 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
508 | printf("STOP:%2d-%2d-%2d (start_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, runrec.time, endrec.time); |
||
509 | printf("Elapsed time: %u s.\n", endrec.time-runrec.time); |
||
510 | |||
511 | daq_on=0; |
||
512 | SetCtrlVal (p1h, P1_DAQ, daq_on); |
||
513 | SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
514 | if (print) printf("RUN END. \n\n"); |
||
515 | |||
516 | return 0; |
||
517 | } |
||
518 | |||
519 | |||
520 | int SetDac(int ch, double val){ |
||
521 | |||
522 | const unsigned int maxdac=0xFFF; |
||
523 | const double RANGE = +9.9976; //V |
||
524 | unsigned int dac; |
||
525 | |||
526 | if ((val > RANGE) || (val < 0)) { |
||
527 | printf("DAC value OUT OF RANGE!!!\n"); |
||
528 | return(-1); |
||
529 | } |
||
530 | |||
531 | dac = (val/RANGE)*maxdac; |
||
532 | CSSA_W(NDAC,ch,16,&dac); |
||
533 | printf("DAC ch %d set to %f V dac=%d\n", ch, val, dac); |
||
534 | return 0; |
||
535 | } |
||
536 | |||
537 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
538 | LPSTR lpszCmdLine, int nCmdShow) |
||
539 | { |
||
540 | int i,j,status,refon; |
||
541 | long int xpos, ypos, zpos; |
||
542 | char dfile[300]; |
||
543 | FILE *fp; |
||
544 | |||
545 | typedef struct { |
||
546 | unsigned long id,len; |
||
547 | unsigned long fver,time; |
||
548 | unsigned long nev,nch,ped,xy; |
||
549 | long nx,x0,dx,ny,y0,dy; |
||
550 | } RUNREC; |
||
551 | RUNREC runrec; |
||
552 | |||
553 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
554 | return -1; /* out of memory */ |
||
555 | |||
556 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
557 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
558 | |||
559 | SetStdioWindowOptions (1000, 0, 0); |
||
560 | SetStdioWindowSize (200, 560); |
||
561 | SetStdioWindowPosition (825, 250); |
||
562 | |||
563 | #ifdef USE_DAQ |
||
564 | VME_START(NULL); |
||
565 | #endif |
||
566 | |||
567 | #ifdef USE_MIKRO |
||
568 | MIKRO_Open (MIKRO_COM); |
||
569 | MIKRO_Init (MIKRO_X,0); |
||
570 | #ifdef USE_MIKRO_Y |
||
571 | MIKRO_Init (MIKRO_Y,0); |
||
572 | #endif |
||
573 | #ifdef USE_MIKRO_Z |
||
574 | MIKRO_Init (MIKRO_Z,0); |
||
575 | #endif |
||
576 | #endif |
||
577 | |||
33 | f9daq | 578 | if ((p1h = LoadPanel (0, "include/sipmBias_ui.uir", P1)) < 0) return -1; |
29 | f9daq | 579 | DisplayPanel (p1h); |
580 | SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1); |
||
581 | |||
582 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
||
583 | SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2); |
||
584 | |||
585 | QueueUserEvent (1000, p1h, P1_RESET); |
||
586 | //QueueUserEvent (1000, p1h, P1_ASD8THR); |
||
587 | QueueUserEvent (1000, p1h, P1_AMPDISSHTHR); |
||
34 | f9daq | 588 | QueueUserEvent (1000, p1h, P1_BIAS); |
29 | f9daq | 589 | do { |
590 | GetUserEvent (1, &pID, &rID); |
||
591 | switch (rID) { |
||
592 | case P1_TIMER: |
||
593 | ntics+=1; |
||
594 | GetCtrlVal (p1h, P1_REFON, &refon); |
||
595 | if (refon) update_plots(); |
||
596 | break; |
||
597 | case P1_REFRESH: |
||
598 | update_plots(); |
||
599 | break; |
||
600 | case P1_DAQ: |
||
601 | GetCtrlVal (p1h, P1_DAQ, &daq_on); |
||
602 | if (daq_on) { |
||
603 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
604 | } else { |
||
605 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
606 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
607 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
608 | } |
||
609 | break; |
||
610 | |||
611 | case P1_ZSET: |
||
612 | if (!daq_on) { |
||
613 | GetCtrlVal (p1h, P1_ZSET, &zpos); |
||
614 | #ifdef USE_MIKRO_Z |
||
615 | MIKRO_MoveTo (MIKRO_Z, zpos); |
||
616 | #endif |
||
617 | } |
||
618 | break; |
||
619 | case P1_REREAD: |
||
620 | if (!daq_on) { |
||
621 | status = FileSelectPopup ("", "*.dat", ".dat", |
||
622 | "Choose the previous file", |
||
623 | VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile); |
||
624 | if (status==1) { |
||
625 | fp = fopen (dfile, "rb"); |
||
626 | status = fread (&runrec, 1, sizeof(runrec), fp); |
||
627 | fclose(fp); |
||
628 | if (runrec.id==1) { |
||
629 | SetCtrlVal (p1h, P1_NX, runrec.nx); |
||
630 | SetCtrlVal (p1h, P1_XSTEP, runrec.dx); |
||
631 | SetCtrlVal (p1h, P1_XMIN, runrec.x0); |
||
632 | SetCtrlVal (p1h, P1_NY, runrec.ny); |
||
633 | SetCtrlVal (p1h, P1_YSTEP, runrec.dy); |
||
634 | SetCtrlVal (p1h, P1_YMIN, runrec.y0); |
||
635 | SetCtrlVal (p1h, P1_NEVE, runrec.nev); |
||
636 | } |
||
637 | } |
||
638 | } |
||
639 | break; |
||
640 | case P1_MGET: |
||
641 | #ifdef USE_MIKRO |
||
642 | MIKRO_GetPosition(MIKRO_X,&xpos); |
||
643 | Delay(0.01); |
||
644 | SetCtrlVal (p1h, P1_X, xpos); |
||
645 | #ifdef USE_MIKRO_Y |
||
646 | MIKRO_GetPosition(MIKRO_Y,&ypos); |
||
647 | Delay(0.01); |
||
648 | SetCtrlVal (p1h, P1_Y, ypos); |
||
649 | #endif |
||
650 | #ifdef USE_MIKRO_Z |
||
651 | MIKRO_GetPosition(MIKRO_Z,&zpos); |
||
652 | Delay(0.01); |
||
653 | SetCtrlVal (p1h, P1_Z, zpos); |
||
654 | #endif |
||
655 | #endif |
||
656 | break; |
||
657 | case P1_HO: |
||
658 | if (!daq_on) { |
||
659 | SetWaitCursor (1); |
||
660 | #ifdef USE_MIKRO |
||
661 | MIKRO_ReferenceMove (MIKRO_X); |
||
662 | #ifdef USE_MIKRO_Y |
||
663 | MIKRO_ReferenceMove (MIKRO_Y); |
||
664 | #endif |
||
665 | #ifdef USE_MIKRO_Z |
||
666 | MIKRO_ReferenceMove (MIKRO_Z); |
||
667 | #endif |
||
668 | #endif |
||
669 | SetWaitCursor (0); |
||
670 | } |
||
671 | break; |
||
672 | case P1_RESET: |
||
673 | for (j=0;j<NCH;j++) { |
||
674 | for (i=0;i<MAXCH;i++){ |
||
675 | dtdc[j][0][i]=0; |
||
676 | dtdc[j][1][i]=0; |
||
677 | |||
678 | } |
||
679 | } |
||
680 | update_plots(); |
||
681 | break; |
||
682 | case P1_TDCLOG: |
||
683 | GetCtrlVal (p1h, P1_TDCLOG, &status); |
||
684 | SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status); |
||
685 | update_plots(); |
||
686 | break; |
||
687 | case P1_ADCLOG: |
||
688 | GetCtrlVal (p1h, P1_ADCLOG, &status); |
||
689 | SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status); |
||
690 | update_plots(); |
||
691 | break; |
||
34 | f9daq | 692 | case P1_ASD8THR: |
29 | f9daq | 693 | { |
694 | double value; |
||
695 | GetCtrlVal (p1h, P1_SASD8THR, &value); |
||
34 | f9daq | 696 | SetDac(ASD8, value); |
29 | f9daq | 697 | |
698 | } |
||
34 | f9daq | 699 | break; |
700 | case P1_BIAS: |
||
701 | { |
||
702 | double value; |
||
703 | GetCtrlVal (p1h, P1_SBIAS, &value); |
||
704 | value -= POWERSUPPLY; |
||
705 | SetDac(SIPM, value); |
||
706 | } |
||
707 | break; |
||
29 | f9daq | 708 | case P1_AMPDISSHTHR: |
709 | { |
||
710 | double value; |
||
711 | GetCtrlVal (p1h, P1_SAMPDISSHTHR, &value); |
||
34 | f9daq | 712 | SetDac(AMPDISSH, value); |
29 | f9daq | 713 | |
714 | } |
||
715 | break; |
||
716 | |||
717 | } |
||
718 | |||
719 | } while ((rID != P1_EXIT)||daq_on); |
||
720 | |||
721 | CmtDiscardThreadPool (poolHandle); |
||
722 | DiscardPanel (p1h); |
||
723 | |||
724 | #ifdef USE_MIKRO |
||
725 | MIKRO_Close (); |
||
726 | #endif |
||
727 | |||
728 | #ifdef USE_DAQ |
||
729 | VME_STOP(); |
||
730 | #endif |
||
731 | return 0; |
||
732 | |||
733 | } |