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