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