#include "mlx90615.h"
/**********************************************************************
* Function Name : Mlx90614_Configuration
* Description : Mlx90614_Configuration
* Input : None
* Output : None
* Return : None
**********************************************************************/
void Mlx96015_Configuration(void)
{
//GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO; //声明一个结构体变量
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);//非接触温度传感器SDAL 连接 PB.12,SCL 连接 PB.13---打开 GPIOB 时钟
/*温度传感器引脚配置*/
GPIO.GPIO_Pin = (GPIO_Pin_11|GPIO_Pin_12);//非接触温度传感器 SDAL 连接 PB.12,SCL 连接 PB.13
GPIO.GPIO_Speed = GPIO_Speed_Level_3;//管脚频率为 50MHZ
GPIO.GPIO_Mode =GPIO_Mode_OUT ;//输出模式
GPIO.GPIO_OType=GPIO_OType_OD; //为开漏
GPIO_Init(GPIOC,&GPIO);//初始化 GPIOB 寄存器
SDA_H; //拉高SDAL和SCL
SCL_H; //因为 SMBus 是下降沿触发
}
/**********************************************************************
* Function Name : SMBus_StartBit
* Description : Generate START condition on SMBus
* Input : None
* Output : None
* Return : None
**********************************************************************/
void SMBus_StartBit(void)
{
SDA_H; // Set SDA line
SMBus_Delay(1); // Wait a few microseconds
SCL_H; // Set SCK line
SMBus_Delay(5); // Generate bus free time between Stop
SDA_L; // Clear SDA line
SMBus_Delay(10); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SCL_L; // Clear SCK line
SMBus_Delay(2); // Wait a few microseconds
}
/**********************************************************************
* Function Name : SMBus_StopBit
* Description : Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
**********************************************************************/
void SMBus_StopBit(void)
{
SCL_L; // Clear SCK line
SMBus_Delay(5); // Wait a few microseconds
SDA_L; // Clear SDA line
SMBus_Delay(5); // Wait a few microseconds
SCL_H; // Set SCK line
SMBus_Delay(10); // Stop condition setup time(Tsu:sto=4.0us min)
SDA_H; // Set SDA line
}
/**********************************************************************
* Function Name : SMBus_SendByte
* Description : Send a byte on SMBus
* Input : Tx_buffer
* Output : None
* Return : None
**********************************************************************/
uint8_t SMBus_SendByte(uint8_t Tx_buffer)
{
uint8_t Bit_counter;
uint8_t Ack_bit;
uint8_t bit_out;
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;
}
/**********************************************************************
* Function Name : SMBus_SendBit
* Description : Send a bit on SMBus
* Input : bit_out
* Output : None
* Return : None
**********************************************************************/
void SMBus_SendBit(uint8_t bit_out)
{
if(bit_out==0)
{
SDA_L;
}
else
{
SDA_H;
}
SMBus_Delay(2); // Tsu:dat = 250ns minimum
SCL_H; // Set SCK line
SMBus_Delay(10); // High Level of Clock Pulse
SCL_L; // Clear SCK line
SMBus_Delay(10); // 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
**********************************************************************/
uint8_t SMBus_ReceiveBit(void)
{
uint8_t Ack_bit;
SDA_H; //引脚靠外部电阻上拉, 当作输入
SCL_H; // Set SCL line
SMBus_Delay(2); // High Level of Clock Pulse
if (SMBUS_SDA_PIN)
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SCL_L; // Clear SCL line
SMBus_Delay(4); // Low Level of Clock Pulse
return Ack_bit;
}
/**********************************************************************
* Function Name : SMBus_ReceiveByte
* Description : Receive a byte on SMBus
* Input : ack_nack
* Output : None
* Return : RX_buffer
**********************************************************************/
uint8_t SMBus_ReceiveByte(uint8_t ack_nack)
{
uint8_t RX_buffer;
uint8_t Bit_Counter;
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;
}
/**********************************************************************
* Function Name : SMBus_Delay* Description : 延时 一次循环约 1us
* Input : time
* Output : None
* Return : None
**********************************************************************/
void SMBus_Delay(uint16_t time)
{
uint16_t i, j;
for (i=0; i<4; i++)
{
for (j=0; j<time; j++);
}
}
/**************************************************************************
* Function Name : SMBus_ReadMemory
* Description : READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Output : None
* Return : Data
***************************************************************************/
uint16_t SMBus_ReadMemory(uint8_t slaveAddress, uint8_t command)
{
uint16_t data; // Data storage (DataH:DataL)
uint8_t Pec; // PEC byte storage
uint8_t DataL=0; // Low data byte storage
uint8_t DataH=0; // High data byte storage
uint8_t arr[6]; // Buffer for the sent bytes
uint8_t PecReg; // Calculated PEC byte storage
uint8_t ErrorCounter; // Defines the number of the attempts for communicationwith MLX90615
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{}
}
SMBus_StartBit(); //Start condition
if(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位 Wr=0 表示接下来写命令
{
goto repeat; //Repeat comunication again
}
if(SMBus_SendByte(command)) //Send command
{
goto repeat; //Repeat comunication again
}
SMBus_StartBit(); //Repeated Start condition
if(SMBus_SendByte(slaveAddress+1)) //Send SlaveAddress 最低位 Rd=1 表示接下来读数据
{
goto repeat; //Repeat comunication again
}
DataL = SMBus_ReceiveByte(ACK); //Read low data,master must send ACK
DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK
Pec = SMBus_ReceiveByte(NACK); //Read PEC byte, master must send NACK
SMBus_StopBit(); //Stop condition
arr[5] = slaveAddress;
arr[4] = command;
arr[3] = slaveAddress+1; //Load array arr
arr[2] = DataL;
arr[1] = DataH;
arr[0] = 0;
PecReg=PEC_Calculation(arr); //Calculate CRC
}
while(PecReg != Pec); //If received and calculated CRC are equal go out fromdo-while{}
data = (DataH<<8) | DataL; //data=DataH:DataL
return data;
}
/**********************************************************************
* Function Name : PEC_calculatio