i really need to converte this code..... I found a code in mikroBasic-Forum written in mikroPacal this code is for the ir sensor mlx90614 and I would like to convert it to mikroC because I only have the mikroC compiler. It will be possible?
the code in mikro pascal is the next:
{******************************************************************************
This program is open source, anyone and everyone can use it for educations or
whatever you like.
This is an attempt to help convert the poorly documented C-Code for the
MLX90614 IR Infared Thermal Sensor by
Melexis 'Microelectronic Integrated Systems'
over to AVR PASCAL and too help explain some of the code and addresses.
by: Robert Underwood
Email: k0ru@arrl.net
Date: April 5, 2011
Source: MikroElektronika mikroPASCAL PRO for AVR MCU
Enjoy!
Any feedback would be greatly appreciated:
===============================================================================}
program MLX90614_ATMega16_v1;
var
ReadStr, MLX_Temp, MLX_FTemp, MLX_CTemp : array[23] of char;
Sensor, MLX_LSB, MLX_MSB, MLX_PEC : Word;
MLX_Calc, MLX_F, MLX_C : Real;
CRC8 : LongInt;
const
SA : Word = 0x0000; // 0x00 calls all on bus, 0x5A is factory default address
DTemp : Word = 0x06; // Object2 Temp $08 Object1 Temp $07 Device Ambient Temp $06
OTemp1 : Word = 0x07; // Ambient Temp is actually the devices own temp
OTemp2 : Word = 0x08;
{ Declarations section }
// LCD module connections
var LCD_RS : sbit at PORTD2_bit;
var LCD_EN : sbit at PORTD3_bit;
var LCD_D4 : sbit at PORTD4_bit;
var LCD_D5 : sbit at PORTD5_bit;
var LCD_D6 : sbit at PORTD6_bit;
var LCD_D7 : sbit at PORTD7_bit;
var LCD_RS_Direction : sbit at DDD2_bit;
var LCD_EN_Direction : sbit at DDD3_bit;
var LCD_D4_Direction : sbit at DDD4_bit;
var LCD_D5_Direction : sbit at DDD5_bit;
var LCD_D6_Direction : sbit at DDD6_bit;
var LCD_D7_Direction : sbit at DDD7_bit;
// End LCD module connections
// ** Performs calculations on CRC to ensure we received a valid reading
Function CalcCRC8(CRC8:byte; v : Byte) : Byte;
var n : byte;
begin
v := v XOR CRC8;
for n:=0 to n<8 do
begin
if (v and 0x80) then v := (v shl 1) XOR 0x07
else v := v shl 1;
end;
Result:=v;
end;
//** Read Scada bus for IR Infared Thermal Temperature Device MLX90614
Procedure MLX_ReadCMD();
begin
Repeat
If Not(TWI_Busy) then TWI_Stop(); // Ensure the Scada is STOPPED!
If Not(TWI_Busy) then TWI_Start(); // Start Read Config Addresses
If Not(TWI_Busy) then TWI_Write((SA * 2) or 0x00); // Write the Slave Address OR with the WRITE CMD of 0x00
If Not(TWI_Busy) then TWI_Write(Sensor); // Write the Sensor Address Object2 Temp $08 Object1 Temp $07 Ambient Temp $06
If Not(TWI_Busy) then TWI_Start(); // Start Temperature Read
If Not(TWI_Busy) then TWI_Write((SA * 2) or 0x01); // Write the Slave Address OR with the Read CMD of 0x01
If Not(TWI_Busy) then MLX_LSB :=TWI_Read(1); // Read the LSB first
If Not(TWI_Busy) then MLX_MSB :=TWI_Read(1); // Read the MSB next
If Not(TWI_Busy) then MLX_PEC :=TWI_Read(0); // Read the PEC (CRC sum for string confirmation calc'ing)
If Not(TWI_Busy) then TWI_Stop(); // Stop Scada
CRC8:=0x00; // Zero out CRC sums and prepare to confirm stream total sum
CRC8:=CalcCRC8(CRC8, (SA * 2)); // Send CRC with Slave Address for WRITE to confirm sumation
CRC8:=CalcCRC8(CRC8, Sensor); // Send CRC with Object Temp Address to confirm sumation
CRC8:=CalcCRC8(CRC8, SA * 2 + 1);// Send CRC with Slave Address for READ to confirm sumation
CRC8:=CalcCRC8(CRC8, MLX_LSB); // Send CRC with Temp LSB to confirm sumation
CRC8:=CalcCRC8(CRC8, MLX_MSB); // Send CRC with Temp MSB to confirm sumation
CRC8:=CalcCRC8(CRC8, MLX_PEC); // Send CRC Sumation to ensure no xfer error on the scada
if Not(CRC8=0) then
begin
LCD_CMD(_LCD_CLEAR);
LCD_Out(1,1,'CRC Checksum');
LCD_Out(2,1,'Check Sensor');
Delay_ms(1000);
LCD_CMD(_LCD_CLEAR);
TWI_Stop(); // Force Stop Scada and try to restart
TWI_Stop(); //
Delay_ms(1000);
end;
Until CRC8=0; // Continue trying to talk to Sensor until CRC8 checksum is good.
//=== Calculate Celsius and Ferinheit Temperature Readings
MLX_Calc:=0x00; // Zero Out MLX Previous Calc Value
MLX_Calc := (MLX_MSB * 256) + MLX_LSB; // Calculate Kelvin Value first
MLX_C:= (MLX_Calc * 0.02) - 273.70; // Convert Kelvin Value to Celsius the number 273.70 is used for compensation 273.15 is spec'd
MLX_F:= ((MLX_C * 9) / 5) + 32; // Convert Celsius Value to Ferinheit Value for us dumb American's
// MLX_F:= (MLX_C * 1.
![Cool 8)](./images/smilies/icon_cool.gif)
end;
Function MLX_DigitFormat(Reading:Real): array[23] of char;
var
Temp1, Temp2 : array[23] of char;
Tres, Tiptr : Real;
IntRes, IntIptr : LongInt;
begin
Tres:=modf(Reading, Tiptr);
IntRes:=Integer(Tres*10);
IntIptr:=Integer(TIptr);
IntToStr(IntIptr,Temp1);
Ltrim(temp1);
While length(temp1)<3 do StrAppendPre(' ',temp1);
IntToStr(IntRes,Temp2);
Ltrim(temp2);
ReadStr:=Temp1+'.'+Temp2;
While length(ReadSTr)<5 do StrAppendSuf(ReadStr,' ');
Result:=ReadStr;
end;
begin
{ Main program }
DDRA:= %00000001;
PORTA:=%00000000;
TWI_Init(100000);
Delay_ms(10);
MLX_LSB:=0;
MLX_MSB:=0;
MLX_PEC:=0;
MLX_Temp:='';
MLX_CTemp:='';
MLX_FTemp:='';
LCD_Init();
LCD_CMD(_LCD_CURSOR_OFF);
Delay_ms(10);
LCD_Out(1,1,'MLX90614 IR TEMP');
LCD_Out(2,1,' SENSOR ');
Delay_ms(1000);
LCD_CMD(_LCD_CLEAR);
Delay_ms(10);
Sensor:=OTemp1;
LCD_Out(1,16,'O');
While TRUE do
begin
if (PinA and 1) then
begin
if Sensor=OTemp1 then
begin
Sensor:=DTemp;
LCD_Out(1,16,'D');
end
else
begin
Sensor:=OTemp1;
LCD_Out(1,16,'O');
end;
// Delay_ms(250);
While (PinA and 1) do begin end;
end;
MLX_ReadCmd();
MLX_Temp:=MLX_DigitFormat(MLX_Calc)+'K';
MLX_CTemp:=MLX_DigitFormat(MLX_C)+'C';
MLX_FTemp:=MLX_DigitFormat(MLX_F)+'F';
LCD_Out(1,5,MLX_Temp); // Display Kelvin Temp Reading
LCD_Out(2,1,MLX_FTemp); // Display Ferinheit Temp Reading
LCD_Out(2,10,MLX_CTemp); // Display Celsiur Temp Reading
end;
end.
i really