Rev 2182 Rev 2183
1 #include "main.h" 1 #include "main.h"
2 #include <math.h> 2 #include <math.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4   4  
5   5  
6 #define INTN_PIN PIN_D7 6 #define INTN_PIN PIN_D7
7 #include "GP2.h" 7 #include "GP2.h"
8   8  
9 #define VERSION "0.2" 9 #define VERSION "0.2"
10   10  
11 #define ONE_WIRE_PIN PIN_E2 11 #define ONE_WIRE_PIN PIN_E2
12 #include "ds1820.c" 12 #include "ds1820.c"
13   13  
14 void temperature_measurement() /// Temperature masurement by TDC and dallas sensor 14 void temperature_measurement() /// Temperature masurement by TDC and dallas sensor
15   15  
16 { 16 {
17 //For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!! 17 //For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!!
18 TDC_reset(); 18 TDC_reset();
19 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags 19 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
20 en_err_val=TDC_ERRVAL_EN; // enable of error value output 20 en_err_val=TDC_ERRVAL_EN; // enable of error value output
21 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 21 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
22 22
23 portnum=TDC_TPORTNUM_4; 23 portnum=TDC_TPORTNUM_4;
24 Tcycle=TDC_TCYCLE_SHORT; 24 Tcycle=TDC_TCYCLE_SHORT;
25 fakenum=TDC_TFAKENUM_2; 25 fakenum=TDC_TFAKENUM_2;
26 selclkT=TDC_TSELCLK_128HS; 26 selclkT=TDC_TSELCLK_128HS;
27   27  
28 TDC_update_registers(); 28 TDC_update_registers();
29 delay_ms(10); 29 delay_ms(10);
30   30  
31 TDC_init(); 31 TDC_init();
32 delay_ms(50); 32 delay_ms(50);
33   33  
34 TDC_start_temp(); 34 TDC_start_temp();
35 While(input(INTN_PIN)); // waiting for interrupt flag 35 While(input(INTN_PIN)); // waiting for interrupt flag
36 36
37 printf("$TDC%s TMP %10LU %10LU %10LU %10LU ", VERSION, TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 37 printf("$TDC%s TMP %10LU %10LU %10LU %10LU ", VERSION, TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
38 printf("%f \r\n",ds1820_read()+273.15); 38 printf("%f \r\n",ds1820_read()+273.15);
39   39  
40 } 40 }
41   41  
42 void measurementM1(unsigned int hits1,unsigned int hits2,) 42 void measurementM1(unsigned int hits1,unsigned int hits2,)
43 { 43 {
44 TDC_reset(); 44 TDC_reset();
45 delay_ms(50); 45 delay_ms(50);
46 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags 46 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
47 en_err_val=TDC_ERRVAL_EN; // enable of error value output 47 en_err_val=TDC_ERRVAL_EN; // enable of error value output
48 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 2 48 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 2
49 firenum=TDC_FIRENUM_1; 49 firenum=TDC_FIRENUM_1;
50 calibrate=TDC_CALIBRATE_EN; 50 calibrate=TDC_CALIBRATE_EN;
51 disautocal=TDC_AUTOCAL_EN; // automatic calibration enabled 51 disautocal=TDC_AUTOCAL_EN; // automatic calibration enabled
52 52
53 rfedge2=TDC_CH2EDGE_FAL_RIS; // stop channels input edge sensityvity selection 53 rfedge2=TDC_CH2EDGE_FAL_RIS; // stop channels input edge sensityvity selection
54 rfedge1=TDC_CH1EDGE_FAL_RIS; 54 rfedge1=TDC_CH1EDGE_FAL_RIS;
55   55  
56 MRange=TDC_MRANGE1; 56 MRange=TDC_MRANGE1;
57 hit1=TDC_MRANGE1_HIT1_NOAC; 57 hit1=TDC_MRANGE1_HIT1_NOAC;
58 hit2=TDC_MRANGE1_HIT2_NOAC; 58 hit2=TDC_MRANGE1_HIT2_NOAC;
59   59  
60   60  
61 // hitin2=TDC_HITIN2_1; 61 // hitin2=TDC_HITIN2_1;
62 // hitin1=TDC_HITIN1_1; 62 // hitin1=TDC_HITIN1_1;
63   63  
64 delval1=0x0; 64 delval1=0x0;
65 delval2=0x0; 65 delval2=0x0;
66 delval3=0x0; 66 delval3=0x0;
67   67  
68 switch(hits2) // sets number of hits on channel 1 68 switch(hits2) // sets number of hits on channel 1
69 { 69 {
70 case 0: 70 case 0:
71 hitin2=TDC_HITIN2_0; 71 hitin2=TDC_HITIN2_0;
72 break; 72 break;
73   73  
74 case 1: 74 case 1:
75 hitin2=TDC_HITIN2_1; 75 hitin2=TDC_HITIN2_1;
76 break; 76 break;
77   77  
78 case 2: 78 case 2:
79 hitin2=TDC_HITIN2_2; 79 hitin2=TDC_HITIN2_2;
80 break; 80 break;
81   81  
82 case 3: 82 case 3:
83 hitin2=TDC_HITIN2_3; 83 hitin2=TDC_HITIN2_3;
84 break; 84 break;
85   85  
86 case 4: 86 case 4:
87 hitin2=TDC_HITIN2_4; 87 hitin2=TDC_HITIN2_4;
88 break; 88 break;
89   89  
90 default: return; 90 default: return;
91 } 91 }
92   92  
93 switch(hits1) // sets number of hits on channel 1 93 switch(hits1) // sets number of hits on channel 1
94 { 94 {
95 case 0: 95 case 0:
96 hitin1=TDC_HITIN1_0; 96 hitin1=TDC_HITIN1_0;
97 break; 97 break;
98   98  
99 case 1: 99 case 1:
100 hitin1=TDC_HITIN1_1; 100 hitin1=TDC_HITIN1_1;
101 break; 101 break;
102   102  
103 case 2: 103 case 2:
104 hitin1=TDC_HITIN1_2; 104 hitin1=TDC_HITIN1_2;
105 break; 105 break;
106   106  
107 case 3: 107 case 3:
108 hitin1=TDC_HITIN1_3; 108 hitin1=TDC_HITIN1_3;
109 break; 109 break;
110   110  
111 case 4: 111 case 4:
112 hitin1=TDC_HITIN1_4; 112 hitin1=TDC_HITIN1_4;
113 break; 113 break;
114   114  
115 default: return; 115 default: return;
116 } 116 }
117   117  
118 TDC_update_registers(); 118 TDC_update_registers();
119   119  
120 delay_ms(50); 120 delay_ms(50);
121   121  
122   122  
123 //----------------------------------------------- Mereni 1 123 //----------------------------------------------- Mereni 1
124 124
125 TDC_init(); 125 TDC_init();
126 // TDC_start_cycle(); // Fire pulse generator activation 126 // TDC_start_cycle(); // Fire pulse generator activation
127 delay_ms(100); 127 delay_ms(100);
128   128  
129 While(!input(INTN_PIN)); // waiting for interrupt flag 129 While(!input(INTN_PIN)); // waiting for interrupt flag
130   130  
131 //----------------------------------------------- Pocitani 131 //----------------------------------------------- Pocitani
132   132  
133 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 133 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
134   134  
135 /* output_low(TDC_ENABLE); //status register 135 /* output_low(TDC_ENABLE); //status register
136 ret8=0; 136 ret8=0;
137 ret8=(0b1011<<4)|4; 137 ret8=(0b1011<<4)|4;
138 spi_xfer(TDC_stream,ret8,8); 138 spi_xfer(TDC_stream,ret8,8);
139 ret16=spi_xfer(TDC_stream,0,16); 139 ret16=spi_xfer(TDC_stream,0,16);
140 output_high(TDC_ENABLE); 140 output_high(TDC_ENABLE);
141   141  
142 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status()); 142 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status());
143 */ 143 */
144   144  
145 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(TDC_get_status())>>12), (1&(TDC_get_status())>>11), (1&(TDC_get_status())>>10), 1&(TDC_get_status())>>9, 7&(TDC_get_status())>>6, 7&(TDC_get_status())>>3, 7&TDC_get_status()); 145 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(TDC_get_status())>>12), (1&(TDC_get_status())>>11), (1&(TDC_get_status())>>10), 1&(TDC_get_status())>>9, 7&(TDC_get_status())>>6, 7&(TDC_get_status())>>3, 7&TDC_get_status());
146   146  
147 printf("$TDC%s M1 ", VERSION); 147 printf("$TDC%s M1 ", VERSION);
148 printf("%f %f", TDC_mrange1_get_time(1,1,1,2), TDC_mrange1_get_time(1,0,1,1)); 148 printf("%f %f", TDC_mrange1_get_time(1,1,1,2), TDC_mrange1_get_time(1,0,1,1));
149 149
150 // syntax TDC_mrange1_get_time(Channel, shot, Channel , shot ) 150 // syntax TDC_mrange1_get_time(Channel, shot, Channel , shot )
151 151
152 printf("\r\n"); 152 printf("\r\n");
153 } 153 }
154   154  
155   155  
156 void measurementM2(unsigned int hits) 156 void measurementM2(unsigned int hits)
157 { 157 {
158 unsigned int i; 158 unsigned int i;
159   159  
160 TDC_reset(); 160 TDC_reset();
161 delay_ms(50); 161 delay_ms(50);
162 MRange=TDC_MRANGE2; // sets measurement mode 162 MRange=TDC_MRANGE2; // sets measurement mode
163 hit1=TDC_MRANGE2_HIT1_START; // time is always counted from start pulse at this measurement mode 163 hit1=TDC_MRANGE2_HIT1_START; // time is always counted from start pulse at this measurement mode
164 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) 164 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
165 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags 165 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
166 en_err_val=TDC_ERRVAL_EN; // enable of error value output 166 en_err_val=TDC_ERRVAL_EN; // enable of error value output
167 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 167 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
168 firenum=TDC_FIRENUM_1; 168 firenum=TDC_FIRENUM_1;
169 169
170 switch(hits) // sets number of hits on channel 1 170 switch(hits) // sets number of hits on channel 1
171 { 171 {
172 case 1: 172 case 1:
173 hitin1=TDC_HITIN1_2; 173 hitin1=TDC_HITIN1_2;
174 break; 174 break;
175   175  
176 case 2: 176 case 2:
177 hitin1=TDC_HITIN1_3; 177 hitin1=TDC_HITIN1_3;
178 break; 178 break;
179   179  
180 case 3: 180 case 3:
181 hitin1=TDC_HITIN1_4; 181 hitin1=TDC_HITIN1_4;
182 break; 182 break;
183   183  
184 default: return; 184 default: return;
185 } 185 }
186   186  
187 delval1=0x0; // windowing disabled 187 delval1=0x0; // windowing disabled
188 delval2=0x0; 188 delval2=0x0;
189 delval3=0x0; 189 delval3=0x0;
190 190
191 TDC_update_registers(); 191 TDC_update_registers();
192   192  
193 delay_ms(10); 193 delay_ms(10);
194   194  
195 //----------------------------------------------- Measuring mode 2 195 //----------------------------------------------- Measuring mode 2
196 196
197 TDC_init(); 197 TDC_init();
198 delay_ms(50); 198 delay_ms(50);
199 TDC_start_cycle(); 199 TDC_start_cycle();
200 While(!input(INTN_PIN)); // waiting for interrupt flag 200 While(!input(INTN_PIN)); // waiting for interrupt flag
201 201
202 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! -  
203 -  
204 //----------------------------------------------- Calculate and print output 202 //----------------------------------------------- Calculate and print output
205   203  
206 printf("$TDC%s M2 ", VERSION); 204 printf("$TDC%s M2 ", VERSION);
207 for(i=1;i<=hits;i++) printf(" %4.6f", TDC_mrange2_get_time(i)); 205 for(i=1;i<=hits;i++) printf(" %4.6f", TDC_mrange2_get_time(i));
208 printf("\r\n"); 206 printf("\r\n");
209   207  
210 } 208 }
211   209  
212 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght 210 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght
213 { 211 {
214 char c=0; 212 char c=0;
215 unsigned int len=0; 213 unsigned int len=0;
216   214  
217 while ((c=getc()) != 13) 215 while ((c=getc()) != 13)
218 { 216 {
219 ptr[len]=c; 217 ptr[len]=c;
220 218
221 if (len == max-2) 219 if (len == max-2)
222 { 220 {
223 ptr[len+1]=0; 221 ptr[len+1]=0;
224 return; 222 return;
225 } 223 }
226 len++; 224 len++;
227 } 225 }
228 226
229 ptr[len]=0; 227 ptr[len]=0;
230 return; 228 return;
231 } 229 }
232   230  
233   231  
234 void main() 232 void main()
235 { 233 {
236   234  
237 char command[20]; 235 char command[20];
238 char tmp[5]; 236 char tmp[5];
239 char *ptr; 237 char *ptr;
240 unsigned long parameter, parameter2; 238 unsigned long parameter, parameter2;
241 setup_adc_ports(NO_ANALOGS|VSS_VDD); 239 setup_adc_ports(NO_ANALOGS|VSS_VDD);
242 setup_adc(ADC_CLOCK_DIV_2); 240 setup_adc(ADC_CLOCK_DIV_2);
243 setup_spi(SPI_SS_DISABLED); 241 setup_spi(SPI_SS_DISABLED);
244 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 242 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
245 setup_timer_1(T1_DISABLED); 243 setup_timer_1(T1_DISABLED);
246 setup_timer_2(T2_DISABLED,0,1); 244 setup_timer_2(T2_DISABLED,0,1);
247 setup_ccp1(CCP_OFF); 245 setup_ccp1(CCP_OFF);
248 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 246 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
249   247  
250 TDC_reset(); 248 TDC_reset();
251 delay_ms(50); 249 delay_ms(50);
252   250  
253 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message 251 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message
254 // printf("#%s\r\n",&REV[4]); 252 // printf("#%s\r\n",&REV[4]);
255   253  
256 while(TRUE) 254 while(TRUE)
257 { 255 {
258 printf("$TDC%s->", VERSION); // print prompt 256 printf("$TDC%s->", VERSION); // print prompt
259 get_command(command, 20); // receive command from terminal 257 get_command(command, 20); // receive command from terminal
260 printf("%s\r\n", command); // echo received command 258 printf("%s\r\n", command); // echo received command
261   259  
262 strcpy(tmp,"TM"); 260 strcpy(tmp,"TM");
263 if (!strncmp(command, tmp, 2)) temperature_measurement(); 261 if (!strncmp(command, tmp, 2)) temperature_measurement();
264   262  
265 strcpy(tmp,"M2 "); 263 strcpy(tmp,"M2 ");
266 if (!strncmp(command, tmp, 3)) 264 if (!strncmp(command, tmp, 3))
267 { 265 {
268 parameter=strtol(command+3,&ptr,10); 266 parameter=strtol(command+3,&ptr,10);
269 measurementM2(parameter); 267 measurementM2(parameter);
270 } 268 }
271 269
272 strcpy(tmp,"M1 "); 270 strcpy(tmp,"M1 ");
273 if (!strncmp(command, tmp, 3)) 271 if (!strncmp(command, tmp, 3))
274 { 272 {
275 parameter=strtol(command+3,&ptr,10); 273 parameter=strtol(command+3,&ptr,10);
276 parameter2=strtol(ptr,&ptr,10); 274 parameter2=strtol(ptr,&ptr,10);
277 printf("%lu\r\n", parameter); // echo received command 275 printf("%lu\r\n", parameter); // echo received command
278 printf("%lu\r\n", parameter2); // echo received command 276 printf("%lu\r\n", parameter2); // echo received command
279 measurementM1(parameter, parameter2); 277 measurementM1(parameter, parameter2);
280 } 278 }
281   279  
282 } 280 }
283 } 281 }