Rev 2184 Rev 2185
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_2; // divide clkHS by 2 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("%5.6f %5.6f", TDC_mrange1_get_time(1,0,1,1), TDC_mrange1_get_time(2,1,2,0)); 134 printf("%5.6f %5.6f", TDC_mrange1_get_time(1,1,1,0), TDC_mrange1_get_time(2,1,2,0));
135 -  
136 // syntax TDC_mrange1_get_time(Channel, shot, Channel , shot ) 135 // syntax TDC_mrange1_get_time(HIT1:[Channel, shot], HIT2:[Channel , shot]) time=HIT1-HIT2 and does not support negative output
137 -  
138 printf("\r\n"); 136 printf("\r\n");
139 } 137 }
140   138  
141   139  
142 void measurementM2(unsigned int hits) 140 void measurementM2(unsigned int hits)
143 { 141 {
144 unsigned int i; 142 unsigned int i;
145   143  
146 TDC_reset(); 144 TDC_reset();
147 delay_ms(50); 145 delay_ms(50);
148 MRange=TDC_MRANGE2; // sets measurement mode 146 MRange=TDC_MRANGE2; // sets measurement mode
149 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
150 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)
151 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
152 en_err_val=TDC_ERRVAL_EN; // enable of error value output 150 en_err_val=TDC_ERRVAL_EN; // enable of error value output
153 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 151 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
154 firenum=TDC_FIRENUM_1; 152 firenum=TDC_FIRENUM_1;
155 153
156 switch(hits) // sets number of hits on channel 1 154 switch(hits) // sets number of hits on channel 1
157 { 155 {
158 case 1: 156 case 1:
159 hitin1=TDC_HITIN1_2; 157 hitin1=TDC_HITIN1_2;
160 break; 158 break;
161   159  
162 case 2: 160 case 2:
163 hitin1=TDC_HITIN1_3; 161 hitin1=TDC_HITIN1_3;
164 break; 162 break;
165   163  
166 case 3: 164 case 3:
167 hitin1=TDC_HITIN1_4; 165 hitin1=TDC_HITIN1_4;
168 break; 166 break;
169   167  
170 default: return; 168 default: return;
171 } 169 }
172   170  
173 delval1=0x0; // windowing disabled 171 delval1=0x0; // windowing disabled
174 delval2=0x0; 172 delval2=0x0;
175 delval3=0x0; 173 delval3=0x0;
176 174
177 TDC_update_registers(); 175 TDC_update_registers();
178   176  
179 delay_ms(10); 177 delay_ms(10);
180   178  
181 //----------------------------------------------- Measuring mode 2 179 //----------------------------------------------- Measuring mode 2
182 180
183 TDC_init(); 181 TDC_init();
184 delay_ms(50); 182 delay_ms(50);
185 TDC_start_cycle(); 183 TDC_start_cycle();
186 While(!input(INTN_PIN)); // waiting for interrupt flag 184 While(!input(INTN_PIN)); // waiting for interrupt flag
187 185
188 //----------------------------------------------- Calculate and print output 186 //----------------------------------------------- Calculate and print output
189   187  
190 printf("$TDC%s M2 ", VERSION); 188 printf("$TDC%s M2 ", VERSION);
191 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));
192 printf("\r\n"); 190 printf("\r\n");
193   191  
194 } 192 }
195   193  
196 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
197 { 195 {
198 char c=0; 196 char c=0;
199 unsigned int len=0; 197 unsigned int len=0;
200   198  
201 while ((c=getc()) != 13) 199 while ((c=getc()) != 13)
202 { 200 {
203 ptr[len]=c; 201 ptr[len]=c;
204 202
205 if (len == max-2) 203 if (len == max-2)
206 { 204 {
207 ptr[len+1]=0; 205 ptr[len+1]=0;
208 return; 206 return;
209 } 207 }
210 len++; 208 len++;
211 } 209 }
212 210
213 ptr[len]=0; 211 ptr[len]=0;
214 return; 212 return;
215 } 213 }
216   214  
217   215  
218 void main() 216 void main()
219 { 217 {
220   218  
221 char command[20]; 219 char command[20];
222 char tmp[5]; 220 char tmp[5];
223 char *ptr; 221 char *ptr;
224 unsigned long parameter, parameter2; 222 unsigned long parameter, parameter2;
225 setup_adc_ports(NO_ANALOGS|VSS_VDD); 223 setup_adc_ports(NO_ANALOGS|VSS_VDD);
226 setup_adc(ADC_CLOCK_DIV_2); 224 setup_adc(ADC_CLOCK_DIV_2);
227 setup_spi(SPI_SS_DISABLED); 225 setup_spi(SPI_SS_DISABLED);
228 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 226 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
229 setup_timer_1(T1_DISABLED); 227 setup_timer_1(T1_DISABLED);
230 setup_timer_2(T2_DISABLED,0,1); 228 setup_timer_2(T2_DISABLED,0,1);
231 setup_ccp1(CCP_OFF); 229 setup_ccp1(CCP_OFF);
232 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
233   231  
234 TDC_reset(); 232 TDC_reset();
235 delay_ms(50); 233 delay_ms(50);
236   234  
237 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message 235 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message
238 // printf("#%s\r\n",&REV[4]); 236 // printf("#%s\r\n",&REV[4]);
239   237  
240 while(TRUE) 238 while(TRUE)
241 { 239 {
242 printf("$TDC%s->", VERSION); // print prompt 240 printf("$TDC%s->", VERSION); // print prompt
243 get_command(command, 20); // receive command from terminal 241 get_command(command, 20); // receive command from terminal
244 printf("%s\r\n", command); // echo received command 242 printf("%s\r\n", command); // echo received command
245   243  
-   244  
-   245 // delay_ms(50);
-   246 // strcpy(command,"M1 1 1");
-   247  
-   248  
246 strcpy(tmp,"TM"); 249 strcpy(tmp,"TM");
247 if (!strncmp(command, tmp, 2)) temperature_measurement(); 250 if (!strncmp(command, tmp, 2)) temperature_measurement();
248   251  
249 strcpy(tmp,"M2 "); 252 strcpy(tmp,"M2 ");
250 if (!strncmp(command, tmp, 3)) 253 if (!strncmp(command, tmp, 3))
251 { 254 {
252 parameter=strtol(command+3,&ptr,10); 255 parameter=strtol(command+3,&ptr,10);
253 measurementM2(parameter); 256 measurementM2(parameter);
254 } 257 }
255 258
256 strcpy(tmp,"M1 "); 259 strcpy(tmp,"M1 ");
257 if (!strncmp(command, tmp, 3)) 260 if (!strncmp(command, tmp, 3))
258 { 261 {
259 parameter=strtol(command+3,&ptr,10); 262 parameter=strtol(command+3,&ptr,10);
260 parameter2=strtol(ptr,&ptr,10); 263 parameter2=strtol(ptr,&ptr,10);
261 measurementM1(parameter, parameter2); 264 measurementM1(parameter, parameter2);
262 } 265 }
263 } 266 }
264 } 267 }