Rev 1886 Rev 1931
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 /* 36 /*
37 1 0 0 0 0 ADR2 ADR1 ADR0 Write into address ADR 37 1 0 0 0 0 ADR2 ADR1 ADR0 Write into address ADR
38 1 0 1 1 0 ADR2 ADR1 ADR0 Read from address ADR 38 1 0 1 1 0 ADR2 ADR1 ADR0 Read from address ADR
39 0 1 1 1 0 0 0 0 Init 39 0 1 1 1 0 0 0 0 Init
40 0 1 0 1 0 0 0 0 Power On Reset 40 0 1 0 1 0 0 0 0 Power On Reset
41 0 0 0 0 0 0 0 1 Start_Cycle 41 0 0 0 0 0 0 0 1 Start_Cycle
42 0 0 0 0 0 0 1 0 Start_Temp 42 0 0 0 0 0 0 1 0 Start_Temp
43 0 0 0 0 0 0 1 1 Start_Cal_Resonator 43 0 0 0 0 0 0 1 1 Start_Cal_Resonator
44 0 0 0 0 0 1 0 0 Start_Cal_TDC 44 0 0 0 0 0 1 0 0 Start_Cal_TDC
45 */ 45 */
46   46  
47 int32 ble,ret32; 47 int32 ble,ret32;
48 int16 ret16; 48 int16 ret16;
49 int8 ret8; 49 int8 ret8;
50   50  
-   51  
51 while(TRUE) 52 while(TRUE)
52 { 53 {
-   54 delay_ms(100);
-   55  
-   56  
53 TDC_reset(); 57 TDC_reset();
54 delay_ms(100); 58 delay_ms(100);
55 59
56 //----------------------------------------------- Nastaveni registru 60 //----------------------------------------------- Nastaveni registru
57 output_low(TDC_ENABLE); 61 output_low(TDC_ENABLE);
58 ble=0; 62 ble=0;
59 ble=(8<<28)|(0<<24); 63 ble=(8<<28)|(0<<24);
60 ble|=(0<<20)|(0<<16)|(0<<14)|(3<<12)|(1<<10)|(0<<9)|(0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(0<<1)|0; 64 ble|=(0<<20)|(0<<16)|(0<<14)|(3<<12)|(1<<10)|(0<<9)|(0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(0<<1)|0;
61 spi_xfer(TDC_stream,ble,32); 65 spi_xfer(TDC_stream,ble,32);
62 output_high(TDC_ENABLE); 66 output_high(TDC_ENABLE);
63 67
64 output_low(TDC_ENABLE); 68 output_low(TDC_ENABLE);
65 ble=0; 69 ble=0;
66 ble=(8<<28)|(1<<24); 70 ble=(8<<28)|(1<<24);
67 ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0; 71 ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0;
68 spi_xfer(TDC_stream,ble,32); 72 spi_xfer(TDC_stream,ble,32);
69 output_high(TDC_ENABLE); 73 output_high(TDC_ENABLE);
70 74
71 output_low(TDC_ENABLE); 75 output_low(TDC_ENABLE);
72 ble=0; 76 ble=0;
73 ble=(8<<28)|(2<<24); 77 ble=(8<<28)|(2<<24);
74 ble|=(1<<21)|(1<<20)|(1<<19)|0; 78 ble|=(1<<21)|(1<<20)|(1<<19)|0;
75 spi_xfer(TDC_stream,ble,32); 79 spi_xfer(TDC_stream,ble,32);
76 output_high(TDC_ENABLE); 80 output_high(TDC_ENABLE);
77 81
78 output_low(TDC_ENABLE); 82 output_low(TDC_ENABLE);
79 ble=0; 83 ble=0;
80 ble=(8<<28)|(3<<24); 84 ble=(8<<28)|(3<<24);
81 ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0; 85 ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0;
82 spi_xfer(TDC_stream,ble,32); 86 spi_xfer(TDC_stream,ble,32);
83 output_high(TDC_ENABLE); 87 output_high(TDC_ENABLE);
84 88
85 output_low(TDC_ENABLE); 89 output_low(TDC_ENABLE);
86 ble=0; 90 ble=0;
87 ble=(8<<28)|(4<<24); 91 ble=(8<<28)|(4<<24);
88 ble|=(4<<19)|0; 92 ble|=(4<<19)|0;
89 spi_xfer(TDC_stream,ble,32); 93 spi_xfer(TDC_stream,ble,32);
90 output_high(TDC_ENABLE); 94 output_high(TDC_ENABLE);
91 95
92 output_low(TDC_ENABLE); 96 output_low(TDC_ENABLE);
93 ble=0; 97 ble=0;
94 ble=(8<<28)|(5<<24); 98 ble=(8<<28)|(5<<24);
95 ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0; 99 ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0;
96 spi_xfer(TDC_stream,ble,32); 100 spi_xfer(TDC_stream,ble,32);
97 output_high(TDC_ENABLE); 101 output_high(TDC_ENABLE);
98 -  
99 102  
100 103  
101 //----------------------------------------------- Vypis registru 104 //----------------------------------------------- Vypis registru
102 output_low(TDC_ENABLE); 105 output_low(TDC_ENABLE);
103 ret8=0; 106 ret8=0;
104 ret8=(0b1011<<4)|0; 107 ret8=(0b1011<<4)|0;
105 spi_xfer(TDC_stream,ret8,8); 108 spi_xfer(TDC_stream,ret8,8);
106 ret32=spi_xfer(TDC_stream,0,32); 109 ret32=spi_xfer(TDC_stream,0,32);
107 output_high(TDC_ENABLE); 110 output_high(TDC_ENABLE);
108 printf("- %LX ", ret32); 111 printf("- %LX ", ret32);
109 112
110 output_low(TDC_ENABLE); 113 output_low(TDC_ENABLE);
111 ret8=0; 114 ret8=0;
112 ret8=(0b1011<<4)|1; 115 ret8=(0b1011<<4)|1;
113 spi_xfer(TDC_stream,ret8,8); 116 spi_xfer(TDC_stream,ret8,8);
114 ret32=spi_xfer(TDC_stream,0,32); 117 ret32=spi_xfer(TDC_stream,0,32);
115 output_high(TDC_ENABLE); 118 output_high(TDC_ENABLE);
116 printf("%LX ", ret32); 119 printf("%LX ", ret32);
117 120
118 output_low(TDC_ENABLE); 121 output_low(TDC_ENABLE);
119 ret8=0; 122 ret8=0;
120 ret8=(0b1011<<4)|2; 123 ret8=(0b1011<<4)|2;
121 spi_xfer(TDC_stream,ret8,8); 124 spi_xfer(TDC_stream,ret8,8);
122 ret32=spi_xfer(TDC_stream,0,32); 125 ret32=spi_xfer(TDC_stream,0,32);
123 output_high(TDC_ENABLE); 126 output_high(TDC_ENABLE);
124 printf("%LX ", ret32); 127 printf("%LX ", ret32);
125 128
126 output_low(TDC_ENABLE); 129 output_low(TDC_ENABLE);
127 ret8=0; 130 ret8=0;
128 ret8=(0b1011<<4)|3; 131 ret8=(0b1011<<4)|3;
129 spi_xfer(TDC_stream,ret8,8); 132 spi_xfer(TDC_stream,ret8,8);
130 ret32=spi_xfer(TDC_stream,0,32); 133 ret32=spi_xfer(TDC_stream,0,32);
131 output_high(TDC_ENABLE); 134 output_high(TDC_ENABLE);
132 printf("%LX ", ret32); 135 printf("%LX ", ret32);
133 136
134 output_low(TDC_ENABLE); 137 output_low(TDC_ENABLE);
135 ret8=0; 138 ret8=0;
136 ret8=(0b1011<<4)|4; 139 ret8=(0b1011<<4)|4;
137 spi_xfer(TDC_stream,ret8,8); 140 spi_xfer(TDC_stream,ret8,8);
138 ret16=spi_xfer(TDC_stream,0,16); 141 ret16=spi_xfer(TDC_stream,0,16);
139 output_high(TDC_ENABLE); 142 output_high(TDC_ENABLE);
140 printf("[%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&ret16); 143 printf("[%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&ret16);
141 144
142 output_low(TDC_ENABLE); 145 output_low(TDC_ENABLE);
143 ret8=0; 146 ret8=0;
144 ret8=(0b1011<<4)|5; 147 ret8=(0b1011<<4)|5;
145 spi_xfer(TDC_stream,ret8,8); 148 spi_xfer(TDC_stream,ret8,8);
146 ret8=spi_xfer(TDC_stream,0,8); 149 ret8=spi_xfer(TDC_stream,0,8);
147 output_high(TDC_ENABLE); 150 output_high(TDC_ENABLE);
148 printf("%X\r\n", ret8); 151 printf("%X\r\n", ret8);
149 152
150 //----------------------------------------------- Mereni 153 //----------------------------------------------- Mereni
151 154
152 TDC_init(); 155 TDC_init();
153 156
154 delay_ms(50); 157 delay_ms(50);
155 158
156 TDC_start_cycle(); 159 TDC_start_cycle();
157 160
158 delay_ms(200); 161 delay_ms(200);
159 162
160 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H) 163 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H)
161 164
162 output_high(START); 165 output_high(START);
163 output_low(START); 166 output_low(START);
164 delay_us(1); 167 delay_us(1);
165 168
166 output_high(STOP1); 169 output_high(STOP1);
167 output_low(STOP1); 170 output_low(STOP1);
168 delay_us(1); 171 delay_us(1);
169 172
170 173
171 output_high(STOP1); 174 output_high(STOP1);
172 output_low(STOP1); 175 output_low(STOP1);
173 delay_us(1); 176 delay_us(1);
174 177
175 output_high(STOP1); 178 output_high(STOP1);
176 output_low(STOP1); 179 output_low(STOP1);
177 delay_us(1); 180 delay_us(1);
178 181
179 182
180 183
181 //----------------------------------------------- Pocitani 184 //----------------------------------------------- Pocitani
182 int32 nn; 185 int32 nn;
183 for(nn=3;nn<=5;nn++) 186 for(nn=3;nn<=5;nn++)
184 { 187 {
185 delay_ms(500); 188 delay_ms(500);
186 189
187 output_low(TDC_ENABLE); 190 output_low(TDC_ENABLE);
188 ret8=0; 191 ret8=0;
189 ret8=(0b1011<<4)|0; 192 ret8=(0b1011<<4)|0;
190 spi_xfer(TDC_stream,ret8,8); 193 spi_xfer(TDC_stream,ret8,8);
191 ret32=spi_xfer(TDC_stream,0,32); 194 ret32=spi_xfer(TDC_stream,0,32);
192 output_high(TDC_ENABLE); 195 output_high(TDC_ENABLE);
193 printf("* %LX ", ret32); 196 printf("* %LX ", ret32);
194 197
195 output_low(TDC_ENABLE); 198 output_low(TDC_ENABLE);
196 ret8=0; 199 ret8=0;
197 ret8=(0b1011<<4)|1; 200 ret8=(0b1011<<4)|1;
198 spi_xfer(TDC_stream,ret8,8); 201 spi_xfer(TDC_stream,ret8,8);
199 ret32=spi_xfer(TDC_stream,0,32); 202 ret32=spi_xfer(TDC_stream,0,32);
200 output_high(TDC_ENABLE); 203 output_high(TDC_ENABLE);
201 printf("%LX ", ret32); 204 printf("%LX ", ret32);
202 205
203 output_low(TDC_ENABLE); 206 output_low(TDC_ENABLE);
204 ret8=0; 207 ret8=0;
205 ret8=(0b1011<<4)|2; 208 ret8=(0b1011<<4)|2;
206 spi_xfer(TDC_stream,ret8,8); 209 spi_xfer(TDC_stream,ret8,8);
207 ret32=spi_xfer(TDC_stream,0,32); 210 ret32=spi_xfer(TDC_stream,0,32);
208 output_high(TDC_ENABLE); 211 output_high(TDC_ENABLE);
209 printf("%LX ", ret32); 212 printf("%LX ", ret32);
210 213
211 output_low(TDC_ENABLE); 214 output_low(TDC_ENABLE);
212 ret8=0; 215 ret8=0;
213 ret8=(0b1011<<4)|3; 216 ret8=(0b1011<<4)|3;
214 spi_xfer(TDC_stream,ret8,8); 217 spi_xfer(TDC_stream,ret8,8);
215 ret32=spi_xfer(TDC_stream,0,32); 218 ret32=spi_xfer(TDC_stream,0,32);
216 output_high(TDC_ENABLE); 219 output_high(TDC_ENABLE);
217 printf("%LX ", ret32); 220 printf("%LX ", ret32);
218 221
219 output_low(TDC_ENABLE); 222 output_low(TDC_ENABLE);
220 ret8=0; 223 ret8=0;
221 ret8=(0b1011<<4)|4; 224 ret8=(0b1011<<4)|4;
222 spi_xfer(TDC_stream,ret8,8); 225 spi_xfer(TDC_stream,ret8,8);
223 ret16=spi_xfer(TDC_stream,0,16); 226 ret16=spi_xfer(TDC_stream,0,16);
224 output_high(TDC_ENABLE); 227 output_high(TDC_ENABLE);
225 printf("[%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&ret16); 228 printf("[%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&ret16);
226 229
227 output_low(TDC_ENABLE); 230 output_low(TDC_ENABLE);
228 ret8=0; 231 ret8=0;
229 ret8=(0b1011<<4)|5; 232 ret8=(0b1011<<4)|5;
230 spi_xfer(TDC_stream,ret8,8); 233 spi_xfer(TDC_stream,ret8,8);
231 ret8=spi_xfer(TDC_stream,0,8); 234 ret8=spi_xfer(TDC_stream,0,8);
232 output_high(TDC_ENABLE); 235 output_high(TDC_ENABLE);
233 printf("%X\r\n", ret8); 236 printf("%X\r\n", ret8);
234 237
235 // Next calculation 238 // Next calculation
236 output_low(TDC_ENABLE); 239 output_low(TDC_ENABLE);
237 ble=0; 240 ble=0;
238 ble=(8<<28)|(1<<24); 241 ble=(8<<28)|(1<<24);
239 ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00; 242 ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00;
240 spi_xfer(TDC_stream,ble,32); 243 spi_xfer(TDC_stream,ble,32);
241 output_high(TDC_ENABLE); 244 output_high(TDC_ENABLE);
242 245
243 } 246 }
244 247
245 } 248 }
246   249  
247 } 250 }