标题:
基于stm32单片机的红外测温MLX90614代码分享
[打印本页]
作者:
2300606886
时间:
2024-7-11 10:53
标题:
基于stm32单片机的红外测温MLX90614代码分享
51hei图片_20240713092342.jpg
(1.15 MB, 下载次数: 10)
下载附件
2024-7-13 09:23 上传
1.起始信号与停止信号:void SMBus_StartBit(void)
{
SMBUS_SDA_H(); // 首先拉高数据线
SMBus_Delay(5); // 延时几微妙
SMBUS_SCK_H();// 拉高时钟线
SMBus_Delay(5); // 延时几微妙
SMBUS_SDA_L(); // 拉低数据线
SMBus_Delay(5); // 延时几微妙
//在SCK=1时,检测到SDA由1到0表示通信开始(下降沿)
SMBUS_SCK_L(); // 拉低时钟线
SMBus_Delay(5); // 延时几微妙
}
void SMBus_StopBit(void)
{
SMBUS_SCK_L(); // 拉低时钟线
SMBus_Delay(5); // 延时几微妙
SMBUS_SDA_L(); // 拉低数据线
SMBus_Delay(5); // 延时几微妙
SMBUS_SCK_H(); // 拉高时钟线
SMBus_Delay(5); // 延时几微妙
SMBUS_SDA_H(); // 拉高数据线
}
2.发送一个字节:
u8 SMBus_SendByte(u8 Tx_buffer)
{
u8 Bit_counter;
u8 Ack_bit;
u8 bit_out;
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)//如果最高位为1
{
bit_out=1; // 把最高位置1
}
else //如果最高位为0
{
bit_out=0; // 把最高位置0
}
SMBus_SendBit(bit_out); // 把最高位发送出去
Tx_buffer<<=1;// 左移一位把最高位移出去等待下一个最高位,循环8次,每次都发最高位,就可把一个字节发出去了
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
3.接收一个字节:
u8 SMBus_ReceiveByte(u8 ack_nack)
{
u8 RX_buffer;
u8 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;//如果Ack_bit=1,把收到应答信号1与0000 0001 进行或运算,确保为1
}
else
{
RX_buffer <<= 1;// If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;//如果Ack_bit=1,把收到应答信号0与1111 1110 进行与运算,确保为0
}
}
SMBus_SendBit(ack_nack);//把应答信号发出去,如果0,就进行下一次通信,如果为1,就拜拜了。
return RX_buffer;
}
4.数据校验:
u8 PEC_Calculation(u8 pec[])
{
u8 crc[6];//存放多项式
u8 BitPosition=47;//存放所有数据最高位,6*8=48 最高位就是47位
u8 shift;
u8 i;
u8 j;
u8 temp;
do
{ //Load pattern value 0x00 00 00 00 01 07
crc[5]=0;
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
BitPosition=47;
shift=0;
i=5;
j=0;
while((pec[ i]&(0x80>>j))==0 && i>0)[ i]
{
BitPosition--;
if(j<7)
{
j++;
}
else
{
j=0x00;
i--;
}
}
shift=BitPosition-8;
while(shift)
{
for(i=5; i<0xFF; i--)
{
if((crc[i-1]&0x80) && (i>0))
{
temp=1;
}
else
{
temp=0;
}
crc[ i]<<=1;[ i]
crc[ i]+=temp;[ i]
}
shift--;
}
for(i=0; i<=5; i++)
{
pec[ i] ^=crc[ i];[ i][ i]
}
}
while(BitPosition>8);
return pec[0];
}
5.读取温度函数:
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data;
u8 Pec;
u8 DataL=0;
u8 DataH=0;
u8 arr[6];
u8 PecReg;
u8 ErrorCounter;
ErrorCounter=0x00;// Initialising of ErrorCounter
slaveAddress <<= 1; //2-7位表示从机地址 从机地址左移一位,把读写位空出来
do
{
repeat:
SMBus_StopBit();
--ErrorCounter;
if(!ErrorCounter) //ErrorCounter=0?
{
break; //如果为0就跳出do-while{}循环
}
SMBus_StartBit();
if(SMBus_SendByte(slaveAddress))//发送从机地址最低位Wr=0表示接下来写命令
{
goto repeat;
}
if(SMBus_SendByte(command))//发送命令
{
goto repeat;
}
SMBus_StartBit();
if(SMBus_SendByte(slaveAddress+1)) //发送从机地址+1最低位Rd=1表示接下来读数据
{
goto repeat;
}
DataL = SMBus_ReceiveByte(ACK); //读低位数据保存到DataL
DataH = SMBus_ReceiveByte(ACK); //读高位数据保存到DataH
Pec = SMBus_ReceiveByte(NACK); //读校验数据保存到Pec
SMBus_StopBit();
arr[5] = slaveAddress;
arr[4] = command;
arr[3] = slaveAddress+1;
arr[2] = DataL;
arr[1] = DataH;
arr[0] = 0;
PecReg=PEC_Calculation(arr);//Calculate CRC 数据校验
}
while(PecReg != Pec);
data = (DataH<<8) | DataL;
return data;
}
6.得到最终温度值:
float SMBus_ReadTemp(void)
{
float temp;
temp = SMBus_ReadMemory(0x00, 0x07)*0.02-273.15;
return temp;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1