/*******************************************************************************
* 文件名 : mlx90614.c
* 作 者 :
* 版 本 :
* 日 期 : 2013-08-07
* 描 述 : mlx90614函数
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "mlx90614.h"
#include "delay.h"
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//int main(void)
//{
// float temp;
// delay_init(); //延时函数初始化
// uart_init(115200); //串口初始化为115200
// IIC_Init();
// delay_ms(100);
// while(1)
// {
// temp=SMBus_ReadTemp();
// printf("温度值:%.2f\r\n",temp);
// delay_ms(1000);
// }
//
//}
/*******************************************************************************
* Function Name : SMBus_StartBit
* Description : Generate START condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{
SMBUS_IIC_SDA=1; // Set SDA line
delay_us(5); // Wait a few microseconds
SMBUS_IIC_SCL=1; // Set SCL line
delay_us(5); // Generate bus free time between Stop
SMBUS_IIC_SDA=0; // Clear SDA line
delay_us(5); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SMBUS_IIC_SCL=0; // Clear SCL line
delay_us(5); // Wait a few microseconds
}
/*******************************************************************************
* Function Name : SMBus_StopBit
* Description : Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{
SMBUS_IIC_SCL=0; // Clear SCL line
delay_us(5); // Wait a few microseconds
SMBUS_IIC_SDA=0; // Clear SDA line
delay_us(5); // Wait a few microseconds
SMBUS_IIC_SCL=1; // Set SCL line
delay_us(5); // Stop condition setup time(Tsu:sto=4.0us min)
SMBUS_IIC_SDA=1; // 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 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(uint8_t bit_out)
{
if(bit_out==0)
{
SMBUS_IIC_SDA=0;
}
else
{
SMBUS_IIC_SDA=1;
}
delay_us(2); // Tsu:dat = 250ns minimum
SMBUS_IIC_SCL=1; // Set SCL line
delay_us(6); // High Level of Clock Pulse
SMBUS_IIC_SCL=0; // Clear SCL line
delay_us(3); // Low Level of Clock Pulse
// SMBUS_IIC_SDA=1; // 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;
SMBUS_IIC_SDA=1; //引脚靠外部电阻上拉,当作输入
delay_us(2); // High Level of Clock Pulse
SMBUS_IIC_SCL=1; // Set SCL line
delay_us(5); // High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_IIC_SCL=0; // Clear SCL line
delay_us(3); // 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 : delay_us
//* Description : 延时 一次循环约1us
//* Input : time
//* Output : None
//* Return : None
//*******************************************************************************/
//void delay_us(u16 time)
//{
// uint16_t i, j;
// for (i=0; i<4; i++)
// {
// for (j=0; j<time; j++);
// }
//}
/*******************************************************************************
* Function Name : SMBus_Init
* Description : SMBus初始化
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Init()
{
SMBUS_IIC_SCL=1;
SMBUS_IIC_SDA=1;
}
/*******************************************************************************
* 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 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{}
}
SMBus_StartBit(); //Start condition
if(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位Wr=0表示接下来写命令
{