diff --git a/I2C_serial_pi.py b/I2C_serial_pi.py index bcd369455e4b308da1e9b94d524da00793cc6795..7d22124fd1e224f856ba29d290ad8fe59f104b89 100644 --- a/I2C_serial_pi.py +++ b/I2C_serial_pi.py @@ -55,7 +55,7 @@ class I2C: return ret_ack, ret_value - def read_last_reg(self, bytes_to_read): + def read_last_reg(self, bytes_to_read, print_on = DEBUG): bus = smbus.SMBus(self.bus_nr) rd_value = [] ret_value = '' @@ -68,12 +68,12 @@ class I2C: except IOError: ret_ack = 0 rd_value.append(0) - if DEBUG: - print("Reading error") + if print_on: + print(f"Reading IOerror {rd_value}") except err: ret_ack = 0 rd_value.append(0) - if DEBUG: + if print_on: print("Reading error") for cnt in range(bytes_to_read): ret_value += (hex(rd_value[cnt])[2:]) diff --git a/apsct_lib.py b/apsct_lib.py index 0b2d17d8c39c6c47d7394368017499dc518d631d..f6b0a253c0ac22db4adb5591dc62e36731d618c4 100644 --- a/apsct_lib.py +++ b/apsct_lib.py @@ -406,6 +406,8 @@ class ApsctSensors: if not (15 < self.temperature < 50): result = False print(f"Error temperature read {self.temperature:4.2f} °C") + else: + print(f"OK temperature read {self.temperature:4.2f} °C") return result def read_voltage(self, input_channel=0): @@ -422,9 +424,8 @@ class ApsctSensors: if DEBUG: stri = "Word to select sens input is 0x{0:x}".format(channel_select_word) print(stri) - sleep(0.2) self.dev_i2c_sensor.write_bytes(channel_select_word, 0xB8) - sleep(0.2) + sleep(0.3) # Wait for device to take snapshot ret_ack, ret_value = self.dev_i2c_sensor.read_last_reg(3) if DEBUG: stri = "Return value input 0 : 0x{0} ".format(ret_value) @@ -434,10 +435,11 @@ class ApsctSensors: else: steps = (int(ret_value, 16) & 0x1FFFFF) >> 6 voltage = one_step * steps - voltage = ((4.7+2.2)/2.2)*2*voltage + voltage = ((4.7+2.2)/2.2)*2*voltage # Resistor network + half swing if DEBUG: string = "Voltage sens line {1} is {0:.4f} V".format(voltage, input_channel) print(string) + sleep(0.2) # wait for device to go to sleep return voltage def read_temp(self): @@ -448,16 +450,20 @@ class ApsctSensors: # Vref = 3.0 temp_slope = 93.5E-6 * 2**(16+1) / Vref - sleep(1.0) - self.dev_i2c_sensor.write_bytes(0xA0, 0xC0) - sleep(1.0) - ret_ack, ret_value = self.dev_i2c_sensor.read_last_reg(3) - if ret_ack: - raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6 - temperature_K = (raw_value/temp_slope) - self.temperature = temperature_K-273 - else: - self.temperature = 9999 + ret_ack = self.dev_i2c_sensor.write_bytes(0xA0, 0xC0) + sleep(0.5) + self.temperature = 9999 + loops = 0 + while (self.temperature > 100 ) & (loops < 2): + loops = loops + 1 + ret_ack, ret_value = self.dev_i2c_sensor.read_last_reg(3) + if ret_ack: + raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6 + temperature_K = (raw_value/temp_slope) + self.temperature = temperature_K-273 + else: + self.temperature = 9999 + sleep(0.2) return self.temperature class PpsClass: