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