Tool/software:
Hi TI team,
Need detailed step by step information on reading temperature from this sensor tmp114.
Getting the output as FFFF.
when i tried to write configuration register address - 0x03 's value as 0 and trying to read from the sensor writing to 0x00 and reading from that.
I'm attaching the base c code that we tried.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stdint.h>
#define TMP114_ADDRESS 0x49
#define I2C_BUS "/dev/i2c-0"
#define REG_TEMP_RESULT 0x00
#define REG_CONFIGURATION 0x03
struct tmp114_registers {
uint16_t temp_result;
};
void read_register(int file, uint8_t reg, uint16_t *value) {
uint8_t buffer[2] = {0};
buffer[0] = reg;
if (write(file, buffer, 1) != 1) {
perror("Failed to move to register address\n");
exit(1);
}
if (read(file, buffer, 2) != 2) {
perror("Failed to read register data\n");
exit(1);
}
*value = (buffer[0] << 8) | buffer[1];
}
void write_register(int file, uint8_t reg, uint16_t value) {
uint8_t buffer[3] = {0};
buffer[0] = reg;
buffer[1] = value >> 8;
buffer[2] = value & 0xFF;
if (write(file, buffer, 3) != 3) {
perror("Failed to write to register\n");
exit(1);
}
}
void configure_sensor(int file) {
uint16_t config_value = 0x0000;
write_register(file, REG_CONFIGURATION, config_value);
}
void read_temperature_result(int file, struct tmp114_registers *regs) {
read_register(file, REG_TEMP_RESULT, ®s->temp_result);
}
void print_temperature_result(const struct tmp114_registers *regs) {
printf("Temp_Result: %d\n", regs->temp_result);
}
int main() {
int file;
if ((file = open(I2C_BUS, O_RDWR)) < 0) {
perror("Failed to open the I2C bus");
exit(1);
}
if (ioctl(file, I2C_SLAVE, TMP114_ADDRESS) < 0) {
perror("Failed to acquire bus access and/or talk to the slave");
exit(1);
}
struct tmp114_registers regs;
configure_sensor(file);
while (1) {
read_temperature_result(file, ®s);
print_temperature_result(®s);
sleep(1);
}
close(file);
return 0;
}
Please suggest further changes that need to be done to read the temperature data from this sensor...
Also if available, please provide a c code for the same.
Thanks & Regards
Sai Akhil C