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 |
|
2178 |
kaklik |
42 |
void measurementM1() |
2164 |
kaklik |
43 |
{ |
2178 |
kaklik |
44 |
MRange=TDC_MRANGE1; |
|
|
45 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
|
|
46 |
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; |
|
|
52 |
delval1=0x0; |
|
|
53 |
delval2=0x0; |
|
|
54 |
delval3=0x0; |
|
|
55 |
|
|
|
56 |
TDC_update_registers(); |
|
|
57 |
|
|
|
58 |
delay_ms(100); |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
//----------------------------------------------- Mereni 1 |
|
|
62 |
|
|
|
63 |
TDC_init(); |
|
|
64 |
|
|
|
65 |
//----------------------------------------------- Pocitani |
|
|
66 |
|
|
|
67 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
|
|
68 |
|
|
|
69 |
/* output_low(TDC_ENABLE); //status register |
|
|
70 |
ret8=0; |
|
|
71 |
ret8=(0b1011<<4)|4; |
|
|
72 |
spi_xfer(TDC_stream,ret8,8); |
|
|
73 |
ret16=spi_xfer(TDC_stream,0,16); |
|
|
74 |
output_high(TDC_ENABLE); |
|
|
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()); |
|
|
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)); |
|
|
79 |
|
|
|
80 |
|
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
void measurementM2(unsigned int hits) |
|
|
85 |
{ |
2163 |
kaklik |
86 |
TDC_reset(); |
|
|
87 |
delay_ms(50); |
1980 |
kaklik |
88 |
MRange=TDC_MRANGE2; // sets measurement mode |
1965 |
kaklik |
89 |
hit1=TDC_MRANGE2_HIT1_START; |
2178 |
kaklik |
90 |
// hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
1980 |
kaklik |
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 |
|
|
93 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
|
|
94 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
2162 |
kaklik |
95 |
firenum=TDC_FIRENUM_1; |
1980 |
kaklik |
96 |
|
2178 |
kaklik |
97 |
switch(hits) |
|
|
98 |
{ |
|
|
99 |
case 1: |
|
|
100 |
hitin1=TDC_HITIN1_2; |
|
|
101 |
break; |
|
|
102 |
|
|
|
103 |
case 2: |
|
|
104 |
hitin1=TDC_HITIN1_3; |
|
|
105 |
break; |
|
|
106 |
|
|
|
107 |
case 3: |
|
|
108 |
hitin1=TDC_HITIN1_4; |
|
|
109 |
break; |
|
|
110 |
|
|
|
111 |
default: return; |
|
|
112 |
} |
|
|
113 |
|
1980 |
kaklik |
114 |
delval1=0x0; // windowing disabled |
1965 |
kaklik |
115 |
delval2=0x0; |
|
|
116 |
delval3=0x0; |
|
|
117 |
|
|
|
118 |
TDC_update_registers(); |
1931 |
kaklik |
119 |
|
2161 |
kaklik |
120 |
delay_ms(10); |
1980 |
kaklik |
121 |
|
2161 |
kaklik |
122 |
//----------------------------------------------- Measuring mode 2 |
1886 |
kakl |
123 |
|
|
|
124 |
TDC_init(); |
2161 |
kaklik |
125 |
delay_ms(50); |
|
|
126 |
TDC_start_cycle(); |
|
|
127 |
While(!input(INTN_PIN)); // waiting for interrupt flag |
2163 |
kaklik |
128 |
|
|
|
129 |
// STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! |
2160 |
kaklik |
130 |
|
1886 |
kakl |
131 |
//----------------------------------------------- Pocitani |
1964 |
kaklik |
132 |
|
2163 |
kaklik |
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)); |
2012 |
kaklik |
135 |
|
2164 |
kaklik |
136 |
} |
1967 |
kaklik |
137 |
|
2012 |
kaklik |
138 |
|
2164 |
kaklik |
139 |
void main() |
|
|
140 |
{ |
|
|
141 |
|
|
|
142 |
char command[20]; |
|
|
143 |
char tmp[5]; |
2178 |
kaklik |
144 |
unsigned int parameter; |
2164 |
kaklik |
145 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
146 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
147 |
setup_spi(SPI_SS_DISABLED); |
|
|
148 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
149 |
setup_timer_1(T1_DISABLED); |
|
|
150 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
151 |
setup_ccp1(CCP_OFF); |
|
|
152 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
153 |
|
|
|
154 |
TDC_reset(); |
|
|
155 |
delay_ms(50); |
|
|
156 |
|
2178 |
kaklik |
157 |
printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message |
|
|
158 |
// printf("#%s\r\n",&REV[4]); |
|
|
159 |
|
2164 |
kaklik |
160 |
while(TRUE) |
|
|
161 |
{ |
2178 |
kaklik |
162 |
printf("$TDC%s->", VERSION); |
|
|
163 |
fgets(command); |
|
|
164 |
printf("%s\r\n", command); |
|
|
165 |
|
2164 |
kaklik |
166 |
strcpy(tmp,"TM"); |
2178 |
kaklik |
167 |
if (!strncmp(command, tmp, 2)) temperature_measurement(); |
1980 |
kaklik |
168 |
|
2178 |
kaklik |
169 |
strcpy(tmp,"M2 "); |
|
|
170 |
if (!strncmp(command, tmp, 3)) |
|
|
171 |
{ |
|
|
172 |
parameter=atoi(command[5]); |
|
|
173 |
printf("%u\r\n",parameter); |
|
|
174 |
measurementM2(parameter); |
|
|
175 |
} |
1980 |
kaklik |
176 |
|
2178 |
kaklik |
177 |
strcpy(tmp,"M1 "); |
|
|
178 |
if (!strncmp(command, tmp, 3)) |
|
|
179 |
{ |
|
|
180 |
measurementM1(); |
|
|
181 |
} |
1980 |
kaklik |
182 |
|
1886 |
kakl |
183 |
} |
1744 |
kakl |
184 |
} |