User Manual

Application Note
SMBus communication with MLX90614
390119061402 Page 31 of 32 Jan-2008
Rev 004
//------------------------------------------------------------------------------------------------------------------------------
void send_bit(unsigned char bit_out)
{
if(bit_out==0) // Check bit
mSDA_LOW(); // Set SDA if bit_out=1
else
mSDA_HIGH(); // Clear SDA if bit_out=0
Nop(); //
Nop(); // Tsu:dat = 250ns minimum
Nop(); //
mSCL_HIGH(); // Set SCL line
Delay10TCYx( HIGHLEV ); // High Level of Clock Pulse
mSCL_LOW(); // Clear SCL line
Delay10TCYx( LOWLEV ); // Low Level of Clock Pulse
// mSDA_HIGH(); // Master release SDA line ,
return;
}//End of send_bit()
//************************************************************************************************************
// RECEIVE DATA ON SMBus
//************************************************************************************************************
//Name: RX_byte
//Function: Receives a byte on SMBus
//Parameters: unsigned char ack_nack (acknowledgment bit)
// 0 - master sends ACK
// 1 - master sends NACK
//Return: unsigned char RX_buffer (Received byte)
//Comments: MSbit is received first
//************************************************************************************************************
unsigned char RX_byte(unsigned char ack_nack)
{
unsigned char RX_buffer;
unsigned char Bit_Counter;
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(Receive_bit()) // Read a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0b00000001;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0b11111110;
}
}
send_bit(ack_nack); // Send acknowledgment bit
return RX_buffer;
}// End of RX_byte()