Rev 2181 Rev 2182
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;
-   62 // hitin1=TDC_HITIN1_1;
-   63  
61 delval1=0x0; 64 delval1=0x0;
62 delval2=0x0; 65 delval2=0x0;
63 delval3=0x0; 66 delval3=0x0;
64   67  
65 switch(hits2) // sets number of hits on channel 1 68 switch(hits2) // sets number of hits on channel 1
66 { 69 {
67 case 0: 70 case 0:
68 hitin2=TDC_HITIN2_0; 71 hitin2=TDC_HITIN2_0;
69 break; 72 break;
70   73  
71 case 1: 74 case 1:
72 hitin2=TDC_HITIN2_1; 75 hitin2=TDC_HITIN2_1;
73 break; 76 break;
74   77  
75 case 2: 78 case 2:
76 hitin2=TDC_HITIN2_2; 79 hitin2=TDC_HITIN2_2;
77 break; 80 break;
78   81  
79 case 3: 82 case 3:
80 hitin2=TDC_HITIN2_3; 83 hitin2=TDC_HITIN2_3;
81 break; 84 break;
82   85  
83 case 4: 86 case 4:
84 hitin2=TDC_HITIN2_4; 87 hitin2=TDC_HITIN2_4;
85 break; 88 break;
86   89  
87 default: return; 90 default: return;
88 } 91 }
89   92  
90 switch(hits1) // sets number of hits on channel 1 93 switch(hits1) // sets number of hits on channel 1
91 { 94 {
92 case 0: 95 case 0:
93 hitin1=TDC_HITIN1_0; 96 hitin1=TDC_HITIN1_0;
94 break; 97 break;
95   98  
96 case 1: 99 case 1:
97 hitin1=TDC_HITIN1_1; 100 hitin1=TDC_HITIN1_1;
98 break; 101 break;
99   102  
100 case 2: 103 case 2:
101 hitin1=TDC_HITIN1_2; 104 hitin1=TDC_HITIN1_2;
102 break; 105 break;
103   106  
104 case 3: 107 case 3:
105 hitin1=TDC_HITIN1_3; 108 hitin1=TDC_HITIN1_3;
106 break; 109 break;
107   110  
108 case 4: 111 case 4:
109 hitin1=TDC_HITIN1_4; 112 hitin1=TDC_HITIN1_4;
110 break; 113 break;
111   114  
112 default: return; 115 default: return;
113 } 116 }
114   117  
115 TDC_update_registers(); 118 TDC_update_registers();
116   119  
117 delay_ms(50); 120 delay_ms(50);
118   121  
119   122  
120 //----------------------------------------------- Mereni 1 123 //----------------------------------------------- Mereni 1
121 124
122 TDC_init(); 125 TDC_init();
123 // TDC_start_cycle(); // Fire pulse generator activation 126 // TDC_start_cycle(); // Fire pulse generator activation
124 delay_ms(100); 127 delay_ms(100);
125   128  
126 While(!input(INTN_PIN)); // waiting for interrupt flag 129 While(!input(INTN_PIN)); // waiting for interrupt flag
127   130  
128 //----------------------------------------------- Pocitani 131 //----------------------------------------------- Pocitani
129   132  
130 // 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));
131   134  
132 /* output_low(TDC_ENABLE); //status register 135 /* output_low(TDC_ENABLE); //status register
133 ret8=0; 136 ret8=0;
134 ret8=(0b1011<<4)|4; 137 ret8=(0b1011<<4)|4;
135 spi_xfer(TDC_stream,ret8,8); 138 spi_xfer(TDC_stream,ret8,8);
136 ret16=spi_xfer(TDC_stream,0,16); 139 ret16=spi_xfer(TDC_stream,0,16);
137 output_high(TDC_ENABLE); 140 output_high(TDC_ENABLE);
138   141  
139 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());
140 */ 143 */
141   144  
142 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());
143   146  
144 printf("$TDC%s M1 ", VERSION); 147 printf("$TDC%s M1 ", VERSION);
145 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));
146 149
147 // syntax TDC_mrange1_get_time(Channel, shot, Channel , shot ) 150 // syntax TDC_mrange1_get_time(Channel, shot, Channel , shot )
148 151
149 printf("\r\n"); 152 printf("\r\n");
150 } 153 }
151   154  
152   155  
153 void measurementM2(unsigned int hits) 156 void measurementM2(unsigned int hits)
154 { 157 {
155 unsigned int i; 158 unsigned int i;
156   159  
157 TDC_reset(); 160 TDC_reset();
158 delay_ms(50); 161 delay_ms(50);
159 MRange=TDC_MRANGE2; // sets measurement mode 162 MRange=TDC_MRANGE2; // sets measurement mode
160 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
161 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)
162 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
163 en_err_val=TDC_ERRVAL_EN; // enable of error value output 166 en_err_val=TDC_ERRVAL_EN; // enable of error value output
164 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 167 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
165 firenum=TDC_FIRENUM_1; 168 firenum=TDC_FIRENUM_1;
166 169
167 switch(hits) // sets number of hits on channel 1 170 switch(hits) // sets number of hits on channel 1
168 { 171 {
169 case 1: 172 case 1:
170 hitin1=TDC_HITIN1_2; 173 hitin1=TDC_HITIN1_2;
171 break; 174 break;
172   175  
173 case 2: 176 case 2:
174 hitin1=TDC_HITIN1_3; 177 hitin1=TDC_HITIN1_3;
175 break; 178 break;
176   179  
177 case 3: 180 case 3:
178 hitin1=TDC_HITIN1_4; 181 hitin1=TDC_HITIN1_4;
179 break; 182 break;
180   183  
181 default: return; 184 default: return;
182 } 185 }
183   186  
184 delval1=0x0; // windowing disabled 187 delval1=0x0; // windowing disabled
185 delval2=0x0; 188 delval2=0x0;
186 delval3=0x0; 189 delval3=0x0;
187 190
188 TDC_update_registers(); 191 TDC_update_registers();
189   192  
190 delay_ms(10); 193 delay_ms(10);
191   194  
192 //----------------------------------------------- Measuring mode 2 195 //----------------------------------------------- Measuring mode 2
193 196
194 TDC_init(); 197 TDC_init();
195 delay_ms(50); 198 delay_ms(50);
196 TDC_start_cycle(); 199 TDC_start_cycle();
197 While(!input(INTN_PIN)); // waiting for interrupt flag 200 While(!input(INTN_PIN)); // waiting for interrupt flag
198 201
199 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! 202 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses!
200 203
201 //----------------------------------------------- Calculate and print output 204 //----------------------------------------------- Calculate and print output
202   205  
203 printf("$TDC%s M2 ", VERSION); 206 printf("$TDC%s M2 ", VERSION);
204 for(i=1;i<=hits;i++) printf(" %4.6f", TDC_mrange2_get_time(i)); 207 for(i=1;i<=hits;i++) printf(" %4.6f", TDC_mrange2_get_time(i));
205 printf("\r\n"); 208 printf("\r\n");
206   209  
207 } 210 }
208   211  
209 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght 212 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght
210 { 213 {
211 char c=0; 214 char c=0;
212 unsigned int len=0; 215 unsigned int len=0;
213   216  
214 while ((c=getc()) != 13) 217 while ((c=getc()) != 13)
215 { 218 {
216 ptr[len]=c; 219 ptr[len]=c;
217 220
218 if (len == max-2) 221 if (len == max-2)
219 { 222 {
220 ptr[len+1]=0; 223 ptr[len+1]=0;
221 return; 224 return;
222 } 225 }
223 len++; 226 len++;
224 } 227 }
225 228
226 ptr[len]=0; 229 ptr[len]=0;
227 return; 230 return;
228 } 231 }
229   232  
230   233  
231 void main() 234 void main()
232 { 235 {
233   236  
234 char command[20]; 237 char command[20];
235 char tmp[5]; 238 char tmp[5];
236 char *ptr; 239 char *ptr;
237 unsigned long parameter, parameter2; 240 unsigned long parameter, parameter2;
238 setup_adc_ports(NO_ANALOGS|VSS_VDD); 241 setup_adc_ports(NO_ANALOGS|VSS_VDD);
239 setup_adc(ADC_CLOCK_DIV_2); 242 setup_adc(ADC_CLOCK_DIV_2);
240 setup_spi(SPI_SS_DISABLED); 243 setup_spi(SPI_SS_DISABLED);
241 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 244 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
242 setup_timer_1(T1_DISABLED); 245 setup_timer_1(T1_DISABLED);
243 setup_timer_2(T2_DISABLED,0,1); 246 setup_timer_2(T2_DISABLED,0,1);
244 setup_ccp1(CCP_OFF); 247 setup_ccp1(CCP_OFF);
245 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 248 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
246   249  
247 TDC_reset(); 250 TDC_reset();
248 delay_ms(50); 251 delay_ms(50);
249   252  
250 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message 253 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message
251 // printf("#%s\r\n",&REV[4]); 254 // printf("#%s\r\n",&REV[4]);
252   255  
253 while(TRUE) 256 while(TRUE)
254 { 257 {
255 printf("$TDC%s->", VERSION); // print prompt 258 printf("$TDC%s->", VERSION); // print prompt
256 get_command(command, 20); // receive command from terminal 259 get_command(command, 20); // receive command from terminal
257 printf("%s\r\n", command); // echo received command 260 printf("%s\r\n", command); // echo received command
258 261  
259 strcpy(tmp,"TM"); 262 strcpy(tmp,"TM");
260 if (!strncmp(command, tmp, 2)) temperature_measurement(); 263 if (!strncmp(command, tmp, 2)) temperature_measurement();
261   264  
262 strcpy(tmp,"M2 "); 265 strcpy(tmp,"M2 ");
263 if (!strncmp(command, tmp, 3)) 266 if (!strncmp(command, tmp, 3))
264 { 267 {
265 parameter=strtol(command+3,&ptr,10); 268 parameter=strtol(command+3,&ptr,10);
266 measurementM2(parameter); 269 measurementM2(parameter);
267 } 270 }
268 271
269 strcpy(tmp,"M1 "); 272 strcpy(tmp,"M1 ");
270 if (!strncmp(command, tmp, 3)) 273 if (!strncmp(command, tmp, 3))
271 { 274 {
272 parameter=strtol(command+3,&ptr,10); 275 parameter=strtol(command+3,&ptr,10);
273 parameter2=strtol(ptr,&ptr,10); 276 parameter2=strtol(ptr,&ptr,10);
274 printf("%lu\r\n", parameter); // echo received command 277 printf("%lu\r\n", parameter); // echo received command
275 printf("%lu\r\n", parameter2); // echo received command 278 printf("%lu\r\n", parameter2); // echo received command
276 measurementM1(parameter, parameter2); 279 measurementM1(parameter, parameter2);
277 } 280 }
278   281  
279 } 282 }
280 } 283 }