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