1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
#include <math.h> |
2 |
#include <math.h> |
3 |
|
3 |
|
4 |
#define INTN_PIN PIN_D7 |
4 |
#define INTN_PIN PIN_D7 |
5 |
#include "GP2.h" |
5 |
#include "GP2.h" |
6 |
|
6 |
|
7 |
#define VERSION 0.2 |
7 |
#define VERSION 0.2 |
8 |
|
8 |
|
9 |
#define ONE_WIRE_PIN PIN_E2 |
9 |
#define ONE_WIRE_PIN PIN_E2 |
10 |
#include "ds1820.c" |
10 |
#include "ds1820.c" |
11 |
|
11 |
|
12 |
void main() |
12 |
void main() |
13 |
{ |
13 |
{ |
14 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
14 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
15 |
setup_adc(ADC_CLOCK_DIV_2); |
15 |
setup_adc(ADC_CLOCK_DIV_2); |
16 |
setup_spi(SPI_SS_DISABLED); |
16 |
setup_spi(SPI_SS_DISABLED); |
17 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
17 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
18 |
setup_timer_1(T1_DISABLED); |
18 |
setup_timer_1(T1_DISABLED); |
19 |
setup_timer_2(T2_DISABLED,0,1); |
19 |
setup_timer_2(T2_DISABLED,0,1); |
20 |
setup_ccp1(CCP_OFF); |
20 |
setup_ccp1(CCP_OFF); |
21 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
21 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
22 |
|
22 |
|
23 |
TDC_reset(); |
23 |
TDC_reset(); |
24 |
|
24 |
|
25 |
delay_ms(50); |
25 |
delay_ms(50); |
26 |
|
26 |
|
27 |
int16 ret16; |
27 |
int16 ret16; |
28 |
int8 ret8; |
28 |
int8 ret8; |
29 |
|
29 |
|
30 |
TDC_reset(); |
30 |
TDC_reset(); |
31 |
delay_ms(100); |
31 |
delay_ms(100); |
32 |
|
32 |
|
33 |
while(TRUE) |
33 |
while(TRUE) |
34 |
{ |
34 |
{ |
35 |
|
35 |
|
36 |
//----------------------------------------------- Nastaveni registru |
36 |
//----------------------------------------------- Nastaveni registru |
37 |
|
37 |
|
38 |
MRange=TDC_MRANGE2; // sets measurement mode |
38 |
MRange=TDC_MRANGE2; // sets measurement mode |
39 |
hit1=TDC_MRANGE2_HIT1_START; |
39 |
hit1=TDC_MRANGE2_HIT1_START; |
40 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
40 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
41 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
41 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
42 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
42 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
43 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
43 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
44 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
44 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
- |
|
45 |
firenum=TDC_FIRENUM_1; |
45 |
|
46 |
|
46 |
delval1=0x0; // windowing disabled |
47 |
delval1=0x0; // windowing disabled |
47 |
delval2=0x0; |
48 |
delval2=0x0; |
48 |
delval3=0x0; |
49 |
delval3=0x0; |
49 |
|
50 |
|
50 |
TDC_update_registers(); |
51 |
TDC_update_registers(); |
51 |
|
52 |
|
52 |
delay_ms(10); |
53 |
delay_ms(10); |
53 |
|
54 |
|
54 |
//----------------------------------------------- Measuring mode 2 |
55 |
//----------------------------------------------- Measuring mode 2 |
55 |
|
56 |
|
56 |
TDC_init(); |
57 |
TDC_init(); |
57 |
delay_ms(50); |
58 |
delay_ms(50); |
58 |
TDC_start_cycle(); |
59 |
TDC_start_cycle(); |
59 |
While(!input(INTN_PIN)); // waiting for interrupt flag |
60 |
While(!input(INTN_PIN)); // waiting for interrupt flag |
60 |
|
61 |
|
61 |
//----------------------------------------------- Pocitani |
62 |
//----------------------------------------------- Pocitani |
62 |
|
63 |
|
63 |
// printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
64 |
// printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
64 |
|
65 |
|
65 |
|
66 |
|
66 |
output_low(TDC_ENABLE); //status register read |
67 |
output_low(TDC_ENABLE); //status register read |
67 |
ret8=0; |
68 |
ret8=0; |
68 |
ret8=(0b1011<<4)|4; |
69 |
ret8=(0b1011<<4)|4; |
69 |
spi_xfer(TDC_stream,ret8,8); |
70 |
spi_xfer(TDC_stream,ret8,8); |
70 |
ret16=spi_xfer(TDC_stream,0,16); |
71 |
ret16=spi_xfer(TDC_stream,0,16); |
71 |
output_high(TDC_ENABLE); |
72 |
output_high(TDC_ENABLE); |
72 |
|
73 |
|
73 |
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()); //status register print |
74 |
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()); //status register print |
74 |
|
75 |
|
75 |
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)); |
76 |
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)); |
76 |
|
77 |
|
77 |
|
78 |
|
78 |
//----------------------------------------------- Nastaveni registru |
79 |
//----------------------------------------------- Nastaveni registru |
79 |
|
80 |
|
80 |
/* MRange=TDC_MRANGE1; |
81 |
/* MRange=TDC_MRANGE1; |
81 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
82 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
82 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
83 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
83 |
hitin1=TDC_HITIN1_1; |
84 |
hitin1=TDC_HITIN1_1; |
84 |
hitin2=TDC_HITIN2_1; |
85 |
hitin2=TDC_HITIN2_1; |
85 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
86 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
86 |
en_err_val=TDC_ERRVAL_EN; |
87 |
en_err_val=TDC_ERRVAL_EN; |
87 |
clkhsdiv=TDC_CLKHSDIV_4; |
88 |
clkhsdiv=TDC_CLKHSDIV_4; |
88 |
delval1=0x0; |
89 |
delval1=0x0; |
89 |
delval2=0x0; |
90 |
delval2=0x0; |
90 |
delval3=0x0; |
91 |
delval3=0x0; |
91 |
|
92 |
|
92 |
TDC_update_registers(); |
93 |
TDC_update_registers(); |
93 |
|
94 |
|
94 |
delay_ms(100); |
95 |
delay_ms(100); |
95 |
|
96 |
|
96 |
|
97 |
|
97 |
//----------------------------------------------- Mereni 1 |
98 |
//----------------------------------------------- Mereni 1 |
98 |
|
99 |
|
99 |
TDC_init(); |
100 |
TDC_init(); |
100 |
|
101 |
|
101 |
//----------------------------------------------- Pocitani |
102 |
//----------------------------------------------- Pocitani |
102 |
|
103 |
|
103 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
104 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
104 |
|
105 |
|
105 |
output_low(TDC_ENABLE); //status register |
106 |
output_low(TDC_ENABLE); //status register |
106 |
ret8=0; |
107 |
ret8=0; |
107 |
ret8=(0b1011<<4)|4; |
108 |
ret8=(0b1011<<4)|4; |
108 |
spi_xfer(TDC_stream,ret8,8); |
109 |
spi_xfer(TDC_stream,ret8,8); |
109 |
ret16=spi_xfer(TDC_stream,0,16); |
110 |
ret16=spi_xfer(TDC_stream,0,16); |
110 |
output_high(TDC_ENABLE); |
111 |
output_high(TDC_ENABLE); |
111 |
|
112 |
|
112 |
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()); |
113 |
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()); |
113 |
|
114 |
|
114 |
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)); |
115 |
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)); |
115 |
|
116 |
|
116 |
/// ----------------------------------------------- Temperature masurement |
117 |
/// ----------------------------------------------- Temperature masurement |
117 |
|
118 |
|
118 |
*/ |
119 |
*/ |
119 |
TDC_reset(); |
120 |
TDC_reset(); |
120 |
portnum=TDC_TPORTNUM_4; |
121 |
portnum=TDC_TPORTNUM_4; |
121 |
Tcycle=TDC_TCYCLE_SHORT; |
122 |
Tcycle=TDC_TCYCLE_SHORT; |
122 |
fakenum=TDC_TFAKENUM_2; |
123 |
fakenum=TDC_TFAKENUM_2; |
123 |
selclkT=TDC_TSELCLK_128HS; |
124 |
selclkT=TDC_TSELCLK_128HS; |
124 |
|
125 |
|
125 |
TDC_update_registers(); |
126 |
TDC_update_registers(); |
126 |
|
127 |
|
127 |
TDC_init(); |
128 |
TDC_init(); |
128 |
TDC_start_temp(); |
129 |
TDC_start_temp(); |
129 |
|
130 |
|
130 |
|
131 |
|
131 |
output_low(TDC_ENABLE); //status register |
132 |
output_low(TDC_ENABLE); //status register |
132 |
ret8=0; |
133 |
ret8=0; |
133 |
ret8=(0b1011<<4)|4; |
134 |
ret8=(0b1011<<4)|4; |
134 |
spi_xfer(TDC_stream,ret8,8); |
135 |
spi_xfer(TDC_stream,ret8,8); |
135 |
ret16=spi_xfer(TDC_stream,0,16); |
136 |
ret16=spi_xfer(TDC_stream,0,16); |
136 |
output_high(TDC_ENABLE); |
137 |
output_high(TDC_ENABLE); |
137 |
printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
138 |
printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
138 |
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()); |
139 |
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()); |
139 |
} |
140 |
} |
140 |
} |
141 |
} |