1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
#include <math.h> |
2 |
#include <math.h> |
- |
|
3 |
#include <stdlib.h> |
- |
|
4 |
|
3 |
|
5 |
|
4 |
#define INTN_PIN PIN_D7 |
6 |
#define INTN_PIN PIN_D7 |
5 |
#include "GP2.h" |
7 |
#include "GP2.h" |
6 |
|
8 |
|
7 |
#define VERSION "0.2" |
9 |
#define VERSION "0.2" |
8 |
|
10 |
|
9 |
#define ONE_WIRE_PIN PIN_E2 |
11 |
#define ONE_WIRE_PIN PIN_E2 |
10 |
#include "ds1820.c" |
12 |
#include "ds1820.c" |
11 |
|
13 |
|
- |
|
14 |
|
12 |
void main() |
15 |
void get_string(char* s, unsigned int8 max) |
13 |
{ |
16 |
{ |
14 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
- |
|
15 |
setup_adc(ADC_CLOCK_DIV_2); |
- |
|
16 |
setup_spi(SPI_SS_DISABLED); |
17 |
unsigned int8 len; |
17 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
- |
|
18 |
setup_timer_1(T1_DISABLED); |
- |
|
19 |
setup_timer_2(T2_DISABLED,0,1); |
- |
|
20 |
setup_ccp1(CCP_OFF); |
18 |
char c; |
21 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
- |
|
22 |
|
19 |
|
- |
|
20 |
--max; |
- |
|
21 |
len=0; |
- |
|
22 |
do { |
23 |
TDC_reset(); |
23 |
c=getc(); |
- |
|
24 |
if(c==8) { // Backspace |
- |
|
25 |
if(len>0) { |
- |
|
26 |
len--; |
- |
|
27 |
putc(c); |
- |
|
28 |
putc(' '); |
- |
|
29 |
putc(c); |
- |
|
30 |
} |
- |
|
31 |
} else if ((c>=' ')&&(c<='~')) |
- |
|
32 |
if(len<=max) { |
- |
|
33 |
s[len++]=c; |
- |
|
34 |
putc(c); |
- |
|
35 |
} |
- |
|
36 |
} while(c!=13); |
- |
|
37 |
s[len]=0; |
- |
|
38 |
} |
- |
|
39 |
|
- |
|
40 |
signed int16 get_int() { |
- |
|
41 |
char s[5]; |
- |
|
42 |
signed int16 i; |
- |
|
43 |
|
- |
|
44 |
get_string(s, 7); |
- |
|
45 |
|
- |
|
46 |
i=atoi(s); |
24 |
delay_ms(50); |
47 |
return(i); |
- |
|
48 |
} |
25 |
|
49 |
|
- |
|
50 |
void temperature_measurement() /// Temperature masurement by TDC and dallas sensor |
26 |
while(TRUE) |
51 |
|
27 |
{ |
52 |
{ |
- |
|
53 |
//For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!! |
- |
|
54 |
TDC_reset(); |
- |
|
55 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
- |
|
56 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
- |
|
57 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
28 |
|
58 |
|
- |
|
59 |
portnum=TDC_TPORTNUM_4; |
- |
|
60 |
Tcycle=TDC_TCYCLE_SHORT; |
- |
|
61 |
fakenum=TDC_TFAKENUM_2; |
- |
|
62 |
selclkT=TDC_TSELCLK_128HS; |
- |
|
63 |
|
- |
|
64 |
TDC_update_registers(); |
- |
|
65 |
delay_ms(10); |
- |
|
66 |
|
- |
|
67 |
TDC_init(); |
- |
|
68 |
delay_ms(50); |
- |
|
69 |
|
- |
|
70 |
TDC_start_temp(); |
29 |
//----------------------------------------------- Nastaveni registru |
71 |
While(input(INTN_PIN)); // waiting for interrupt flag |
- |
|
72 |
|
- |
|
73 |
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)); |
- |
|
74 |
printf("%f \r\n",ds1820_read()+273.15); |
- |
|
75 |
|
- |
|
76 |
} |
30 |
|
77 |
|
- |
|
78 |
void measurementM2() |
- |
|
79 |
{ |
31 |
TDC_reset(); |
80 |
TDC_reset(); |
32 |
delay_ms(50); |
81 |
delay_ms(50); |
33 |
MRange=TDC_MRANGE2; // sets measurement mode |
82 |
MRange=TDC_MRANGE2; // sets measurement mode |
34 |
hit1=TDC_MRANGE2_HIT1_START; |
83 |
hit1=TDC_MRANGE2_HIT1_START; |
35 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
84 |
hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 |
36 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
85 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
37 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
86 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
38 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
87 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
39 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
88 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
40 |
firenum=TDC_FIRENUM_1; |
89 |
firenum=TDC_FIRENUM_1; |
41 |
|
90 |
|
42 |
delval1=0x0; // windowing disabled |
91 |
delval1=0x0; // windowing disabled |
43 |
delval2=0x0; |
92 |
delval2=0x0; |
44 |
delval3=0x0; |
93 |
delval3=0x0; |
45 |
|
94 |
|
46 |
TDC_update_registers(); |
95 |
TDC_update_registers(); |
47 |
|
96 |
|
48 |
delay_ms(10); |
97 |
delay_ms(10); |
49 |
|
98 |
|
50 |
//----------------------------------------------- Measuring mode 2 |
99 |
//----------------------------------------------- Measuring mode 2 |
51 |
|
100 |
|
52 |
TDC_init(); |
101 |
TDC_init(); |
53 |
delay_ms(50); |
102 |
delay_ms(50); |
54 |
TDC_start_cycle(); |
103 |
TDC_start_cycle(); |
55 |
While(!input(INTN_PIN)); // waiting for interrupt flag |
104 |
While(!input(INTN_PIN)); // waiting for interrupt flag |
56 |
|
105 |
|
57 |
// STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! |
106 |
// STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses! |
58 |
|
107 |
|
59 |
//----------------------------------------------- Pocitani |
108 |
//----------------------------------------------- Pocitani |
60 |
|
109 |
|
61 |
printf("$TDC%s M2 ", VERSION); |
110 |
printf("$TDC%s M2 ", VERSION); |
62 |
printf("%3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3)); |
111 |
printf("%3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3)); |
63 |
|
112 |
|
64 |
TDC_init(); |
113 |
} |
- |
|
114 |
|
65 |
|
115 |
|
- |
|
116 |
void main() |
- |
|
117 |
{ |
- |
|
118 |
|
- |
|
119 |
char command[20]; |
- |
|
120 |
char tmp[5]; |
66 |
|
121 |
|
- |
|
122 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
- |
|
123 |
setup_adc(ADC_CLOCK_DIV_2); |
- |
|
124 |
setup_spi(SPI_SS_DISABLED); |
- |
|
125 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
- |
|
126 |
setup_timer_1(T1_DISABLED); |
- |
|
127 |
setup_timer_2(T2_DISABLED,0,1); |
- |
|
128 |
setup_ccp1(CCP_OFF); |
- |
|
129 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
- |
|
130 |
|
- |
|
131 |
TDC_reset(); |
- |
|
132 |
delay_ms(50); |
- |
|
133 |
|
- |
|
134 |
while(TRUE) |
- |
|
135 |
{ |
- |
|
136 |
get_string(command, 20); |
- |
|
137 |
|
- |
|
138 |
strcpy(tmp,"TM"); |
67 |
//----------------------------------------------- Nastaveni registru |
139 |
if (stricmp(command, tmp)) temperature_measurement(); |
- |
|
140 |
strcpy(tmp,"M2"); |
- |
|
141 |
if (stricmp(command, tmp)) measurementM2(); |
68 |
|
142 |
|
69 |
/* MRange=TDC_MRANGE1; |
143 |
/* MRange=TDC_MRANGE1; |
70 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
144 |
hit1=TDC_MRANGE1_HIT1_NOAC; |
71 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
145 |
hit2=TDC_MRANGE1_HIT2_NOAC; |
72 |
hitin1=TDC_HITIN1_1; |
146 |
hitin1=TDC_HITIN1_1; |
73 |
hitin2=TDC_HITIN2_1; |
147 |
hitin2=TDC_HITIN2_1; |
74 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
148 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; |
75 |
en_err_val=TDC_ERRVAL_EN; |
149 |
en_err_val=TDC_ERRVAL_EN; |
76 |
clkhsdiv=TDC_CLKHSDIV_4; |
150 |
clkhsdiv=TDC_CLKHSDIV_4; |
77 |
delval1=0x0; |
151 |
delval1=0x0; |
78 |
delval2=0x0; |
152 |
delval2=0x0; |
79 |
delval3=0x0; |
153 |
delval3=0x0; |
80 |
|
154 |
|
81 |
TDC_update_registers(); |
155 |
TDC_update_registers(); |
82 |
|
156 |
|
83 |
delay_ms(100); |
157 |
delay_ms(100); |
84 |
|
158 |
|
85 |
|
159 |
|
86 |
//----------------------------------------------- Mereni 1 |
160 |
//----------------------------------------------- Mereni 1 |
87 |
|
161 |
|
88 |
TDC_init(); |
162 |
TDC_init(); |
89 |
|
163 |
|
90 |
//----------------------------------------------- Pocitani |
164 |
//----------------------------------------------- Pocitani |
91 |
|
165 |
|
92 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
166 |
// printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); |
93 |
|
167 |
|
94 |
output_low(TDC_ENABLE); //status register |
168 |
output_low(TDC_ENABLE); //status register |
95 |
ret8=0; |
169 |
ret8=0; |
96 |
ret8=(0b1011<<4)|4; |
170 |
ret8=(0b1011<<4)|4; |
97 |
spi_xfer(TDC_stream,ret8,8); |
171 |
spi_xfer(TDC_stream,ret8,8); |
98 |
ret16=spi_xfer(TDC_stream,0,16); |
172 |
ret16=spi_xfer(TDC_stream,0,16); |
99 |
output_high(TDC_ENABLE); |
173 |
output_high(TDC_ENABLE); |
100 |
|
174 |
|
101 |
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()); |
175 |
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 |
|
176 |
|
103 |
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)); |
177 |
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)); |
104 |
|
178 |
|
105 |
|
179 |
|
106 |
*/ |
180 |
*/ |
107 |
/// ----------------------------------------------- Temperature masurement |
- |
|
108 |
TDC_reset(); |
- |
|
109 |
delay_ms(50); |
- |
|
110 |
|
181 |
|
111 |
//For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!! |
- |
|
112 |
|
- |
|
113 |
hit1=TDC_MRANGE2_HIT1_START; |
- |
|
114 |
hitin1=TDC_HITIN1_4; // set number of hits on channel 1 |
- |
|
115 |
hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) |
- |
|
116 |
en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags |
- |
|
117 |
en_err_val=TDC_ERRVAL_EN; // enable of error value output |
- |
|
118 |
clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 |
- |
|
119 |
|
- |
|
120 |
portnum=TDC_TPORTNUM_4; |
- |
|
121 |
Tcycle=TDC_TCYCLE_SHORT; |
- |
|
122 |
fakenum=TDC_TFAKENUM_2; |
- |
|
123 |
selclkT=TDC_TSELCLK_128HS; |
- |
|
124 |
|
- |
|
125 |
TDC_update_registers(); |
- |
|
126 |
delay_ms(10); |
- |
|
127 |
|
- |
|
128 |
TDC_init(); |
- |
|
129 |
delay_ms(50); |
- |
|
130 |
|
- |
|
131 |
TDC_start_temp(); |
- |
|
132 |
|
- |
|
133 |
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)); |
- |
|
134 |
printf("%f \r\n",ds1820_read()+273.15); |
- |
|
135 |
} |
182 |
} |
136 |
} |
183 |
} |