After further investigation, I have made some progress with the MAX1978 switch functionality – it has been restored and is working again. However, I've noticed that there is still an underlying issue that has not been resolved.
The problem arises when switching between different interfaces in my application, such as moving to the temperature control screen or the data display screen. Occasionally, this action causes the temperature control to shut off unexpectedly. This leads me to suspect that there might be an issue with the logic behind the MAX1978 switch in my code.
I suspect that the transitions between the different GUI screens might be inadvertently affecting the GPIO state, possibly due to the way the GPIO pins are managed or due to unintended side effects in the code executed during screen transitions.
I am currently reviewing the logic and the sequence of operations that occur when switching screens to identify any possible causes for this behavior. However, I would appreciate any insights, suggestions, or similar experiences that anyone might have, which could help in diagnosing and resolving this intermittent issue.
Thank you once again for your support and assistance.
Code:
import RPi.GPIO as GPIOimport spidevimport tracebackclass TECController: CS_PIN = 17 # 根据你的硬件配置 SPI_BUS = 1 SPI_DEVICE = 1 # 温度到DAC值的映射 temperature_to_dac_value = { 40: 5738, 41: 5595, 42: 5441, 43: 5297, 44: 5165, 45: 5022, 46: 4890, 47: 4758, 48: 4626, 49: 4493, 50: 4372, 51: 4251, 52: 4141, 53: 4020, 54: 3910, 55: 3800, 56: 3689, 57: 3590, 58: 3491, 59: 3392, 60: 3293, 61: 3194, 62: 3106, 63: 3018, 64: 2930, 65: 2852, 66: 2764, 67: 2687, 68: 2610, 69: 2533, 70: 2467, 71: 2390, 72: 2324, 73: 2258, 74: 2192, 75: 2126, 76: 2070, 77: 2004, 78: 1949, 79: 1894, 80: 1839, } def __init__(self): # 初始化SPI和GPIO self.spi = spidev.SpiDev(self.SPI_BUS, self.SPI_DEVICE) self.spi.max_speed_hz = 500000 self.spi.mode = 0b00 # GPIO.setmode(GPIO.BOARD) # GPIO.setup(self.CS_PIN, GPIO.OUT) # GPIO.setup(4, GPIO.OUT) # 假设7号引脚与你的MAX1978相关 # 设置MAX1978初始状态为ON self.max1978_state = 'ON' # GPIO.output(4, GPIO.HIGH) # 打开MAX1978 self.dac = self.MAX5144(self.spi, self.CS_PIN) class MAX5144: def __init__(self, spi, cs_pin): self.spi = spi self.cs_pin = cs_pin # GPIO.setup(self.cs_pin, GPIO.OUT) def set_dac_output(self, value): # print("MAX5144: Setting DAC output...") # 断言:确保传入的值在0到16383之间(因为DAC的值范围是16位,但最高两位是控制位) assert 0 <= value < 16384, "Invalid DAC value" # 将值左移两位,为控制位腾出空间 data_word = value << 2 # 计算最高字节(MSB)和最低字节(LSB) msb = (data_word >> 8) & 0xFF lsb = data_word & 0xFF # 将CS(片选)引脚设置为低电平,以开始与DAC的通信 GPIO.output(self.cs_pin, GPIO.LOW) # 通过SPI发送数据:先发送MSB,然后发送LSB self.spi.writebytes([msb, lsb]) # 将CS(片选)引脚设置为高电平,结束与DAC的通信 GPIO.output(self.cs_pin, GPIO.HIGH) # 打印设置的DAC值,以十六进制格式显示MSB和LSB print("DAC value set to: MSB = {0:#04X}, LSB = {1:#04X}".format(msb, lsb)) def set_temperature(self, temperature): # print("TECController: Setting DAC value for temperature...") self.current_temperature = temperature # 保存当前温度设置 dac_value = self.temperature_to_dac_value.get(temperature) if dac_value is not None: self.dac.set_dac_output(dac_value) print(f"Set temperature to {temperature}°C with DAC value {dac_value}") else: print("Temperature out of range") # print(f"[TECController] Set temperature to {temperature}°C with DAC value {dac_value}") def toggle_max1978(self): # 打印调用栈 # print(''.join(traceback.format_stack())) # 根据当前状态切换MAX1978的状态 if self.max1978_state == 'ON': GPIO.output(7, GPIO.LOW) # 关闭MAX1978 self.max1978_state = 'OFF' else: GPIO.output(7, GPIO.HIGH) # 打开MAX1978 self.max1978_state = 'ON' print(f"MAX1978 turned {self.max1978_state}") def get_max1978_state(self): return self.max1978_state # 返回当前MAX1978状态 def cleanup(self): # self.spi.close() # GPIO.cleanup() pass# 使用示例:if __name__ == "__main__": try: tec = TECController() tec.set_temperature(50) # 设置温度 print(f"Current MAX1978 state: {tec.get_max1978_state()}") finally: tec.cleanup()
I suspect that the transitions between the different GUI screens might be inadvertently affecting the GPIO state, possibly due to the way the GPIO pins are managed or due to unintended side effects in the code executed during screen transitions.
I am currently reviewing the logic and the sequence of operations that occur when switching screens to identify any possible causes for this behavior. However, I would appreciate any insights, suggestions, or similar experiences that anyone might have, which could help in diagnosing and resolving this intermittent issue.
Thank you once again for your support and assistance.
Statistics: Posted by dgy411852 — Fri Mar 01, 2024 10:44 pm