member_tdh
Member level 5
mlx90615
Hi everybody!
At the moment, I'm studying the SMBUS protocol. I'm making a project that use IR temperature ( MLX90615 ).
The result that i've receied alway is:
Tobj=382.19C
Tobj=382.19C
Tobj=382.19C
...
My code can wrong, could anybody help me...!!?
Thanks very very much!!!
This is my code:
"SMBus.h"
"SMBus.c"
"main.c"
Hi everybody!
At the moment, I'm studying the SMBUS protocol. I'm making a project that use IR temperature ( MLX90615 ).
The result that i've receied alway is:
Tobj=382.19C
Tobj=382.19C
Tobj=382.19C
...
My code can wrong, could anybody help me...!!?
Thanks very very much!!!
This is my code:
"SMBus.h"
Code:
#ifndef __SMBUS_INCLUDE__
#define __SMBUS_INCLUDE__
#define _SDA 7 //PORTD.7
#define _SCL 6 //PORTD.6
#define _SDA_OUTPUT DDRD |= (1<<_SDA);
#define _SDA_INPUT DDRD &= ~(1<<_SDA);
#define _SCL_OUTPUT DDRD |= (1<<_SCL);
#define mSDA_IN PIND.7
#define mSDA_HIGH PORTD |= (1<<_SDA);
#define mSDA_LOW PORTD &= ~(1<<_SDA);
#define mSCL_HIGH PORTD |= (1<<_SCL);
#define mSCL_LOW PORTD &=~ (1<<_SCL);
#define TBUF 5
#define HIGHLEV 3
#define LOWLEV 1
#define ACK 0
#define NACK 1
#define WRITE 0x00 // SMBus WRITE command
#define READ 0x01 // SMBus READ command
#define SA 0x00 // SMBus Slave address
#define DEFAULT_SA 0x5B // Factory Default SMBus Slave address (MLX90615)
#define RAM_Access 0x00 // RAM access command
#define RAM_Tobj1 0x07 // To1 address in the eeprom
#define EEPROM_Access 0x20 // EEPROM access command
#define Read_Flags 0xF0
#define SLEEP_mode 0xFF
extern void MLX90615_init(void);
extern void START_bit(void);
extern void STOP_bit(void);
extern void SendRequest(void);
extern void DummyCommand(BYTE);
extern float CalcTemp(WORD);
extern WORD read_RAM(BYTE slaveADDR, BYTE ADDR);
extern WORD read_EEPROM(BYTE slaveADDR, BYTE ADDR);
extern void Object_Temperature(void);
extern void Ambient_Temperature(void);
extern void TruyenByte(BYTE x);
extern BYTE NhanByte(BYTE ack_nack);
#endif
"SMBus.c"
Code:
void MLX90615_init(void)
{
_SDA_OUTPUT; // Set SDA as Output
_SCL_OUTPUT; // Set SCL as Output
mSDA_HIGH; // bus free
mSCL_HIGH;
}
void START_bit(void)
{
_SDA_OUTPUT; // Set SDA as Output
mSDA_HIGH; // Set SDA line
mSCL_HIGH; // Set SCL line
delay_us(TBUF); // Generate bus free time between Stop
// and Start condition (Tbuf=4.7us min)
mSDA_LOW; // Clear SDA line
delay_us(TBUF); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
// (Thd:sta=4.0us min)
mSCL_LOW; // Clear SCL line
}
void STOP_bit(void)
{
_SDA_OUTPUT; // Set SDA as Output
mSDA_LOW; // Clear SDA line
mSCL_HIGH; // Set SCL line
delay_us(TBUF); // Wait a few microseconds
mSDA_HIGH; // Set SDA line
mSCL_LOW;
}
void TruyenByte(BYTE x)
{
BYTE i;
_SDA_OUTPUT; // Set SDA as Output
for(i=0;i<8;i++)
{
mSCL_LOW;
if(x&0x80) mSDA_HIGH;
else mSDA_LOW;
x<<=1;
mSCL_HIGH;
delay_us(20);
}
_SDA_INPUT; //SDA line as Input
mSDA_LOW; //ack tai clock thu 9
mSCL_LOW;
mSCL_HIGH;
if(mSDA_IN & 0x80) printf("MLX90615 writed error...ACK!\n\r");
mSCL_LOW;
}
BYTE NhanByte(BYTE ack_nack)
{
BYTE i;
WORD byte=0;
_SDA_INPUT;
mSCL_LOW;
for(i=0;i<8;i++)
{
byte<<=1;
mSCL_HIGH;
byte|=mSDA_IN;
mSCL_LOW;
delay_us(20);
}
_SDA_OUTPUT; //DATA-line in output
PORTD.7=!ack_nack; //"ack==1" pull down DATA-Line
mSCL_HIGH;
delay_us(20);
mSCL_LOW;
//_SDA_INPUT; //DATA-line in input
//mSDA_HIGH;
return byte;
}
WORD read_RAM(BYTE slaveADDR, BYTE ADDR)
{
BYTE data_4lsb;
BYTE data_4msb;
BYTE data_crc;
WORD data;
START_bit();
TruyenByte(slaveADDR);
TruyenByte(ADDR|RAM_Access);
START_bit();
TruyenByte(slaveADDR|0x01);
data_4lsb = NhanByte(ACK);
data_4msb = NhanByte(ACK);
data_crc = NhanByte(ACK);
STOP_bit();
data=(WORD)data_4msb<<8|data_4lsb;
return data;
}
"main.c"
Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
[url]http://www.hpinfotech.com[/url]
Project :
Version :
Date : 7/20/2009
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega32
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 512
*****************************************************/
#include "mega32.h"
#include "global.h"
#include "SMBus.c"
void main(void)
{
delay_ms(100); //Power Startup Stable
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xFE;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Global enable interrupts
//#asm("sei")
printf(" --------------------------------------- \n\r");
printf(" ------------ IR TEMPERATUR ------------ \n\r");
printf(" ------------ MLX90615 ------------ \n\r");
printf(" --------------------------------------- \n\r");
MLX90615_init();
delay_ms(200); //Wait after POR,Tvalid=0.15s
while (1)
{
Object_Temperature();
delay_ms(1000);
};//end while
}//end main
void Object_Temperature(void)
{
WORD temperature;
temperature = read_RAM(0x00, 0x07);
printf("Tobj=%d.%dßC \n\r", ((temperature*2)-27315)/100, ((WORD)((temperature*2)-27315))%100);
}
Last edited by a moderator: