做项目使用了NRF51822,要用mlx90614进行红外测温,分享一下相关的使用。
mlx90614是一款红外非接触温度计,有多个型号,BAA、DCI等,不同封装的测温距离以及精度不同,我这里使用的是BAA。mlx90614在使用上支持两线串行协议,对应引脚为SDA/PWM和SCL。在这里使用的是SMBus。SMBus 接口为两线协议,允许主控器件 (MD) 和一个或是一个以上的从动器件 (SD) 通信。系统在给定的时刻只有一个主控器件。 MLX90614 只作为从动器件使用。一般来讲, MD 是通过从动地址 (SA)选择从动器件并开始数据传输。
在使用时,首先MD(主机)要发送SD(从机)地址,在 SD 接收到每个 8 位数据后,会回复 ACK/NACK 信息。当 MD 初始化通信,将首先发送受控地址,只有能识别该地址的 SD 会确认,其它的会保持沉默。如果 SD 未确认其中的任意字节, MD 应停止通信并重新发送信息。NACK 也会在 PEC 接收后出现,这意味着在接收的信息有错误并且 MD 应重新发送信息。 PEC 的计算结果是基于除 START,REPEATED START,STOP,ACK, 和 NACK 位外的。PEC 是 CRC-8 的多项式。每个字节的最高位首先传送。
首先,MD发送起始位;接下来MD发送SD地址,SD收到会发送ACK,MD收到ACK信息之后,发送命令,如读数据写数据;SD发送低八位数据,接受ACK;SD发送高八位数据,接受ACK;MD发送PEC,接受ACK,通讯结束。
接下来放上我的代码,部分是从其他单片机使用的例子上移植过来的。
//相关定义
#define ACK 0
#define NACK 1
#define SA 0x00 //从机地址,单个MLX90614时地址为0x00,多个时地址默认为0x5a
#define RAM_ACCESS 0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ1 0x07 //To1 address in the eeprom
#define SMBUS_SCK_H() nrf_gpio_pin_set(SMBUS_SCK)
#define SMBUS_SCK_L() nrf_gpio_pin_clear(SMBUS_SCK)
#define SMBUS_SDA_H() nrf_gpio_pin_set(SMBUS_SDA);
#define SMBUS_SDA_L() nrf_gpio_pin_clear(SMBUS_SDA)
//相关函数
* 函数名: 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
//(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
}
/*******************************************************************************
* 函数名: SMBus_SendByte
* 功能: 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)//判断Tx_buffer最高位是否为1
{
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 Tx_buffer=Tx_buffer<<1 Tx_buffer左移一位
}
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(uint8_t 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
*******************************************************************************/
uint8_t SMBus_ReceiveBit(void)
{
uint8_t 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())
if (nrf_gpio_pin_read(SMBUS_SDA))
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
return Ack_bit;
}
/*******************************************************************************
* 函数名: SMBus_ReceiveByte
* 功能: 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;
}
/*******************************************************************************
* 函数名: SMBus_Delay
* 功能: 延时 一次循环约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++);
}
}
/*******************************************************************************
* 函数名: SMBus_ReadMemory
* 功能: READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* 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表示接下来写命令
{
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 from do-while{}
data = (DataH<<8) | DataL; //data=DataH:DataL
return data;
}
/*******************************************************************************
* 函数名: PEC_calculation
* 功能 : 数据校验
* Input : pec[]
* Output : None
* Return : pec[0]-this byte contains calculated crc value
*******************************************************************************/
uint8_t PEC_Calculation(uint8_t pec[])
{
uint8_t crc[6];
uint8_t BitPosition=47;
uint8_t shift;
uint8_t i;
uint8_t j;
uint8_t temp;
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[i]&(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[i]<<=1;
crc[i]+=temp;
}/*End of for*/
shift--;
}/*End of while*/
/*Exclusive OR between pec and crc*/
for(i=0; i<=5; i++)
{
pec[i] ^=crc[i];
}/*End of for*/
}
while(BitPosition>8); /*End of do-while*/
return pec[0];
}
/*******************************************************************************
* 函数名: SMBus_ReadTemp
* 功能: 计算并返回温度值
* Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{
return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
}
主函数中只需使用SMBus_ReadTemp()这一函数即可得到所测得的温度值。