Rev 1967 Rev 1980
1   1  
2 //struct { 2 //struct {
3 unsigned int8 firenum=TDC_FIRENUM_0; 3 unsigned int8 firenum=TDC_FIRENUM_0;
4 unsigned int8 div_fire=TDC_DIV_FIRE_2; 4 unsigned int8 div_fire=TDC_DIV_FIRE_2;
5 unsigned int8 calresnum=TDC_CALPERIODS_2; 5 unsigned int8 calresnum=TDC_CALPERIODS_2;
6 unsigned int8 clkhsdiv=TDC_CLKHSDIV_1; 6 unsigned int8 clkhsdiv=TDC_CLKHSDIV_1;
7 unsigned int8 start_clkhs=TDC_CLKHS_ON; 7 unsigned int8 start_clkhs=TDC_CLKHS_ON;
8 unsigned int1 portnum=TDC_TPORTNUM_4; 8 unsigned int1 portnum=TDC_TPORTNUM_4;
9 unsigned int1 Tcycle=TDC_TCYCLE_SHORT; 9 unsigned int1 Tcycle=TDC_TCYCLE_SHORT;
10 unsigned int1 fakenum=TDC_TFAKENUM_2; 10 unsigned int1 fakenum=TDC_TFAKENUM_2;
11 unsigned int1 selclkT=TDC_TSELCLK_128HS; 11 unsigned int1 selclkT=TDC_TSELCLK_128HS;
12 unsigned int1 calibrate=TDC_CALIBRATE_EN; 12 unsigned int1 calibrate=TDC_CALIBRATE_EN;
13 unsigned int1 disautocal=TDC_AUTOCAL_EN; 13 unsigned int1 disautocal=TDC_AUTOCAL_EN;
14 unsigned int1 MRange=TDC_MRANGE2; 14 unsigned int1 MRange=TDC_MRANGE2;
15 unsigned int1 neg_stop2=TDC_NEG_STOP2; 15 unsigned int1 neg_stop2=TDC_NEG_STOP2;
16 unsigned int1 neg_stop1=TDC_NEG_STOP1; 16 unsigned int1 neg_stop1=TDC_NEG_STOP1;
17 unsigned int1 neg_start=TDC_NEG_START; 17 unsigned int1 neg_start=TDC_NEG_START;
18 //}reg0; 18 //}reg0;
19   19  
20 //struct { 20 //struct {
21 unsigned int hit2=TDC_MRANGE1_HIT2_NOAC; 21 unsigned int hit2=TDC_MRANGE1_HIT2_NOAC;
22 unsigned int hit1=TDC_MRANGE1_HIT1_NOAC; 22 unsigned int hit1=TDC_MRANGE1_HIT1_NOAC;
23 unsigned int1 fast_init=TDC_FAST_INIT_DIS; 23 unsigned int1 fast_init=TDC_FAST_INIT_DIS;
24 unsigned int hitin2=TDC_HITIN2_0; 24 unsigned int hitin2=TDC_HITIN2_0;
25 unsigned int hitin1=TDC_HITIN1_0; 25 unsigned int hitin1=TDC_HITIN1_0;
26 //}reg1; 26 //}reg1;
27   27  
28 //struct { 28 //struct {
29 unsigned int en_int=TDC_INT_ALU; 29 unsigned int en_int=TDC_INT_ALU;
30 unsigned int1 rfedge2=TDC_CH2EDGE_RIS; 30 unsigned int1 rfedge2=TDC_CH2EDGE_RIS;
31 unsigned int1 rfedge1=TDC_CH1EDGE_RIS; 31 unsigned int1 rfedge1=TDC_CH1EDGE_RIS;
32 unsigned int32 delval1=0; 32 unsigned int32 delval1=0;
33 //}reg2; 33 //}reg2;
34   34  
35 //struct { 35 //struct {
36 unsigned int1 en_err_val=TDC_ERRVAL_DIS; 36 unsigned int1 en_err_val=TDC_ERRVAL_DIS;
37 unsigned int tim0_mr2=TDC_TIM0MR2_16384CLKHS; 37 unsigned int tim0_mr2=TDC_TIM0MR2_16384CLKHS;
38 unsigned int32 delval2=0; 38 unsigned int32 delval2=0;
39 //}reg3; 39 //}reg3;
40   40  
41 //reg4 41 //reg4
42 unsigned int32 delval3=0; 42 unsigned int32 delval3=0;
43   43  
44 //reg5 44 //reg5
45 unsigned int conf_fire=0; 45 unsigned int conf_fire=0;
46 unsigned int1 en_startnoise=TDC_STARTNOISE_DIS; 46 unsigned int1 en_startnoise=TDC_STARTNOISE_DIS;
47 unsigned int1 dis_phasenoise=TDC_PHASENOISE_DIS; 47 unsigned int1 dis_phasenoise=TDC_PHASENOISE_DIS;
48 unsigned int repeat_fire=TDC_REPEAT_FIRE_0; 48 unsigned int repeat_fire=TDC_REPEAT_FIRE_0;
49 unsigned int16 phase_fire=0; 49 unsigned int16 phase_fire=0;
50   50  
51 //}TDC_registers; 51 //}TDC_registers;
52   52  
53   53  
-   54 /*
-   55 1 0 0 0 0 ADR2 ADR1 ADR0 Write into address ADR
-   56 1 0 1 1 0 ADR2 ADR1 ADR0 Read from address ADR
-   57 0 1 1 1 0 0 0 0 Init
-   58 0 1 0 1 0 0 0 0 Power On Reset
-   59 0 0 0 0 0 0 0 1 Start_Cycle
-   60 0 0 0 0 0 0 1 0 Start_Temp
-   61 0 0 0 0 0 0 1 1 Start_Cal_Resonator
-   62 0 0 0 0 0 1 0 0 Start_Cal_TDC
-   63 */
-   64  
54 void TDC_init() 65 void TDC_init()
55 { 66 {
56 output_low(TDC_ENABLE); 67 output_low(TDC_ENABLE);
57 spi_xfer(TDC_stream,0x70,8); 68 spi_xfer(TDC_stream,0x70,8);
58 output_high(TDC_ENABLE); 69 output_high(TDC_ENABLE);
59 } 70 }
60   71  
61 void TDC_reset() 72 void TDC_reset()
62 { 73 {
63 output_low(TDC_ENABLE); 74 output_low(TDC_ENABLE);
64 spi_xfer(TDC_stream,0x50,8); 75 spi_xfer(TDC_stream,0x50,8);
65 output_high(TDC_ENABLE); 76 output_high(TDC_ENABLE);
66 } 77 }
67   78  
68 void TDC_start_cycle() 79 void TDC_start_cycle()
69 { 80 {
70 output_low(TDC_ENABLE); 81 output_low(TDC_ENABLE);
71 spi_xfer(TDC_stream,0x01,8); 82 spi_xfer(TDC_stream,0x01,8);
72 output_high(TDC_ENABLE); 83 output_high(TDC_ENABLE);
73 } 84 }
74   85  
75 void TDC_start_temp() 86 void TDC_start_temp()
76 { 87 {
77 output_low(TDC_ENABLE); 88 output_low(TDC_ENABLE);
78 spi_xfer(TDC_stream,0x02,8); 89 spi_xfer(TDC_stream,0x02,8);
79 output_high(TDC_ENABLE); 90 output_high(TDC_ENABLE);
80 } 91 }
81   92  
82 void TDC_start_cal_resonator() 93 void TDC_start_cal_resonator()
83 { 94 {
84 output_low(TDC_ENABLE); 95 output_low(TDC_ENABLE);
85 spi_xfer(TDC_stream,0x03,8); 96 spi_xfer(TDC_stream,0x03,8);
86 output_high(TDC_ENABLE); 97 output_high(TDC_ENABLE);
87 } 98 }
88   99  
89 void TDC_start_cal() 100 void TDC_start_cal()
90 { 101 {
91 output_low(TDC_ENABLE); 102 output_low(TDC_ENABLE);
92 spi_xfer(TDC_stream,0x04,8); 103 spi_xfer(TDC_stream,0x04,8);
93 output_high(TDC_ENABLE); 104 output_high(TDC_ENABLE);
94 } 105 }
95   106  
96 unsigned int32 TDC_get_measurement(int num) 107 unsigned int32 TDC_get_measurement(int num)
97 { 108 {
98 unsigned int32 ret; 109 unsigned int32 ret;
99   110  
100 output_low(TDC_ENABLE); 111 output_low(TDC_ENABLE);
101 spi_xfer(TDC_stream,0xB0 + num - 1, 8); 112 spi_xfer(TDC_stream,0xB0 + num - 1, 8);
102 ret=spi_xfer(TDC_stream,0,32); 113 ret=spi_xfer(TDC_stream,0,32);
103 output_high(TDC_ENABLE); 114 output_high(TDC_ENABLE);
104 return ret; 115 return ret;
105 } 116 }
106   117  
107 unsigned int16 TDC_get_status() 118 unsigned int16 TDC_get_status()
108 { 119 {
109 unsigned int16 ret; 120 unsigned int16 ret;
110   121  
111 output_low(TDC_ENABLE); 122 output_low(TDC_ENABLE);
112 spi_xfer(TDC_stream,0xB4,8); 123 spi_xfer(TDC_stream,0xB4,8);
113 ret=spi_xfer(TDC_stream,0,16); 124 ret=spi_xfer(TDC_stream,0,16);
114 output_high(TDC_ENABLE); 125 output_high(TDC_ENABLE);
115 return ret; 126 return ret;
116 } 127 }
117   128  
118 unsigned int8 TDC_get_reg1() 129 unsigned int8 TDC_get_reg1()
119 { 130 {
120 unsigned int8 ret; 131 unsigned int8 ret;
121   132  
122 output_low(TDC_ENABLE); 133 output_low(TDC_ENABLE);
123 spi_xfer(TDC_stream,0xB5,8); 134 spi_xfer(TDC_stream,0xB5,8);
124 ret=spi_xfer(TDC_stream,0,8); 135 ret=spi_xfer(TDC_stream,0,8);
125 output_high(TDC_ENABLE); 136 output_high(TDC_ENABLE);
126 return ret; 137 return ret;
127 } 138 }
128   139  
129 void TDC_update_reg1() // updates reg1 only 140 void TDC_update_reg1() // updates reg1 only
130 { 141 {
131 output_low(TDC_ENABLE); 142 output_low(TDC_ENABLE);
132 spi_xfer(TDC_stream,0x81,8); 143 spi_xfer(TDC_stream,0x81,8);
133 spi_xfer(TDC_stream,hit2,4); 144 spi_xfer(TDC_stream,hit2,4);
134 spi_xfer(TDC_stream,hit1,4); 145 spi_xfer(TDC_stream,hit1,4);
135 spi_xfer(TDC_stream,fast_init,1); 146 spi_xfer(TDC_stream,fast_init,1);
136 spi_xfer(TDC_stream,1,1); 147 spi_xfer(TDC_stream,1,1);
137 spi_xfer(TDC_stream,hitin2,3); 148 spi_xfer(TDC_stream,hitin2,3);
138 spi_xfer(TDC_stream,hitin1,3); 149 spi_xfer(TDC_stream,hitin1,3);
139 spi_xfer(TDC_stream,0,8); 150 spi_xfer(TDC_stream,0,8);
140 output_high(TDC_ENABLE); 151 output_high(TDC_ENABLE);
141 } 152 }
142   153  
143 void TDC_update_registers() 154 void TDC_update_registers()
144 { 155 {
145 //update reg0 156 //update reg0
146 output_low(TDC_ENABLE); 157 output_low(TDC_ENABLE);
147 spi_xfer(TDC_stream,0x80,8); 158 spi_xfer(TDC_stream,0x80,8);
148 spi_xfer(TDC_stream,firenum,4); 159 spi_xfer(TDC_stream,firenum,4);
149 spi_xfer(TDC_stream,div_fire,4); 160 spi_xfer(TDC_stream,div_fire,4);
150 spi_xfer(TDC_stream,calresnum,2); 161 spi_xfer(TDC_stream,calresnum,2);
151 spi_xfer(TDC_stream,clkhsdiv,2); 162 spi_xfer(TDC_stream,clkhsdiv,2);
152 spi_xfer(TDC_stream,start_clkhs,2); 163 spi_xfer(TDC_stream,start_clkhs,2);
153 spi_xfer(TDC_stream,portnum,1); 164 spi_xfer(TDC_stream,portnum,1);
154 spi_xfer(TDC_stream,Tcycle,1); 165 spi_xfer(TDC_stream,Tcycle,1);
155 spi_xfer(TDC_stream,fakenum,1); 166 spi_xfer(TDC_stream,fakenum,1);
156 spi_xfer(TDC_stream,selclkT,1); 167 spi_xfer(TDC_stream,selclkT,1);
157 spi_xfer(TDC_stream,calibrate,1); 168 spi_xfer(TDC_stream,calibrate,1);
158 spi_xfer(TDC_stream,disautocal,1); 169 spi_xfer(TDC_stream,disautocal,1);
159 spi_xfer(TDC_stream,MRange,1); 170 spi_xfer(TDC_stream,MRange,1);
160 spi_xfer(TDC_stream,neg_stop2,1); 171 spi_xfer(TDC_stream,neg_stop2,1);
161 spi_xfer(TDC_stream,neg_stop1,1); 172 spi_xfer(TDC_stream,neg_stop1,1);
162 spi_xfer(TDC_stream,neg_start,1); 173 spi_xfer(TDC_stream,neg_start,1);
163 output_high(TDC_ENABLE); 174 output_high(TDC_ENABLE);
164   175  
165 TDC_update_reg1(); // update reg1 176 TDC_update_reg1(); // update reg1
166   177  
167 // update reg2 178 // update reg2
168 output_low(TDC_ENABLE); 179 output_low(TDC_ENABLE);
169 spi_xfer(TDC_stream,0x82); 180 spi_xfer(TDC_stream,0x82);
170 spi_xfer(TDC_stream,en_int,3); 181 spi_xfer(TDC_stream,en_int,3);
171 spi_xfer(TDC_stream,rfedge2,1); 182 spi_xfer(TDC_stream,rfedge2,1);
172 spi_xfer(TDC_stream,rfedge1,1); 183 spi_xfer(TDC_stream,rfedge1,1);
173 spi_xfer(TDC_stream,delval1,19); 184 spi_xfer(TDC_stream,delval1,19);
174 output_high(TDC_ENABLE); 185 output_high(TDC_ENABLE);
175   186  
176 // update reg3 187 // update reg3
177 output_low(TDC_ENABLE); 188 output_low(TDC_ENABLE);
178 spi_xfer(TDC_stream,0x83); 189 spi_xfer(TDC_stream,0x83);
179 spi_xfer(TDC_stream,0,2); 190 spi_xfer(TDC_stream,0,2);
180 spi_xfer(TDC_stream,en_err_val,1); 191 spi_xfer(TDC_stream,en_err_val,1);
181 spi_xfer(TDC_stream,tim0_mr2,2); 192 spi_xfer(TDC_stream,tim0_mr2,2);
182 spi_xfer(TDC_stream,delval2,19); 193 spi_xfer(TDC_stream,delval2,19);
183 output_high(TDC_ENABLE); 194 output_high(TDC_ENABLE);
184   195  
185 // update reg4 196 // update reg4
186 output_low(TDC_ENABLE); 197 output_low(TDC_ENABLE);
187 spi_xfer(TDC_stream,0x84); 198 spi_xfer(TDC_stream,0x84);
188 spi_xfer(TDC_stream,0b00100,5); 199 spi_xfer(TDC_stream,0b00100,5);
189 spi_xfer(TDC_stream,delval3,19); 200 spi_xfer(TDC_stream,delval3,19);
190 output_high(TDC_ENABLE); 201 output_high(TDC_ENABLE);
191   202  
192 // update reg5 203 // update reg5
193 output_low(TDC_ENABLE); 204 output_low(TDC_ENABLE);
194 spi_xfer(TDC_stream,0x85); 205 spi_xfer(TDC_stream,0x85);
195 spi_xfer(TDC_stream,conf_fire,3); 206 spi_xfer(TDC_stream,conf_fire,3);
196 spi_xfer(TDC_stream,en_startnoise,1); 207 spi_xfer(TDC_stream,en_startnoise,1);
197 spi_xfer(TDC_stream,dis_phasenoise,1); 208 spi_xfer(TDC_stream,dis_phasenoise,1);
198 spi_xfer(TDC_stream,repeat_fire,3); 209 spi_xfer(TDC_stream,repeat_fire,3);
199 spi_xfer(TDC_stream,phase_fire,16); 210 spi_xfer(TDC_stream,phase_fire,16);
200 output_high(TDC_ENABLE); 211 output_high(TDC_ENABLE);
201 } 212 }
202   213  
203 float TDC_mrange2_get_time(unsigned int shot) 214 float TDC_mrange2_get_time(unsigned int shot)
204 { 215 {
205 unsigned int32 measurement; 216 unsigned int32 measurement;
206 float time; 217 float time;
207   218  
208 switch (shot) 219 switch (shot)
209 { 220 {
210 case 1: 221 case 1:
211 hit2=TDC_MRANGE2_HIT2_1CH1; 222 hit2=TDC_MRANGE2_HIT2_1CH1;
212 break; 223 break;
213   224  
214 case 2: 225 case 2:
215 hit2=TDC_MRANGE2_HIT2_2CH1; 226 hit2=TDC_MRANGE2_HIT2_2CH1;
216 break; 227 break;
217   228  
218 case 3: 229 case 3:
219 hit2=TDC_MRANGE2_HIT2_3CH1; 230 hit2=TDC_MRANGE2_HIT2_3CH1;
220 break; 231 break;
221 } 232 }
222 TDC_update_reg1(); // tell to ALU which shot period must be computed 233 TDC_update_reg1(); // tell to ALU which shot period must be computed
-   234
-   235 Delay_ms(50); // wait to computing of result
-   236
-   237 measurement=TDC_get_measurement(7&TDC_get_status()); // read computed value on pointer result register address
-   238
-   239  
-   240 switch (clkhsdiv)
-   241 {
-   242 case TDC_CLKHSDIV_1:
-   243 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS;
-   244 break;
-   245  
-   246 case TDC_CLKHSDIV_2:
-   247 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 2.0;
-   248 break;
-   249  
-   250 case TDC_CLKHSDIV_4:
-   251 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 4.0;
-   252 break;
-   253 case TDC_CLKHSDIV_8:
-   254 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 8.0;
-   255 break;
-   256 }
-   257 return time;
-   258 }
-   259  
-   260 float TDC_mrange1_get_time(unsigned int channel1, unsigned int shot1, unsigned int channel2, unsigned int shot2)
-   261 {
-   262 unsigned int32 measurement;
-   263 float time;
-   264  
-   265 switch (shot1)
-   266 {
-   267 case 0:
-   268 hit1=TDC_MRANGE1_HIT1_START;
-   269 break;
-   270 case 1:
-   271 if (channel1 == 1) hit1=TDC_MRANGE1_HIT1_1CH1; else hit1=TDC_MRANGE1_HIT1_1CH2;
-   272 break;
-   273  
-   274 case 2:
-   275 if (channel1 == 1) hit1=TDC_MRANGE1_HIT1_2CH1; else hit1=TDC_MRANGE1_HIT1_2CH2;
-   276 break;
-   277  
-   278 case 3:
-   279 if (channel1 == 1) hit1=TDC_MRANGE1_HIT1_3CH1; else hit1=TDC_MRANGE1_HIT1_3CH2;
-   280 break;
-   281  
-   282 case 4:
-   283 if (channel1 == 1) hit1=TDC_MRANGE1_HIT1_4CH1; else hit1=TDC_MRANGE1_HIT1_4CH2;
-   284 break;
-   285 }
-   286  
-   287 switch (shot2)
-   288 {
-   289 case 0:
-   290 hit2=TDC_MRANGE1_HIT2_START;
-   291 break;
-   292  
-   293 case 1:
-   294 if (channel2 == 1) hit2=TDC_MRANGE1_HIT2_1CH1; else hit2=TDC_MRANGE1_HIT2_1CH2;
-   295 break;
-   296  
-   297 case 2:
-   298 if (channel2 == 1) hit2=TDC_MRANGE1_HIT2_2CH1; else hit2=TDC_MRANGE1_HIT2_2CH2;
-   299 break;
-   300  
-   301 case 3:
-   302 if (channel2 == 1) hit2=TDC_MRANGE1_HIT2_3CH1; else hit2=TDC_MRANGE1_HIT2_3CH2;
-   303 break;
-   304  
-   305 case 4:
-   306 if (channel2 == 1) hit2=TDC_MRANGE1_HIT2_4CH1; else hit2=TDC_MRANGE1_HIT2_4CH2;
-   307 break;
-   308 }
-   309  
-   310 TDC_update_reg1(); // tell to ALU which shot period must be computed
223 311
224 Delay_ms(50); // wait to computing of result 312 Delay_ms(50); // wait to computing of result
225 313
226 measurement=TDC_get_measurement(7&TDC_get_status()); // read computed value on pointer result register address 314 measurement=TDC_get_measurement(7&TDC_get_status()); // read computed value on pointer result register address
227 315
228   316  
229 switch (clkhsdiv) 317 switch (clkhsdiv)
230 { 318 {
231 case TDC_CLKHSDIV_1: 319 case TDC_CLKHSDIV_1:
232 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS; 320 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS;
233 break; 321 break;
234   322  
235 case TDC_CLKHSDIV_2: 323 case TDC_CLKHSDIV_2:
236 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 2.0; 324 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 2.0;
237 break; 325 break;
238   326  
239 case TDC_CLKHSDIV_4: 327 case TDC_CLKHSDIV_4:
240 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 4.0; 328 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 4.0;
241 break; 329 break;
242 case TDC_CLKHSDIV_8: 330 case TDC_CLKHSDIV_8:
243 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 8.0; 331 time=(measurement/65536.0) * 1.0e6/TDC_CLKHS * 8.0;
244 break; 332 break;
245 } 333 }
246 return time; 334 return time;
247 } 335 }