#include #include #include #include #include #include "lm75.h" #include "deftypes.h" extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); uchar buf[32]; void lm75_print_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"); } uchar *read_data( uchar *buf ){ //# [buf] = 32 byte if(write(devicef, buf, 1) != 1){ //# write one byte to device bus_err(errno); } usleep(10*1000); //# Wait 10ms for reading if(read(devicef, buf, 2) != 2) { //# read the result bus_err(errno); } return buf; } float calculate_temp(float offset){ // Calculate temperature int16 rawtemp = 0; //# Signed by default float temp; buf[0] = 0x00; //# Measurement in the REG[0] read_data(buf); //# [buf] = 32 byte rawtemp = (buf[0]*256 + buf[1]) >> 5; // (buf[0] << 8), and (>> 5), Device with 11bit data, 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 read_tos(void){ // Over-Temperature Shutdown register int16 rawtemp = 0; //# Int16 default signed buf[0] = 0x02; //# Tos = REG[2] read_data(buf); rawtemp = buf[0]*256 + buf[1]; rawtemp = (rawtemp) >> 7; //# 9bit data if((rawtemp & 256) == 256){ //# check MSB if it's a 2'complement number rawtemp = rawtemp - 512 ; //# 2^9 = 512; } return (rawtemp * 0.5); //# 9bit -> 0.5 celsius } float read_thys(void){ // Over-Temp Hysteresis Register int16 rawtemp = 0; //# signed buf[0] = 0x03; read_data(buf); //# read from REG[3] rawtemp = buf[0]*256 + buf[1]; rawtemp = (rawtemp) >> 7; //# 9bit data if((rawtemp & 256) == 256){ rawtemp = rawtemp - 512 ; //# 2^9 = 512; } return (rawtemp * 0.5); //# 9bit -> 0.5 celsius } uchar read_conf(void){ //Configuration register buf[0] = 0x01; read_data(buf); return buf[0]; } void lm75_read_all(const uchar *opts){ // Print out whole device's data // if(opts != NULL) // { // printf("00:%f\n", calculate_temp(strtof(opts, NULL))); // } // else // { printf("00:%f\n", calculate_temp(0.0)); // } printf("01:0x%x\n", read_conf()); printf("02:%f\n", read_tos()); printf("03:%f\n", read_thys()); } void lm75_read_one(const uchar *opts){ // Prints the selected register's data uint16 id,i; uchar temp[256]; if(opts != NULL){ for(i = 0; i < strlen((char*)opts); i++){ if (*(opts+i) == ','){ break; } temp[i] = *(opts+i); temp[i+1] = '\0'; } id = atoi((char*)temp); strncpy((char*)temp, (char*)opts+i+1, 255); // id = strtol (opts,&ptr,0); //all format allowed // ptr++; //one separator allowed switch (id) { case 0x00: printf("%f\n", calculate_temp(atof((char*)temp))); break; case 0x01: printf("0x%x\n", read_conf()); break; case 0x02: printf("%f\n", read_tos()); break; case 0x03: printf("%f\n", read_thys()); break; default: print_help(); } } } void lm75_conf_set(const uchar *opts){ // printf(""); }