/*******************************************************************************
* 函数名: SMBus_StartBit
* 功能 : 产生起始位
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{
SMBUS_SDA_H(); // Set SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Generate bus free time between Stop
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
}
/*******************************************************************************
* 函数名: SMBus_StopBit
* 功能: Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)
SMBUS_SDA_H(); // Set SDA line
}
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)
{
bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
}
else
{
bit_out=0; // else clear bit_out
}
SMBus_SendBit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
/*******************************************************************************
* 函数名: SMBus_SendBit
* 功能: Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
SMBus_Delay(2); // Tsu:dat = 250ns minimum
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(6); // High Level of Clock Pulse
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
// SMBUS_SDA_H(); // Master release SDA line ,
return;
}
/*******************************************************************************
* Function Name : SMBus_ReceiveBit
* Description : Receive a bit on SMBus
* Input : None
* Output : None
* Return : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
u8 Ack_bit;
SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入
SMBus_Delay(2); // High Level of Clock Pulse
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMBus_ReceiveBit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack); // Sends acknowledgment bit
return RX_buffer;
}
/*******************************************************************************
* 函数名: SMBus_Delay
* 功能: 延时 一次循环约1us
* Input : time
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
u16 i, j;
for (i=0; i<4; i++)
{
for (j=0; j
}
}
/*******************************************************************************
* 函数名: SMBus_ReadMemory
* 功能: READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Return : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data; // Data storage (DataH:DataL)
u8 Pec; // PEC byte storage
u8 DataL=0; // Low data byte storage
u8 DataH=0; // High data byte storage
u8 arr[6]; // Buffer for the sent bytes
u8 PecReg; // Calculated PEC byte storage
u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614
ErrorCounter=0x00; // Initialising of ErrorCounter
slaveAddress <<= 1; //2-7位表示从机地址
do
{
repeat:
SMBus_StopBit(); //If slave send NACK stop comunication
--ErrorCounter; //Pre-decrement ErrorCounter
if(!ErrorCounter) //ErrorCounter=0?
{
break; //Yes,go out from do-while{}
}
do
{
/*Load pattern value 0x000000000107*/
crc[5]=0;
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
/*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
BitPosition=47;
/*Set shift position at 0*/
shift=0;
/*Find first "1" in the transmited message beginning from the MSByte byte5*/
i=5;
j=0;
while((pec&(0x80>>j))==0 && i>0)
{
BitPosition--;
if(j<7)
{
j++;
}
else
{
j=0x00;
i--;
}
}/*End of while */
/*Get shift value for pattern value*/
shift=BitPosition-8;
/*Shift pattern value */
while(shift)
{
for(i=5; i<0xFF; i--)
{
if((crc[i-1]&0x80) && (i>0))
{
temp=1;
}
else
{
temp=0;
}
crc<<=1;
crc+=temp;
}/*End of for*/
shift--;
}/*End of while*/
/*Exclusive OR between pec and crc*/
for(i=0; i<=5; i++)
{
pec ^=crc;
}/*End of for*/
}
while(BitPosition>8); /*End of do-while*/
/*******************************************************************************
* 函数名: SMBus_StartBit
* 功能 : 产生起始位
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{
SMBUS_SDA_H(); // Set SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Generate bus free time between Stop
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
}
/*******************************************************************************
* 函数名: SMBus_StopBit
* 功能: Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)
SMBUS_SDA_H(); // Set SDA line
}
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)
{
bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
}
else
{
bit_out=0; // else clear bit_out
}
SMBus_SendBit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
/*******************************************************************************
* 函数名: SMBus_SendBit
* 功能: Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
SMBus_Delay(2); // Tsu:dat = 250ns minimum
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(6); // High Level of Clock Pulse
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
// SMBUS_SDA_H(); // Master release SDA line ,
return;
}
/*******************************************************************************
* Function Name : SMBus_ReceiveBit
* Description : Receive a bit on SMBus
* Input : None
* Output : None
* Return : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
u8 Ack_bit;
SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入
SMBus_Delay(2); // High Level of Clock Pulse
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMBus_ReceiveBit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack); // Sends acknowledgment bit
return RX_buffer;
}
/*******************************************************************************
* 函数名: SMBus_Delay
* 功能: 延时 一次循环约1us
* Input : time
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
u16 i, j;
for (i=0; i<4; i++)
{
for (j=0; j
}
}
/*******************************************************************************
* 函数名: SMBus_ReadMemory
* 功能: READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Return : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data; // Data storage (DataH:DataL)
u8 Pec; // PEC byte storage
u8 DataL=0; // Low data byte storage
u8 DataH=0; // High data byte storage
u8 arr[6]; // Buffer for the sent bytes
u8 PecReg; // Calculated PEC byte storage
u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614
ErrorCounter=0x00; // Initialising of ErrorCounter
slaveAddress <<= 1; //2-7位表示从机地址
do
{
repeat:
SMBus_StopBit(); //If slave send NACK stop comunication
--ErrorCounter; //Pre-decrement ErrorCounter
if(!ErrorCounter) //ErrorCounter=0?
{
break; //Yes,go out from do-while{}
}
do
{
/*Load pattern value 0x000000000107*/
crc[5]=0;
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
/*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
BitPosition=47;
/*Set shift position at 0*/
shift=0;
/*Find first "1" in the transmited message beginning from the MSByte byte5*/
i=5;
j=0;
while((pec&(0x80>>j))==0 && i>0)
{
BitPosition--;
if(j<7)
{
j++;
}
else
{
j=0x00;
i--;
}
}/*End of while */
/*Get shift value for pattern value*/
shift=BitPosition-8;
/*Shift pattern value */
while(shift)
{
for(i=5; i<0xFF; i--)
{
if((crc[i-1]&0x80) && (i>0))
{
temp=1;
}
else
{
temp=0;
}
crc<<=1;
crc+=temp;
}/*End of for*/
shift--;
}/*End of while*/
/*Exclusive OR between pec and crc*/
for(i=0; i<=5; i++)
{
pec ^=crc;
}/*End of for*/
}
while(BitPosition>8); /*End of do-while*/