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