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