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