| Line 3... |
Line 3... |
| 3 |
#define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) |
3 |
#define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) |
| 4 |
#define RAM_Access 0x00 // RAM access command |
4 |
#define RAM_Access 0x00 // RAM access command |
| 5 |
#define RAM_Tobj1 0x07 // To1 address in the RAM |
5 |
#define RAM_Tobj1 0x07 // To1 address in the RAM |
| 6 |
#define RAM_Tamb 0x06 // Ta address in the RAM |
6 |
#define RAM_Tamb 0x06 // Ta address in the RAM |
| 7 |
|
7 |
|
| - |
|
8 |
|
| 8 |
//*High and Low level of clock |
9 |
//*High and Low level of clock |
| 9 |
#define HIGHLEV 40 // max. 50us |
10 |
#define HIGHLEV 40 // max. 50us |
| 10 |
#define LOWLEV 100 // max. 30ms |
11 |
#define LOWLEV 100 // max. 30ms |
| 11 |
#define TBUF 20 |
12 |
#define TBUF 20 |
| 12 |
|
13 |
|
| Line 32... |
Line 33... |
| 32 |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
33 |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
| 33 |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
34 |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
| 34 |
//********************************************************************************************** |
35 |
//********************************************************************************************** |
| 35 |
void SMB_START_bit(void) |
36 |
void SMB_START_bit(void) |
| 36 |
{ |
37 |
{ |
| 37 |
disable_interrupts(GLOBAL); |
38 |
// disable_interrupts(GLOBAL); |
| 38 |
mSDA_HIGH(); // Set SDA line |
39 |
mSDA_HIGH(); // Set SDA line |
| 39 |
delay_us( TBUF ); // Wait a few microseconds |
40 |
delay_us( TBUF ); // Wait a few microseconds |
| 40 |
mSCL_HIGH(); // Set SCL line |
41 |
mSCL_HIGH(); // Set SCL line |
| 41 |
delay_us( TBUF ); // Generate bus free time between Stop |
42 |
delay_us( TBUF ); // Generate bus free time between Stop |
| 42 |
// and Start condition (Tbuf=4.7us min) |
43 |
// and Start condition (Tbuf=4.7us min) |
| 43 |
mSDA_LOW(); // Clear SDA line |
44 |
mSDA_LOW(); // Clear SDA line |
| 44 |
delay_us( TBUF ); // Hold time after (Repeated) Start |
45 |
delay_us( TBUF ); // Hold time after (Repeated) Start |
| 45 |
// Condition. After this period, the first clock is generated. |
46 |
// Condition. After this period, the first clock is generated. |
| 46 |
//(Thd:sta=4.0us min) |
47 |
//(Thd:sta=4.0us min) |
| 47 |
mSCL_LOW(); // Clear SCL line |
48 |
mSCL_LOW(); // Clear SCL line |
| 48 |
enable_interrupts(GLOBAL); |
49 |
// enable_interrupts(GLOBAL); |
| 49 |
delay_us( TBUF ); // Wait a few microseconds |
50 |
delay_us( TBUF ); // Wait a few microseconds |
| - |
|
51 |
|
| - |
|
52 |
toggle_dome(); |
| 50 |
} |
53 |
} |
| 51 |
//********************************************************************************************* |
54 |
//********************************************************************************************* |
| 52 |
// STOP CONDITION ON SMBus |
55 |
// STOP CONDITION ON SMBus |
| 53 |
//********************************************************************************************* |
56 |
//********************************************************************************************* |
| 54 |
//Name: STOPbit |
57 |
//Name: STOPbit |
| Line 58... |
Line 61... |
| 58 |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
61 |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
| 59 |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
62 |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
| 60 |
//********************************************************************************************* |
63 |
//********************************************************************************************* |
| 61 |
void SMB_STOP_bit(void) |
64 |
void SMB_STOP_bit(void) |
| 62 |
{ |
65 |
{ |
| 63 |
disable_interrupts(GLOBAL); |
66 |
// disable_interrupts(GLOBAL); |
| 64 |
mSDA_HIGH(); |
67 |
mSDA_HIGH(); |
| 65 |
mSCL_LOW(); // Clear SCL line |
68 |
mSCL_LOW(); // Clear SCL line |
| 66 |
delay_us( TBUF ); // Wait a few microseconds |
69 |
delay_us( TBUF ); // Wait a few microseconds |
| 67 |
mSDA_LOW(); // Clear SDA line |
70 |
mSDA_LOW(); // Clear SDA line |
| 68 |
delay_us( TBUF ); // Wait a few microseconds |
71 |
delay_us( TBUF ); // Wait a few microseconds |
| 69 |
mSCL_HIGH(); // Set SCL line |
72 |
mSCL_HIGH(); // Set SCL line |
| 70 |
delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min) |
73 |
delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min) |
| 71 |
mSDA_HIGH(); // Set SDA line |
74 |
mSDA_HIGH(); // Set SDA line |
| 72 |
enable_interrupts(GLOBAL); |
75 |
// enable_interrupts(GLOBAL); |
| - |
|
76 |
|
| - |
|
77 |
toggle_dome(); |
| 73 |
} |
78 |
} |
| 74 |
|
79 |
|
| 75 |
|
80 |
|
| 76 |
void SMB_send_bit(unsigned char bit_out) |
81 |
void SMB_send_bit(unsigned char bit_out) |
| 77 |
{ |
82 |
{ |
| 78 |
disable_interrupts(GLOBAL); |
83 |
// disable_interrupts(GLOBAL); |
| 79 |
if(bit_out==0) {mSDA_LOW();} |
84 |
if(bit_out==0) {mSDA_LOW();} |
| 80 |
else {mSDA_HIGH();} |
85 |
else {mSDA_HIGH();} |
| 81 |
delay_us(3); |
86 |
delay_us(3); |
| 82 |
mSCL_HIGH(); // Set SCL line |
87 |
mSCL_HIGH(); // Set SCL line |
| 83 |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
88 |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
| 84 |
mSCL_LOW(); // Clear SCL line |
89 |
mSCL_LOW(); // Clear SCL line |
| 85 |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
90 |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
| 86 |
// mSDA_HIGH(); // Master release SDA line , |
91 |
// mSDA_HIGH(); // Master release SDA line , |
| 87 |
enable_interrupts(GLOBAL); |
92 |
// enable_interrupts(GLOBAL); |
| - |
|
93 |
|
| - |
|
94 |
toggle_dome(); |
| 88 |
return; |
95 |
return; |
| 89 |
} |
96 |
} |
| 90 |
|
97 |
|
| 91 |
unsigned char SMB_Receive_bit(void) |
98 |
unsigned char SMB_Receive_bit(void) |
| 92 |
{ |
99 |
{ |
| 93 |
unsigned char Ack_bit; |
100 |
unsigned char Ack_bit; |
| 94 |
|
101 |
|
| 95 |
disable_interrupts(GLOBAL); |
102 |
// disable_interrupts(GLOBAL); |
| 96 |
mSDA_HIGH(); //_SDA_IO=1; // SDA-input |
103 |
mSDA_HIGH(); //_SDA_IO=1; // SDA-input |
| 97 |
mSCL_HIGH(); // Set SCL line |
104 |
mSCL_HIGH(); // Set SCL line |
| 98 |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
105 |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
| 99 |
if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit |
106 |
if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit |
| 100 |
else Ack_bit=0; // / |
107 |
else Ack_bit=0; // / |
| 101 |
mSCL_LOW(); // Clear SCL line |
108 |
mSCL_LOW(); // Clear SCL line |
| 102 |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
109 |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
| 103 |
enable_interrupts(GLOBAL); |
110 |
// enable_interrupts(GLOBAL); |
| 104 |
|
111 |
|
| - |
|
112 |
toggle_dome(); |
| 105 |
return Ack_bit; |
113 |
return Ack_bit; |
| 106 |
} |
114 |
} |
| 107 |
|
115 |
|
| 108 |
|
116 |
|
| 109 |
//********************************************************************************************* |
117 |
//********************************************************************************************* |
| Line 131... |
Line 139... |
| 131 |
} |
139 |
} |
| 132 |
|
140 |
|
| 133 |
Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit |
141 |
Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit |
| 134 |
|
142 |
|
| 135 |
return Ack_bit; |
143 |
return Ack_bit; |
| 136 |
}// End of TX_bite() |
144 |
} |
| 137 |
|
145 |
|
| 138 |
//********************************************************************************************* |
146 |
//********************************************************************************************* |
| 139 |
// RECEIVE DATA ON SMBus |
147 |
// RECEIVE DATA ON SMBus |
| 140 |
//********************************************************************************************* |
148 |
//********************************************************************************************* |
| 141 |
//Name: RX_byte |
149 |
//Name: RX_byte |
| Line 202... |
Line 210... |
| 202 |
else |
210 |
else |
| 203 |
{ |
211 |
{ |
| 204 |
j=0x00; |
212 |
j=0x00; |
| 205 |
i--; |
213 |
i--; |
| 206 |
} |
214 |
} |
| 207 |
}/*End of while */ |
215 |
} |
| 208 |
|
216 |
|
| 209 |
shift=BitPosition-8; /*Get shift value for crc value*/ |
217 |
shift=BitPosition-8; /*Get shift value for crc value*/ |
| 210 |
|
218 |
|
| 211 |
|
219 |
|
| 212 |
//Shift crc value |
220 |
//Shift crc value |
| Line 222... |
Line 230... |
| 222 |
{ |
230 |
{ |
| 223 |
temp=0; |
231 |
temp=0; |
| 224 |
} |
232 |
} |
| 225 |
crc[i]<<=1; |
233 |
crc[i]<<=1; |
| 226 |
crc[i]+=temp; |
234 |
crc[i]+=temp; |
| 227 |
}/*End of for*/ |
235 |
} |
| 228 |
shift--; |
236 |
shift--; |
| 229 |
}/*End of while*/ |
237 |
} |
| 230 |
|
238 |
|
| 231 |
//Exclusive OR between pec and crc |
239 |
//Exclusive OR between pec and crc |
| 232 |
for(i=0; i<=5; i++) |
240 |
for(i=0; i<=5; i++) |
| 233 |
{ |
241 |
{ |
| 234 |
pec[i] ^=crc[i]; |
242 |
pec[i] ^=crc[i]; |
| 235 |
}/*End of for*/ |
243 |
} |
| 236 |
} while(BitPosition>8);/*End of do-while*/ |
244 |
} while(BitPosition>8);/*End of do-while*/ |
| 237 |
|
245 |
|
| 238 |
return pec[0]; |
246 |
return pec[0]; |
| 239 |
}/*End of PEC_calculation*/ |
- |
|
| 240 |
|
- |
|
| 241 |
int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM |
- |
|
| 242 |
{ |
- |
|
| 243 |
unsigned char arr[6]; // Buffer for the sent bytes |
- |
|
| 244 |
int8 crc; // Readed CRC |
- |
|
| 245 |
int16 temp; // Readed temperature |
- |
|
| 246 |
|
- |
|
| 247 |
addr<<=1; |
- |
|
| 248 |
|
- |
|
| 249 |
SMB_STOP_bit(); //If slave send NACK stop comunication |
- |
|
| 250 |
SMB_START_bit(); //Start condition |
- |
|
| 251 |
SMB_TX_byte(addr); |
- |
|
| 252 |
SMB_TX_byte(RAM_Access|select); |
- |
|
| 253 |
SMB_START_bit(); //Repeated Start condition |
- |
|
| 254 |
SMB_TX_byte(addr); |
- |
|
| 255 |
arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK |
- |
|
| 256 |
arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK |
- |
|
| 257 |
temp=MAKE16(arr[1],arr[2]); |
- |
|
| 258 |
crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK |
- |
|
| 259 |
SMB_STOP_bit(); //Stop condition |
- |
|
| 260 |
|
- |
|
| 261 |
arr[5]=addr; |
- |
|
| 262 |
arr[4]=RAM_Access|select; |
- |
|
| 263 |
arr[3]=addr; |
- |
|
| 264 |
arr[0]=0; |
- |
|
| 265 |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
- |
|
| 266 |
|
- |
|
| 267 |
return temp; |
- |
|
| 268 |
} |
- |
|
| 269 |
|
- |
|
| 270 |
void main() |
- |
|
| 271 |
{ |
- |
|
| 272 |
unsigned int16 temp, tempa; |
- |
|
| 273 |
signed int16 ta, to; |
- |
|
| 274 |
|
- |
|
| 275 |
setup_adc_ports(NO_ANALOGS); |
- |
|
| 276 |
setup_adc(ADC_OFF); |
- |
|
| 277 |
setup_psp(PSP_DISABLED); |
- |
|
| 278 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
- |
|
| 279 |
setup_timer_1(T1_DISABLED); |
- |
|
| 280 |
setup_timer_2(T2_DISABLED,0,1); |
- |
|
| 281 |
|
- |
|
| 282 |
output_low(KLAKSON); // Ticho |
- |
|
| 283 |
output_high(LED); // Blik |
- |
|
| 284 |
delay_ms(50); |
- |
|
| 285 |
output_low(LED); |
- |
|
| 286 |
printf("\n\r\n\rVER: %s\n\r\n\r", VER); // Vypis verzi |
- |
|
| 287 |
|
- |
|
| 288 |
enable_interrupts(INT_RDA); |
- |
|
| 289 |
enable_interrupts(GLOBAL); |
- |
|
| 290 |
|
- |
|
| 291 |
flag=false; |
- |
|
| 292 |
|
- |
|
| 293 |
while (true) |
- |
|
| 294 |
{ |
- |
|
| 295 |
float ta1, ta2, to1, to2; |
- |
|
| 296 |
int16 s1, s2, s3, s4, s5, s6; |
- |
|
| 297 |
int8 c; |
- |
|
| 298 |
int8 tlacitko; |
- |
|
| 299 |
|
- |
|
| 300 |
if (flag) |
- |
|
| 301 |
{ |
- |
|
| 302 |
flag=false; |
- |
|
| 303 |
|
- |
|
| 304 |
output_high(KLAKSON); |
- |
|
| 305 |
delay_ms(400); |
- |
|
| 306 |
output_low(KLAKSON); |
- |
|
| 307 |
delay_ms(100); |
- |
|
| 308 |
output_high(KLAKSON); |
- |
|
| 309 |
delay_ms(700); |
- |
|
| 310 |
output_low(KLAKSON); |
- |
|
| 311 |
} |
- |
|
| 312 |
|
- |
|
| 313 |
tlacitko=0; |
- |
|
| 314 |
|
- |
|
| 315 |
tempa=ReadTemp(1, RAM_Tamb); // Read temperatures from sensor |
- |
|
| 316 |
temp=ReadTemp(1, RAM_Tobj1); |
- |
|
| 317 |
to=(signed int16)(temp*2-27315); |
- |
|
| 318 |
ta=(signed int16)(tempa*2-27315); |
- |
|
| 319 |
ta1=(float)ta/100; |
- |
|
| 320 |
to1=(float)to/100; |
- |
|
| 321 |
|
- |
|
| 322 |
if(!input(TL)) tlacitko=1; |
- |
|
| 323 |
|
- |
|
| 324 |
tempa=ReadTemp(2, RAM_Tamb); // Read temperatures from sensor |
- |
|
| 325 |
temp=ReadTemp(2, RAM_Tobj1); |
- |
|
| 326 |
to=(signed int16)(temp*2-27315); |
- |
|
| 327 |
ta=(signed int16)(tempa*2-27315); |
- |
|
| 328 |
ta2=(float)ta/100; |
- |
|
| 329 |
to2=(float)to/100; |
- |
|
| 330 |
// printf("T2 %.1g %.1g ",(float)ta/100,(float)to/100); |
- |
|
| 331 |
|
- |
|
| 332 |
// printf("S1 %Lu ", sonar_ping(SONAR1)); |
- |
|
| 333 |
if(!input(TL)) tlacitko=1; |
- |
|
| 334 |
output_high(LED); |
- |
|
| 335 |
s1=sonar_ping(SONAR1); |
- |
|
| 336 |
output_low(LED); |
- |
|
| 337 |
if(!input(TL)) tlacitko=1; |
- |
|
| 338 |
s2=sonar_ping(SONAR2); |
- |
|
| 339 |
if(!input(TL)) tlacitko=1; |
- |
|
| 340 |
s3=sonar_ping(SONAR3); |
- |
|
| 341 |
if(!input(TL)) tlacitko=1; |
- |
|
| 342 |
s4=sonar_ping(SONAR4); |
- |
|
| 343 |
if(!input(TL)) tlacitko=1; |
- |
|
| 344 |
s5=sonar_ping(SONAR5); |
- |
|
| 345 |
if(!input(TL)) tlacitko=1; |
- |
|
| 346 |
s6=sonar_ping(SONAR6); |
- |
|
| 347 |
if(!input(TL)) tlacitko=1; |
- |
|
| 348 |
c=cmps_azimuth(); |
- |
|
| 349 |
if(!input(TL)) tlacitko=1; |
- |
|
| 350 |
|
- |
|
| 351 |
printf("#T1 %.1g %.1g T2 %.1g %.1g ",ta1,to1,ta2,to2); |
- |
|
| 352 |
printf("S1 %Lu S2 %Lu S3 %Lu S4 %Lu S5 %Lu S6 %Lu C %u TL %u\n\r",s1,s2,s3,s4,s5,s6,c,tlacitko); |
- |
|
| 353 |
} |
- |
|
| 354 |
|
- |
|
| 355 |
} |
247 |
} |