diff options
author | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
---|---|---|
committer | Gergő J. Miklós | 2018-09-29 01:32:11 +0200 |
commit | ab99f700bace05491919324259c9110c7f387af8 (patch) | |
tree | 699d197b204e482dfb56f8443e036ed6f740ace0 /src/lm75.c | |
download | i2sensors-ab99f700bace05491919324259c9110c7f387af8.tar.gz i2sensors-ab99f700bace05491919324259c9110c7f387af8.zip |
Existing code & initialcommit.
Diffstat (limited to 'src/lm75.c')
-rw-r--r-- | src/lm75.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/lm75.c b/src/lm75.c new file mode 100644 index 0000000..fac61c2 --- /dev/null +++ b/src/lm75.c @@ -0,0 +1,163 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + + +#include "lm75.h" + +extern int file; +extern void bus_err(int ern); +extern void print_help(void); +unsigned char buf[5]; + + +void lm75_list_all(void) +{ + printf( + "00: Temperature data [°C] (reg: 0x00)\n" + "01: Configuration [hex] (reg: 0x01)\n" + "02: Tos (Overtemp) [°C] (reg: 0x03)\n" + "03: Thys (Hysteresis) [°C] (reg: 0x04)\n" + "\n"); +} + + + +void lm75_get_data(void) +{ + if(write(file, buf, 1) != 1) + { + bus_err(errno); + } + + if(read(file, buf, 2) != 2) + { + bus_err(errno); + } +} + + + +float lm75_read_temp(float offset) +{ + signed int rawtemp = 0; + float temp; + + buf[0] = 0x00; + + lm75_get_data(); + + rawtemp = (buf[0]*256 + buf[1]) >> 5; //(buf[0] << 8), and (>> 5), 11bit device is also supported + + if((rawtemp & 0x400) == 0x400) //check if the msb(bit11) is 1 (1024 = 0x400), 2'complement negative number + { + rawtemp = rawtemp - 2048; //2^11 = 2048 + } + + temp = rawtemp * 0.125 ; //11bit->0.125°C or 9bit->0.5°C + return (temp + offset); +} + + + +float lm75_read_tos(void) +{ + int rawtemp = 0; + + buf[0] = 0x02; + + lm75_get_data(); + + rawtemp = buf[0]*256 + buf[1]; + rawtemp = (rawtemp) >> 7; //9bit data + + if((rawtemp & 256) == 256) // check if the msb 2'complement negative number + { + rawtemp = rawtemp - 512 ; // 2^9 = 512; + } + + return (rawtemp * 0.5); //9bit -> 0.5 celsius +} + + + +float lm75_read_thys(void) +{ + int rawtemp = 0; + + buf[0] = 0x03; + + lm75_get_data(); + + rawtemp = buf[0]*256 + buf[1]; + rawtemp = (rawtemp) >> 7; //9bit data + + if((rawtemp & 256) == 256) // check if the msb 2'complement negative number + { + rawtemp = rawtemp - 512 ; // 2^9 = 512; + } + + return (rawtemp * 0.5); //9bit -> 0.5 celsius +} + + +char lm75_read_conf(void) +{ + + buf[0] = 0x01; + + lm75_get_data(); + + return buf[0]; +} + + + +void lm75_read_all(const char *opts) +{ + if(opts != NULL) + { + printf("00:%f\n", lm75_read_temp(strtof(opts, NULL))); + } + else + { + printf("00:%f\n", lm75_read_temp(0.0)); + } + + printf("01:0x%x\n", lm75_read_conf()); + printf("02:%f\n", lm75_read_tos()); + printf("03:%f\n", lm75_read_thys()); +} + + + +void lm75_read_one(const char *opts) +{ + int id; + char *ptr; + + id = strtol (opts,&ptr,0); //all format allowed + ptr++; //one separator allowed + + switch (id) + { + case 0x00: + printf("00:%f\n", lm75_read_temp(strtof(ptr, NULL))); + break; + case 0x01: + printf("01:0x%x\n", lm75_read_conf()); + break; + case 0x02: + printf("02:%f\n", lm75_read_tos()); + break; + case 0x03: + printf("03:%f\n", lm75_read_thys()); + break; + default: + print_help(); + } +} + |