LM73 温度传感器代码
【代码】LM73 温度传感器代码。
·
#include "i2c-dev.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define READ_ADDR_ECHO(fd, addr) \
printf("addr %02x %02x\n", addr, i2c_smbus_read_byte_data(fd, addr))
const float tableTemp[] = {0, 0, 0.03125, 0.0625, 0.125, 0.25, 0.5, 1,
2, 4, 8, 16, 32, 64, 128, -1};
int main(void) {
int32_t clk = open("/dev/i2c-1", O_RDWR);
int32_t ret = ioctl(clk, I2C_SLAVE, 0x4d);
if (ret < 0) {
printf("%s\n", strerror(errno));
return -1;
}
uint8_t buf[2];
i2c_smbus_read_i2c_block_data(clk, 0x0, 2, (uint8_t *)buf);
float temp = 0.0;
for (int32_t i = 2; i < 16; i++) {
if ((buf[0] << 8 | buf[1]) & (1 << i)) {
temp += tableTemp[i];
}
}
printf("LM73 temp %f\n", temp);
return 0;
}
更多推荐
所有评论(0)