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