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