1369 |
kakl |
1 |
#include "main.h" |
|
|
2 |
#include "string.h" |
|
|
3 |
|
|
|
4 |
#define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) |
|
|
5 |
#define EEPROM_Access 0x20 // EEPROM access command |
|
|
6 |
#define EEPROM_Addr 0x0E // I2CAddr address in the eeprom |
|
|
7 |
#define RAM_Access 0x00 // RAM access command |
|
|
8 |
#define RAM_Tobj1 0x07 // To1 address in the ram |
|
|
9 |
#define RAM_Tamb 0x06 // Ta address in the ram |
|
|
10 |
#define MAXHEAT 60 // Number of cycles for heating |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
BYTE gethex1() { |
|
|
14 |
char digit; |
|
|
15 |
|
|
|
16 |
digit = getc(); |
|
|
17 |
|
|
|
18 |
putc(digit); |
|
|
19 |
|
|
|
20 |
if(digit<='9') |
|
|
21 |
return(digit-'0'); |
|
|
22 |
else |
|
|
23 |
return((toupper(digit)-'A')+10); |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
BYTE gethex() { |
|
|
27 |
unsigned int8 lo,hi; |
|
|
28 |
|
|
|
29 |
hi = gethex1(); |
|
|
30 |
lo = gethex1(); |
|
|
31 |
if(lo==0xdd) |
|
|
32 |
return(hi); |
|
|
33 |
else |
|
|
34 |
return( hi*16+lo ); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
int16 sonar_ping (int8 addr) |
|
|
38 |
{ |
|
|
39 |
int16 distance; |
|
|
40 |
int8 pom; |
|
|
41 |
|
|
|
42 |
i2c_start(); // So Sonar Ping |
|
|
43 |
i2c_write(addr); |
|
|
44 |
i2c_write(0x0); |
|
|
45 |
i2c_write(0x51); // 50 mereni v palcich, 51 mereni v cm, 52 v us |
|
|
46 |
i2c_stop(); |
|
|
47 |
delay_ms(70); |
|
|
48 |
i2c_start(); // Read Reflection |
|
|
49 |
i2c_write(addr); |
|
|
50 |
i2c_write(0x2); |
|
|
51 |
i2c_stop(); |
|
|
52 |
i2c_start(); |
|
|
53 |
i2c_write(addr+1); |
|
|
54 |
pom=i2c_read(1); |
|
|
55 |
distance=MAKE16(pom,i2c_read(0)); |
|
|
56 |
i2c_stop(); |
|
|
57 |
|
|
|
58 |
return distance; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation |
|
|
62 |
{ |
|
|
63 |
unsigned char crc[6]; |
|
|
64 |
unsigned char BitPosition=47; |
|
|
65 |
unsigned char shift; |
|
|
66 |
unsigned char i; |
|
|
67 |
unsigned char j; |
|
|
68 |
unsigned char temp; |
|
|
69 |
|
|
|
70 |
do |
|
|
71 |
{ |
|
|
72 |
crc[5]=0; /* Load CRC value 0x000000000107 */ |
|
|
73 |
crc[4]=0; |
|
|
74 |
crc[3]=0; |
|
|
75 |
crc[2]=0; |
|
|
76 |
crc[1]=0x01; |
|
|
77 |
crc[0]=0x07; |
|
|
78 |
BitPosition=47; /* Set maximum bit position at 47 */ |
|
|
79 |
shift=0; |
|
|
80 |
|
|
|
81 |
//Find first 1 in the transmited message |
|
|
82 |
i=5; /* Set highest index */ |
|
|
83 |
j=0; |
|
|
84 |
while((pec[i]&(0x80>>j))==0 && i>0) |
|
|
85 |
{ |
|
|
86 |
BitPosition--; |
|
|
87 |
if(j<7) |
|
|
88 |
{ |
|
|
89 |
j++; |
|
|
90 |
} |
|
|
91 |
else |
|
|
92 |
{ |
|
|
93 |
j=0x00; |
|
|
94 |
i--; |
|
|
95 |
} |
|
|
96 |
}/*End of while */ |
|
|
97 |
|
|
|
98 |
shift=BitPosition-8; /*Get shift value for crc value*/ |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
//Shift crc value |
|
|
102 |
while(shift) |
|
|
103 |
{ |
|
|
104 |
for(i=5; i<0xFF; i--) |
|
|
105 |
{ |
|
|
106 |
if((crc[i-1]&0x80) && (i>0)) |
|
|
107 |
{ |
|
|
108 |
temp=1; |
|
|
109 |
} |
|
|
110 |
else |
|
|
111 |
{ |
|
|
112 |
temp=0; |
|
|
113 |
} |
|
|
114 |
crc[i]<<=1; |
|
|
115 |
crc[i]+=temp; |
|
|
116 |
}/*End of for*/ |
|
|
117 |
shift--; |
|
|
118 |
}/*End of while*/ |
|
|
119 |
|
|
|
120 |
//Exclusive OR between pec and crc |
|
|
121 |
for(i=0; i<=5; i++) |
|
|
122 |
{ |
|
|
123 |
pec[i] ^=crc[i]; |
|
|
124 |
}/*End of for*/ |
|
|
125 |
} while(BitPosition>8);/*End of do-while*/ |
|
|
126 |
|
|
|
127 |
return pec[0]; |
|
|
128 |
}/*End of PEC_calculation*/ |
|
|
129 |
|
|
|
130 |
int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM |
|
|
131 |
{ |
|
|
132 |
unsigned char arr[6]; // Buffer for the sent bytes |
|
|
133 |
int8 crc; // Readed CRC |
|
|
134 |
int16 temp; // Readed temperature |
|
|
135 |
|
|
|
136 |
i2c_stop(); |
|
|
137 |
i2c_start(); |
|
|
138 |
i2c_write(addr); |
|
|
139 |
i2c_write(RAM_Access|select); // Select the teperature sensor in device |
|
|
140 |
i2c_start(); |
|
|
141 |
i2c_write(addr); |
|
|
142 |
arr[2]=i2c_read(1); // lo |
|
|
143 |
arr[1]=i2c_read(1); // hi |
|
|
144 |
temp=MAKE16(arr[1],arr[2]); |
|
|
145 |
crc=i2c_read(0); //crc |
|
|
146 |
i2c_stop(); |
|
|
147 |
|
|
|
148 |
arr[5]=addr; |
|
|
149 |
arr[4]=RAM_Access|select; |
|
|
150 |
arr[3]=addr; |
|
|
151 |
arr[0]=0; |
|
|
152 |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
|
|
153 |
|
|
|
154 |
return temp; |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
int16 ReadEeprom(int8 addr, int8 select) |
|
|
158 |
{ |
|
|
159 |
unsigned char arr[6]; // Buffer for the sent bytes |
|
|
160 |
int8 crc; // Readed CRC |
|
|
161 |
int16 temp; // Readed temperature |
|
|
162 |
|
|
|
163 |
i2c_stop(); |
|
|
164 |
i2c_start(); |
|
|
165 |
i2c_write(addr); |
|
|
166 |
i2c_write(EEPROM_Access|select); // Select the teperature sensor in device |
|
|
167 |
i2c_start(); |
|
|
168 |
i2c_write(addr); |
|
|
169 |
arr[2]=i2c_read(1); // lo |
|
|
170 |
arr[1]=i2c_read(1); // hi |
|
|
171 |
temp=MAKE16(arr[1],arr[2]); |
|
|
172 |
crc=i2c_read(0); //crc |
|
|
173 |
i2c_stop(); |
|
|
174 |
|
|
|
175 |
arr[5]=addr; |
|
|
176 |
arr[4]=EEPROM_Access|select; |
|
|
177 |
arr[3]=addr; |
|
|
178 |
arr[0]=0; |
|
|
179 |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
|
|
180 |
|
|
|
181 |
return temp; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
void WriteEeprom(int8 addr, int8 select, int16 value) |
|
|
185 |
{ |
|
|
186 |
unsigned char arr[6]; // Buffer for the sent bytes |
|
|
187 |
byte crc,hi,lo; |
|
|
188 |
|
|
|
189 |
hi=MAKE8(value,1); |
|
|
190 |
lo=MAKE8(value,0); |
|
|
191 |
|
|
|
192 |
arr[0]=0; |
|
|
193 |
arr[1]=hi; |
|
|
194 |
arr[2]=lo; |
|
|
195 |
arr[3]=EEPROM_Access|select; |
|
|
196 |
arr[4]=addr; |
|
|
197 |
arr[5]=0; |
|
|
198 |
|
|
|
199 |
crc=PEC_calculation(arr); |
|
|
200 |
|
|
|
201 |
i2c_stop(); |
|
|
202 |
i2c_start(); |
|
|
203 |
i2c_write(addr); |
|
|
204 |
i2c_write(EEPROM_Access|select); |
|
|
205 |
i2c_write(lo); |
|
|
206 |
i2c_write(hi); |
|
|
207 |
i2c_write(crc); |
|
|
208 |
i2c_stop(); |
|
|
209 |
|
|
|
210 |
printf("CRC: %X \n\r",crc); |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
void setadresstemp() |
|
|
214 |
{ |
|
|
215 |
int newaddr=0; |
|
|
216 |
|
|
|
217 |
printf("Zadej adresu (Hex):"); |
|
|
218 |
newaddr=gethex(); |
|
|
219 |
putc('\n'); |
|
|
220 |
putc('\r'); |
|
|
221 |
|
|
|
222 |
WriteEeprom(SA,EEPROM_Addr,0); |
|
|
223 |
delay_ms(8); |
|
|
224 |
WriteEeprom(SA,EEPROM_Addr,newaddr); |
|
|
225 |
delay_ms(8); |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
void main() |
|
|
229 |
{ |
|
|
230 |
char c; |
|
|
231 |
unsigned int16 temp, tempa; |
|
|
232 |
signed int16 ta, to; |
|
|
233 |
byte addr, addr_new; |
|
|
234 |
|
|
|
235 |
setup_adc_ports(NO_ANALOGS); |
|
|
236 |
setup_adc(ADC_OFF); |
|
|
237 |
// setup_psp(PSP_DISABLED); |
|
|
238 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
239 |
setup_timer_1(T1_DISABLED); |
|
|
240 |
setup_timer_2(T2_DISABLED, 0, 1); |
|
|
241 |
setup_oscillator(OSC_4MHZ|OSC_INTRC); |
|
|
242 |
|
|
|
243 |
delay_ms(100); |
|
|
244 |
|
|
|
245 |
printf("\n\r SETCIDLA - Martin Poviser 2008 \n\r"); |
|
|
246 |
|
|
|
247 |
while(true) |
|
|
248 |
{ |
|
|
249 |
printf("k - zapsani adresy do teplotniho cidla\n\r"); |
|
|
250 |
printf("o - precteni adresy teplotniho cidla\n\r"); |
|
|
251 |
printf("r - precteni teploty teplotniho cidla\n\r"); |
|
|
252 |
printf("t - precteni teploty teplotniho cidla z adresy 0\n\r"); |
|
|
253 |
printf("s - precteni vzdalenosti ze sonaru\n\r"); |
|
|
254 |
printf("c - zmena adresy sonaru\n\r"); |
|
|
255 |
printf("?"); |
|
|
256 |
c=getc(); |
|
|
257 |
putc('\n'); |
|
|
258 |
putc('\r'); |
|
|
259 |
|
|
|
260 |
switch(c) |
|
|
261 |
{ |
|
|
262 |
case 'k': |
|
|
263 |
setadresstemp(); |
|
|
264 |
break; |
|
|
265 |
|
|
|
266 |
case 'r': |
|
|
267 |
printf("Zadej adresu (Hex):"); |
|
|
268 |
addr=gethex()<<1; |
|
|
269 |
putc('\n'); |
|
|
270 |
putc('\r'); |
|
|
271 |
|
|
|
272 |
tempa=ReadTemp(addr, RAM_Tamb); // Read temperatures from sensor |
|
|
273 |
temp=ReadTemp(addr, RAM_Tobj1); |
|
|
274 |
|
|
|
275 |
to=(signed int16)(temp*2-27315); |
|
|
276 |
ta=(signed int16)(tempa*2-27315); |
|
|
277 |
|
|
|
278 |
printf("-- %.2g %.2g \n\r",(float)ta/100,(float)to/100); |
|
|
279 |
break; |
|
|
280 |
|
|
|
281 |
case 't': |
|
|
282 |
while(!kbhit()) |
|
|
283 |
{ |
|
|
284 |
tempa=ReadTemp(0, RAM_Tamb); // Read temperatures from sensor |
|
|
285 |
temp=ReadTemp(0, RAM_Tobj1); |
|
|
286 |
|
|
|
287 |
to=(signed int16)(temp*2-27315); |
|
|
288 |
ta=(signed int16)(tempa*2-27315); |
|
|
289 |
|
|
|
290 |
printf("-- %.2g %.2g %.2g\n\r",(float)ta/100,(float)to/100, (float)(ta-to)/100); |
|
|
291 |
delay_ms(1100); |
|
|
292 |
}; |
|
|
293 |
getc(); |
|
|
294 |
break; |
|
|
295 |
|
|
|
296 |
case 'o': |
|
|
297 |
printf("Adresa: %X \n\r",ReadEeprom(SA,EEPROM_Addr)); |
|
|
298 |
break; |
|
|
299 |
|
|
|
300 |
case 's': |
|
|
301 |
printf("Zadej adresu (Hex):"); |
|
|
302 |
addr=gethex(); |
|
|
303 |
putc('\n'); |
|
|
304 |
putc('\r'); |
|
|
305 |
|
|
|
306 |
printf("Distance [cm]: %Lu \n\r", sonar_ping(addr)); |
|
|
307 |
break; |
|
|
308 |
|
|
|
309 |
case 'c': |
|
|
310 |
printf("Zadej starou adresu (Hex):"); |
|
|
311 |
addr=gethex(); |
|
|
312 |
putc('\n'); |
|
|
313 |
putc('\r'); |
|
|
314 |
printf("Zadej novou adresu (Hex):"); |
|
|
315 |
addr_new=gethex(); |
|
|
316 |
putc('\n'); |
|
|
317 |
putc('\r'); |
|
|
318 |
|
|
|
319 |
i2c_start(); |
|
|
320 |
i2c_write(addr); |
|
|
321 |
i2c_write(0x0); |
|
|
322 |
i2c_write(0xA0); // Change address sequence |
|
|
323 |
i2c_stop(); |
|
|
324 |
|
|
|
325 |
i2c_start(); |
|
|
326 |
i2c_write(addr); |
|
|
327 |
i2c_write(0x0); |
|
|
328 |
i2c_write(0xAA); // Change address sequence |
|
|
329 |
i2c_stop(); |
|
|
330 |
|
|
|
331 |
i2c_start(); |
|
|
332 |
i2c_write(addr); |
|
|
333 |
i2c_write(0x0); |
|
|
334 |
i2c_write(0xA5); // Change address sequence |
|
|
335 |
i2c_stop(); |
|
|
336 |
|
|
|
337 |
i2c_start(); |
|
|
338 |
i2c_write(addr); |
|
|
339 |
i2c_write(0x0); |
|
|
340 |
i2c_write(addr_new); // New address |
|
|
341 |
i2c_stop(); |
|
|
342 |
break; |
|
|
343 |
} |
|
|
344 |
} |
|
|
345 |
} |
|
|
346 |
|