Rev Author Line No. Line
1744 kakl 1 #include "main.h"
2  
1886 kakl 3 #define VERSION 0.2
1757 kakl 4  
1759 kakl 5 #define START PIN_D4
6 #define STOP1 PIN_D5
7 #define STOP2 PIN_D6
1886 kakl 8  
1774 kaklik 9 #include "GP2.h"
1759 kakl 10  
1774 kaklik 11 #define ONE_WIRE_PIN PIN_E2
12 #include "ds1820.c"
13  
1744 kakl 14 void main()
15 {
16 setup_adc_ports(NO_ANALOGS|VSS_VDD);
17 setup_adc(ADC_CLOCK_DIV_2);
18 setup_psp(PSP_DISABLED);
19 setup_spi(SPI_SS_DISABLED);
20 setup_wdt(WDT_OFF);
21 setup_timer_0(RTCC_INTERNAL);
22 setup_timer_1(T1_DISABLED);
23 setup_timer_2(T2_DISABLED,0,1);
24 setup_ccp1(CCP_OFF);
25 setup_comparator(NC_NC_NC_NC);
26 setup_vref(FALSE);
27  
1745 kakl 28 TDC_reset();
1886 kakl 29  
1759 kakl 30 output_low(START);
31 output_low(STOP1);
32 output_low(STOP2);
1815 kaklik 33  
34 delay_ms(50);
35  
1886 kakl 36 /*
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
39  
40  
41  
42  
43  
44  
45 */
1815 kaklik 46  
1886 kakl 47 int32 ble,ret32;
48 int16 ret16;
49 int8 ret8;
1815 kaklik 50  
1886 kakl 51 while(TRUE)
1744 kakl 52 {
1886 kakl 53 TDC_reset();
54 delay_ms(100);
55  
56 //----------------------------------------------- Nastaveni registru
57 output_low(TDC_ENABLE);
58 ble=0;
59 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;
61 spi_xfer(TDC_stream,ble,32);
62 output_high(TDC_ENABLE);
63  
64 output_low(TDC_ENABLE);
65 ble=0;
66 ble=(8<<28)|(1<<24);
67 ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0;
68 spi_xfer(TDC_stream,ble,32);
69 output_high(TDC_ENABLE);
1815 kaklik 70  
1886 kakl 71 output_low(TDC_ENABLE);
72 ble=0;
73 ble=(8<<28)|(2<<24);
74 ble|=(1<<21)|(1<<20)|(1<<19)|0;
75 spi_xfer(TDC_stream,ble,32);
76 output_high(TDC_ENABLE);
77  
78 output_low(TDC_ENABLE);
79 ble=0;
80 ble=(8<<28)|(3<<24);
81 ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0;
82 spi_xfer(TDC_stream,ble,32);
83 output_high(TDC_ENABLE);
84  
85 output_low(TDC_ENABLE);
86 ble=0;
87 ble=(8<<28)|(4<<24);
88 ble|=(4<<19)|0;
89 spi_xfer(TDC_stream,ble,32);
90 output_high(TDC_ENABLE);
91  
92 output_low(TDC_ENABLE);
93 ble=0;
94 ble=(8<<28)|(5<<24);
95 ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0;
96 spi_xfer(TDC_stream,ble,32);
97 output_high(TDC_ENABLE);
1798 kaklik 98  
1886 kakl 99  
100  
101 //----------------------------------------------- Vypis registru
102 output_low(TDC_ENABLE);
103 ret8=0;
104 ret8=(0b1011<<4)|0;
105 spi_xfer(TDC_stream,ret8,8);
106 ret32=spi_xfer(TDC_stream,0,32);
107 output_high(TDC_ENABLE);
108 printf("- %LX ", ret32);
109  
110 output_low(TDC_ENABLE);
111 ret8=0;
112 ret8=(0b1011<<4)|1;
113 spi_xfer(TDC_stream,ret8,8);
114 ret32=spi_xfer(TDC_stream,0,32);
115 output_high(TDC_ENABLE);
116 printf("%LX ", ret32);
117  
118 output_low(TDC_ENABLE);
119 ret8=0;
120 ret8=(0b1011<<4)|2;
121 spi_xfer(TDC_stream,ret8,8);
122 ret32=spi_xfer(TDC_stream,0,32);
123 output_high(TDC_ENABLE);
124 printf("%LX ", ret32);
125  
126 output_low(TDC_ENABLE);
127 ret8=0;
128 ret8=(0b1011<<4)|3;
129 spi_xfer(TDC_stream,ret8,8);
130 ret32=spi_xfer(TDC_stream,0,32);
131 output_high(TDC_ENABLE);
132 printf("%LX ", ret32);
133  
134 output_low(TDC_ENABLE);
135 ret8=0;
136 ret8=(0b1011<<4)|4;
137 spi_xfer(TDC_stream,ret8,8);
138 ret16=spi_xfer(TDC_stream,0,16);
139 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);
141  
142 output_low(TDC_ENABLE);
143 ret8=0;
144 ret8=(0b1011<<4)|5;
145 spi_xfer(TDC_stream,ret8,8);
146 ret8=spi_xfer(TDC_stream,0,8);
147 output_high(TDC_ENABLE);
148 printf("%X\r\n", ret8);
149  
150 //----------------------------------------------- Mereni
151  
152 TDC_init();
153  
1759 kakl 154 delay_ms(50);
1886 kakl 155  
156 TDC_start_cycle();
157  
158 delay_ms(200);
159  
160 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H)
161  
1759 kakl 162 output_high(START);
1886 kakl 163 output_low(START);
164 delay_us(1);
1798 kaklik 165  
1886 kakl 166 output_high(STOP1);
167 output_low(STOP1);
168 delay_us(1);
1759 kakl 169  
1886 kakl 170  
1815 kaklik 171 output_high(STOP1);
1886 kakl 172 output_low(STOP1);
173 delay_us(1);
174  
175 output_high(STOP1);
176 output_low(STOP1);
177 delay_us(1);
178  
179  
180  
181 //----------------------------------------------- Pocitani
182 int32 nn;
183 for(nn=3;nn<=5;nn++)
184 {
185 delay_ms(500);
186  
187 output_low(TDC_ENABLE);
188 ret8=0;
189 ret8=(0b1011<<4)|0;
190 spi_xfer(TDC_stream,ret8,8);
191 ret32=spi_xfer(TDC_stream,0,32);
192 output_high(TDC_ENABLE);
193 printf("* %LX ", ret32);
194  
195 output_low(TDC_ENABLE);
196 ret8=0;
197 ret8=(0b1011<<4)|1;
198 spi_xfer(TDC_stream,ret8,8);
199 ret32=spi_xfer(TDC_stream,0,32);
200 output_high(TDC_ENABLE);
201 printf("%LX ", ret32);
202  
203 output_low(TDC_ENABLE);
204 ret8=0;
205 ret8=(0b1011<<4)|2;
206 spi_xfer(TDC_stream,ret8,8);
207 ret32=spi_xfer(TDC_stream,0,32);
208 output_high(TDC_ENABLE);
209 printf("%LX ", ret32);
210  
211 output_low(TDC_ENABLE);
212 ret8=0;
213 ret8=(0b1011<<4)|3;
214 spi_xfer(TDC_stream,ret8,8);
215 ret32=spi_xfer(TDC_stream,0,32);
216 output_high(TDC_ENABLE);
217 printf("%LX ", ret32);
218  
219 output_low(TDC_ENABLE);
220 ret8=0;
221 ret8=(0b1011<<4)|4;
222 spi_xfer(TDC_stream,ret8,8);
223 ret16=spi_xfer(TDC_stream,0,16);
224 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);
226  
227 output_low(TDC_ENABLE);
228 ret8=0;
229 ret8=(0b1011<<4)|5;
230 spi_xfer(TDC_stream,ret8,8);
231 ret8=spi_xfer(TDC_stream,0,8);
232 output_high(TDC_ENABLE);
233 printf("%X\r\n", ret8);
234  
235 // Next calculation
236 output_low(TDC_ENABLE);
237 ble=0;
238 ble=(8<<28)|(1<<24);
239 ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00;
240 spi_xfer(TDC_stream,ble,32);
241 output_high(TDC_ENABLE);
242  
243 }
244  
245 }
1759 kakl 246  
1744 kakl 247 }