This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/EmbededSTM32/lab8/Core/Src/LM75A.c
2023-06-23 17:30:03 +08:00

54 lines
1.4 KiB
C
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "LM75A.h"
#include "stm32f4xx_hal.h"
#include "i2c.h"
#include "usart.h"
/**************************************************
@brif 设置设备模式
@para ConfReg:配置寄存器地址
Mode:需要配置的模式
@rev: 无
****************************************************/
void LM75SetMode(uint8_t ConfReg,uint8_t Mode)
{
while(HAL_I2C_Mem_Write(&hi2c1,LM75A_ADDR,ConfReg,1,&Mode,1,100) != HAL_OK){}; //写模式寄存器
}
//读取温度数据
/**************************************************
@brif 读取Temp寄存器值
@para 无
@rev temp:Temp寄存器中的值(低5位无效)
****************************************************/
uint16_t LM75GetTempReg(void)
{
uint16_t temp;
uint8_t tempReg[2]; //tempReg[0]:高八位tempReg[1]:低八位
if(HAL_I2C_Mem_Read(&hi2c1,LM75A_ADDR,TEMP_ADDR,2,tempReg,2,100) == HAL_OK)
{
temp = ((tempReg[0] << 8) | tempReg[1]) >> 5; //整合成11位温度数据
}
return temp;
}
//温度数据转换为温度值
/**************************************************
@brif 实际温度转换
@para Temp寄存器值
@rev TempValue:转换后的温度值
****************************************************/
double LM75GetTempValue(uint16_t tempReg)
{
double TempValue;
if((tempReg & 0x0400) == 0x0000) //符号位 D10 = 0 —>实际温度为正
{
TempValue = tempReg * 0.125;
}
else
{
TempValue = ((tempReg^0xffff)+1) * 0.125;
TempValue = -TempValue;
}
return TempValue;
}