1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
#include <math.h> |
2 |
#include <math.h> |
3 |
|
3 |
|
4 |
#use fast_io (D) |
4 |
#use fast_io (D) |
5 |
|
5 |
|
6 |
#define VERSION 0.2 |
6 |
#define VERSION 0.2 |
7 |
|
7 |
|
8 |
#define START PIN_D4 |
8 |
#define START PIN_D4 |
9 |
#define STOP1 PIN_D5 |
9 |
#define STOP1 PIN_D5 |
10 |
#define STOP2 PIN_D7 |
10 |
#define STOP2 PIN_D7 |
11 |
|
11 |
|
12 |
#include "GP2.h" |
12 |
#include "GP2.h" |
13 |
|
13 |
|
14 |
#define ONE_WIRE_PIN PIN_E2 |
14 |
#define ONE_WIRE_PIN PIN_E2 |
15 |
#include "ds1820.c" |
15 |
#include "ds1820.c" |
16 |
|
16 |
|
17 |
void main() |
17 |
void main() |
18 |
{ |
18 |
{ |
19 |
/* setup_adc_ports(NO_ANALOGS|VSS_VDD); |
19 |
/* setup_adc_ports(NO_ANALOGS|VSS_VDD); |
20 |
setup_adc(ADC_CLOCK_DIV_2); |
20 |
setup_adc(ADC_CLOCK_DIV_2); |
21 |
setup_psp(PSP_DISABLED); |
21 |
setup_psp(PSP_DISABLED); |
22 |
setup_spi(SPI_SS_DISABLED); |
22 |
setup_spi(SPI_SS_DISABLED); |
23 |
setup_wdt(WDT_OFF); |
23 |
setup_wdt(WDT_OFF); |
24 |
setup_timer_0(RTCC_INTERNAL); |
24 |
setup_timer_0(RTCC_INTERNAL); |
25 |
setup_timer_1(T1_DISABLED); |
25 |
setup_timer_1(T1_DISABLED); |
26 |
setup_timer_2(T2_DISABLED,0,1); |
26 |
setup_timer_2(T2_DISABLED,0,1); |
27 |
setup_ccp1(CCP_OFF); |
27 |
setup_ccp1(CCP_OFF); |
28 |
setup_comparator(NC_NC_NC_NC); |
28 |
setup_comparator(NC_NC_NC_NC); |
29 |
setup_vref(FALSE); |
29 |
setup_vref(FALSE); |
30 |
*/ |
30 |
*/ |
31 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
31 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
32 |
setup_adc(ADC_CLOCK_DIV_2); |
32 |
setup_adc(ADC_CLOCK_DIV_2); |
33 |
setup_spi(SPI_SS_DISABLED); |
33 |
setup_spi(SPI_SS_DISABLED); |
34 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
34 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
35 |
setup_timer_1(T1_DISABLED); |
35 |
setup_timer_1(T1_DISABLED); |
36 |
setup_timer_2(T2_DISABLED,0,1); |
36 |
setup_timer_2(T2_DISABLED,0,1); |
37 |
setup_ccp1(CCP_OFF); |
37 |
setup_ccp1(CCP_OFF); |
38 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
38 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
39 |
|
39 |
|
40 |
|
- |
|
41 |
set_tris_d(0x00); |
40 |
set_tris_d(0x00); |
42 |
|
41 |
|
43 |
TDC_reset(); |
42 |
TDC_reset(); |
44 |
|
43 |
|
45 |
output_low(START); |
44 |
output_low(START); |
46 |
output_low(STOP1); |
45 |
output_low(STOP1); |
47 |
output_low(STOP2); |
46 |
output_low(STOP2); |
48 |
|
47 |
|
49 |
delay_ms(50); |
48 |
delay_ms(50); |
50 |
|
49 |
|
51 |
int16 ret16; |
50 |
int16 ret16; |
52 |
int8 ret8; |
51 |
int8 ret8; |
53 |
|
52 |
|
54 |
TDC_reset(); |
53 |
TDC_reset(); |
55 |
delay_ms(100); |
54 |
delay_ms(100); |
56 |
|
55 |
|
57 |
while(TRUE) |
56 |
while(TRUE) |
58 |
{ |
57 |
{ |
59 |
|
- |
|
60 |
|
58 |
|
61 |
//----------------------------------------------- Nastaveni registru |
59 |
//----------------------------------------------- Nastaveni registru |
62 |
|
60 |
|
63 |
MRange=TDC_MRANGE2; // sets measurement mode |
61 |
MRange=TDC_MRANGE2; // sets measurement mode |
64 |
hit1=TDC_MRANGE2_HIT1_START; |
62 |
hit1=TDC_MRANGE2_HIT1_START; |
65 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
63 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
66 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
64 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
67 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
65 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
68 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
66 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
69 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
67 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
70 |
|
68 |
|
71 |
delval1=0x0; // windowing disabled |
69 |
delval1=0x0; // windowing disabled |
72 |
delval2=0x0; |
70 |
delval2=0x0; |
73 |
delval3=0x0; |
71 |
delval3=0x0; |
74 |
|
72 |
|
75 |
TDC_update_registers(); |
73 |
TDC_update_registers(); |
76 |
|
74 |
|
77 |
delay_ms(100); |
75 |
delay_ms(100); |
78 |
|
76 |
|
79 |
//----------------------------------------------- Mereni 2 |
77 |
//----------------------------------------------- Mereni 2 |
80 |
|
78 |
|
81 |
TDC_init(); |
79 |
TDC_init(); |
82 |
|
80 |
|
83 |
delay_ms(50); |
81 |
delay_ms(50); |
84 |
|
82 |
|
85 |
TDC_start_cycle(); |
83 |
TDC_start_cycle(); |
86 |
|
84 |
|
87 |
delay_ms(200); |
85 |
delay_ms(200); |
88 |
|
86 |
|
89 |
output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme byt v H) |
87 |
output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme byt v H) |
90 |
|
88 |
|
91 |
output_high(START); |
89 |
output_high(START); |
92 |
output_low(START); |
90 |
output_low(START); |
93 |
delay_us(150); |
91 |
delay_us(150); |
94 |
|
92 |
|
95 |
output_high(STOP1); |
93 |
output_high(STOP1); |
96 |
output_low(STOP1); |
94 |
output_low(STOP1); |
97 |
delay_us(1); |
95 |
delay_us(1); |
98 |
|
96 |
|
99 |
output_high(STOP1); |
97 |
output_high(STOP1); |
100 |
output_low(STOP1); |
98 |
output_low(STOP1); |
101 |
delay_us(10); |
99 |
delay_us(10); |
102 |
|
100 |
|
103 |
output_high(STOP1); |
101 |
output_high(STOP1); |
104 |
output_low(STOP1); |
102 |
output_low(STOP1); |
105 |
delay_us(1); |
103 |
delay_us(1); |
106 |
|
104 |
|
107 |
//----------------------------------------------- Pocitani |
105 |
//----------------------------------------------- Pocitani |
108 |
|
106 |
|
109 |
// printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
107 |
// printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
110 |
|
108 |
|
111 |
|
109 |
|
112 |
output_low(TDC_ENABLE); //status register |
110 |
output_low(TDC_ENABLE); //status register |
113 |
ret8=0; |
111 |
ret8=0; |
114 |
ret8=(0b1011<<4)|4; |
112 |
ret8=(0b1011<<4)|4; |
115 |
spi_xfer(TDC_stream,ret8,8); |
113 |
spi_xfer(TDC_stream,ret8,8); |
116 |
ret16=spi_xfer(TDC_stream,0,16); |
114 |
ret16=spi_xfer(TDC_stream,0,16); |
117 |
output_high(TDC_ENABLE); |
115 |
output_high(TDC_ENABLE); |
118 |
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()); |
116 |
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()); |
119 |
|
117 |
|
120 |
printf("Time2: %3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3)); |
118 |
printf("Time2: %3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3)); |
121 |
|
119 |
|
122 |
|
120 |
|
123 |
//----------------------------------------------- Nastaveni registru |
121 |
//----------------------------------------------- Nastaveni registru |
124 |
|
122 |
|
125 |
MRange=TDC_MRANGE1; |
123 |
MRange=TDC_MRANGE1; |
126 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
124 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
127 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
125 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
128 |
hitin1=TDC_HITIN1_1; |
126 |
hitin1=TDC_HITIN1_1; |
129 |
hitin2=TDC_HITIN2_1; |
127 |
hitin2=TDC_HITIN2_1; |
130 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
128 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
131 |
en_err_val=TDC_ERRVAL_EN; |
129 |
en_err_val=TDC_ERRVAL_EN; |
132 |
clkhsdiv=TDC_CLKHSDIV_4; |
130 |
clkhsdiv=TDC_CLKHSDIV_4; |
133 |
delval1=0x0; |
131 |
delval1=0x0; |
134 |
delval2=0x0; |
132 |
delval2=0x0; |
135 |
delval3=0x0; |
133 |
delval3=0x0; |
136 |
|
134 |
|
137 |
TDC_update_registers(); |
135 |
TDC_update_registers(); |
138 |
|
136 |
|
139 |
delay_ms(100); |
137 |
delay_ms(100); |
140 |
|
138 |
|
141 |
|
139 |
|
142 |
//----------------------------------------------- Mereni 1 |
140 |
//----------------------------------------------- Mereni 1 |
143 |
|
141 |
|
144 |
TDC_init(); |
142 |
TDC_init(); |
145 |
|
143 |
|
146 |
delay_ms(50); |
144 |
delay_ms(50); |
147 |
output_low(START); |
145 |
output_low(START); |
148 |
output_low(STOP1); |
146 |
output_low(STOP1); |
149 |
output_low(STOP2); |
147 |
output_low(STOP2); |
150 |
|
148 |
|
151 |
output_high(START); // start of time measurement |
149 |
output_high(START); // start of time measurement |
152 |
|
150 |
|
153 |
output_high(STOP2); |
151 |
output_high(STOP2); |
154 |
output_high(STOP1); |
152 |
output_high(STOP1); |
155 |
|
153 |
|
156 |
output_low(STOP1); |
154 |
output_low(STOP1); |
157 |
output_low(STOP2); |
155 |
output_low(STOP2); |
158 |
output_low(START); |
156 |
output_low(START); |
159 |
|
157 |
|
160 |
|
158 |
|
161 |
|
159 |
|
162 |
//----------------------------------------------- Pocitani |
160 |
//----------------------------------------------- Pocitani |
163 |
|
161 |
|
164 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
162 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
165 |
|
163 |
|
166 |
output_low(TDC_ENABLE); //status register |
164 |
output_low(TDC_ENABLE); //status register |
167 |
ret8=0; |
165 |
ret8=0; |
168 |
ret8=(0b1011<<4)|4; |
166 |
ret8=(0b1011<<4)|4; |
169 |
spi_xfer(TDC_stream,ret8,8); |
167 |
spi_xfer(TDC_stream,ret8,8); |
170 |
ret16=spi_xfer(TDC_stream,0,16); |
168 |
ret16=spi_xfer(TDC_stream,0,16); |
171 |
output_high(TDC_ENABLE); |
169 |
output_high(TDC_ENABLE); |
172 |
|
170 |
|
173 |
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()); |
171 |
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()); |
174 |
|
172 |
|
175 |
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)); |
173 |
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)); |
176 |
|
174 |
|
177 |
/// ----------------------------------------------- Temperature masurement |
175 |
/// ----------------------------------------------- Temperature masurement |
178 |
|
176 |
|
179 |
|
177 |
|
180 |
TDC_reset(); |
178 |
TDC_reset(); |
181 |
portnum=TDC_TPORTNUM_4; |
179 |
portnum=TDC_TPORTNUM_4; |
182 |
Tcycle=TDC_TCYCLE_SHORT; |
180 |
Tcycle=TDC_TCYCLE_SHORT; |
183 |
fakenum=TDC_TFAKENUM_2; |
181 |
fakenum=TDC_TFAKENUM_2; |
184 |
selclkT=TDC_TSELCLK_128HS; |
182 |
selclkT=TDC_TSELCLK_128HS; |
185 |
|
183 |
|
186 |
TDC_update_registers(); |
184 |
TDC_update_registers(); |
187 |
|
185 |
|
188 |
TDC_init(); |
186 |
TDC_init(); |
189 |
TDC_start_temp(); |
187 |
TDC_start_temp(); |
190 |
|
188 |
|
191 |
|
189 |
|
192 |
output_low(TDC_ENABLE); //status register |
190 |
output_low(TDC_ENABLE); //status register |
193 |
ret8=0; |
191 |
ret8=0; |
194 |
ret8=(0b1011<<4)|4; |
192 |
ret8=(0b1011<<4)|4; |
195 |
spi_xfer(TDC_stream,ret8,8); |
193 |
spi_xfer(TDC_stream,ret8,8); |
196 |
ret16=spi_xfer(TDC_stream,0,16); |
194 |
ret16=spi_xfer(TDC_stream,0,16); |
197 |
output_high(TDC_ENABLE); |
195 |
output_high(TDC_ENABLE); |
198 |
printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
196 |
printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
199 |
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()); |
197 |
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()); |
200 |
} |
198 |
} |
201 |
} |
199 |
} |