Rev 2178 Rev 2179
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()
43 { 43 {
44 MRange=TDC_MRANGE1; 44 MRange=TDC_MRANGE1;
45 hit1=TDC_MRANGE1_HIT1_NOAC; 45 hit1=TDC_MRANGE1_HIT1_NOAC;
46 hit2=TDC_MRANGE1_HIT2_NOAC; 46 hit2=TDC_MRANGE1_HIT2_NOAC;
47 hitin1=TDC_HITIN1_1; 47 hitin1=TDC_HITIN1_1;
48 hitin2=TDC_HITIN2_1; 48 hitin2=TDC_HITIN2_1;
49 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; 49 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
50 en_err_val=TDC_ERRVAL_EN; 50 en_err_val=TDC_ERRVAL_EN;
51 clkhsdiv=TDC_CLKHSDIV_4; 51 clkhsdiv=TDC_CLKHSDIV_4;
52 delval1=0x0; 52 delval1=0x0;
53 delval2=0x0; 53 delval2=0x0;
54 delval3=0x0; 54 delval3=0x0;
55 55
56 TDC_update_registers(); 56 TDC_update_registers();
57   57  
58 delay_ms(100); 58 delay_ms(100);
59   59  
60   60  
61 //----------------------------------------------- Mereni 1 61 //----------------------------------------------- Mereni 1
62 62
63 TDC_init(); 63 TDC_init();
64   64  
65 //----------------------------------------------- Pocitani 65 //----------------------------------------------- Pocitani
66   66  
67 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 67 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
68   68  
69 /* output_low(TDC_ENABLE); //status register 69 /* output_low(TDC_ENABLE); //status register
70 ret8=0; 70 ret8=0;
71 ret8=(0b1011<<4)|4; 71 ret8=(0b1011<<4)|4;
72 spi_xfer(TDC_stream,ret8,8); 72 spi_xfer(TDC_stream,ret8,8);
73 ret16=spi_xfer(TDC_stream,0,16); 73 ret16=spi_xfer(TDC_stream,0,16);
74 output_high(TDC_ENABLE); 74 output_high(TDC_ENABLE);
75   75  
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()); 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());
77 */ 77 */
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)); 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   79  
80   80  
81 } 81 }
82   82  
83   83  
84 void measurementM2(unsigned int hits) 84 void measurementM2(unsigned int hits)
85 { 85 {
86 TDC_reset(); 86 TDC_reset();
87 delay_ms(50); 87 delay_ms(50);
88 MRange=TDC_MRANGE2; // sets measurement mode 88 MRange=TDC_MRANGE2; // sets measurement mode
89 hit1=TDC_MRANGE2_HIT1_START; 89 hit1=TDC_MRANGE2_HIT1_START;
90 // hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 90 // hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
91 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) 91 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 92 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 93 en_err_val=TDC_ERRVAL_EN; // enable of error value output
94 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 94 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
95 firenum=TDC_FIRENUM_1; 95 firenum=TDC_FIRENUM_1;
96 96
97 switch(hits) 97 switch(hits)
98 { 98 {
99 case 1: 99 case 1:
100 hitin1=TDC_HITIN1_2; 100 hitin1=TDC_HITIN1_2;
101 break; 101 break;
102   102  
103 case 2: 103 case 2:
104 hitin1=TDC_HITIN1_3; 104 hitin1=TDC_HITIN1_3;
105 break; 105 break;
106   106  
107 case 3: 107 case 3:
108 hitin1=TDC_HITIN1_4; 108 hitin1=TDC_HITIN1_4;
109 break; 109 break;
110   110  
111 default: return; 111 default: return;
112 } 112 }
113   113  
114 delval1=0x0; // windowing disabled 114 delval1=0x0; // windowing disabled
115 delval2=0x0; 115 delval2=0x0;
116 delval3=0x0; 116 delval3=0x0;
117 117
118 TDC_update_registers(); 118 TDC_update_registers();
119   119  
120 delay_ms(10); 120 delay_ms(10);
121   121  
122 //----------------------------------------------- Measuring mode 2 122 //----------------------------------------------- Measuring mode 2
123 123
124 TDC_init(); 124 TDC_init();
125 delay_ms(50); 125 delay_ms(50);
126 TDC_start_cycle(); 126 TDC_start_cycle();
127 While(!input(INTN_PIN)); // waiting for interrupt flag 127 While(!input(INTN_PIN)); // waiting for interrupt flag
128 128
129 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! 129 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses!
130 130
131 //----------------------------------------------- Pocitani 131 //----------------------------------------------- Pocitani
132   132  
133 printf("$TDC%s M2 ", VERSION); 133 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)); 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));
135   135  
136 } 136 }
137   137  
-   138 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght
-   139 {
-   140 char c=0;
-   141 unsigned int len=0;
-   142  
-   143 while ((c=getc()) != 13)
-   144 {
-   145 ptr[len]=c;
-   146
-   147 if (len == max-2)
-   148 {
-   149 ptr[len+1]=0;
-   150 return;
-   151 }
-   152 len++;
-   153 }
-   154
-   155 return;
-   156 }
-   157  
138   158  
139 void main() 159 void main()
140 { 160 {
141   161  
142 char command[20]; 162 char command[20];
143 char tmp[5]; 163 char tmp[5];
-   164 char *ptr;
144 unsigned int parameter; 165 unsigned long parameter;
145 setup_adc_ports(NO_ANALOGS|VSS_VDD); 166 setup_adc_ports(NO_ANALOGS|VSS_VDD);
146 setup_adc(ADC_CLOCK_DIV_2); 167 setup_adc(ADC_CLOCK_DIV_2);
147 setup_spi(SPI_SS_DISABLED); 168 setup_spi(SPI_SS_DISABLED);
148 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 169 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
149 setup_timer_1(T1_DISABLED); 170 setup_timer_1(T1_DISABLED);
150 setup_timer_2(T2_DISABLED,0,1); 171 setup_timer_2(T2_DISABLED,0,1);
151 setup_ccp1(CCP_OFF); 172 setup_ccp1(CCP_OFF);
152 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 173 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
153   174  
154 TDC_reset(); 175 TDC_reset();
155 delay_ms(50); 176 delay_ms(50);
156   177  
157 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message 178 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message
158 // printf("#%s\r\n",&REV[4]); 179 // printf("#%s\r\n",&REV[4]);
159   180  
160 while(TRUE) 181 while(TRUE)
161 { 182 {
162 printf("$TDC%s->", VERSION); 183 printf("$TDC%s->", VERSION); // print prompt
163 fgets(command); 184 get_command(command, 20); // receive command from terminal
164 printf("%s\r\n", command); 185 printf("%s\r\n", command); // echo received command
165 186
166 strcpy(tmp,"TM"); 187 strcpy(tmp,"TM");
167 if (!strncmp(command, tmp, 2)) temperature_measurement(); 188 if (!strncmp(command, tmp, 2)) temperature_measurement();
168   189  
169 strcpy(tmp,"M2 "); 190 strcpy(tmp,"M2 ");
170 if (!strncmp(command, tmp, 3)) 191 if (!strncmp(command, tmp, 3))
171 { 192 {
172 parameter=atoi(command[5]); 193 parameter=strtol(command+3,&ptr,10);
-   194
-   195 printf("%s\r\n", command+3);
173 printf("%u\r\n",parameter); 196 printf("%lu\r\n",parameter);
174 measurementM2(parameter); 197 measurementM2(parameter);
175 } 198 }
176 199
177 strcpy(tmp,"M1 "); 200 strcpy(tmp,"M1 ");
178 if (!strncmp(command, tmp, 3)) 201 if (!strncmp(command, tmp, 3))
179 { 202 {
180 measurementM1(); 203 measurementM1();
181 } 204 }
182   205  
183 } 206 }
184 } 207 }