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