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