simonbiwer / Tic-Tac-Toe

Tic Tac Toe game on a microcontroller with touch display
2 stars 0 forks source link

Issue with controlling MCP4441 with Raspberry Pi #1

Open dcl920108 opened 1 month ago

dcl920108 commented 1 month ago

Hi Simon, I'm trying to control an MCP4441 chip using a Raspberry Pi, and I'm having some issues. I've been using your [repository/library] as a reference, but I'm not getting the expected results. Specifically, I'm trying to set the output value of Channel 2 to its maximum value, but when I read back the value, it's always 0. I've tried modifying the write and read formats, but nothing seems to work. Here's my code: import smbus

class MCP4442_DAC: def init(self, i2c_bus, hw_addr): self.i2c_bus = i2c_bus self.hw_addr = hw_addr self.i2c = smbus.SMBus(i2c_bus)

def set_max(self):
    mem_addr = 0x06  # Channel 2 register address
    value = 0xFF  # Maximum output value

    command_byte = (mem_addr << 4) | (0x00 << 2) | ((value & 0x0300) >> 8)
    data_byte = value & 0x00FF

    self.i2c.write_byte_data(self.hw_addr, command_byte, data_byte)
    print("Successfully wrote value {} to register {}".format(value, mem_addr))

    # Read back the value
    read_value = self.i2c.read_byte_data(self.hw_addr, mem_addr)
    print("Successfully read value {} from register {}".format(read_value, mem_addr))

dac = MCP4442_DAC(1, 0x2C) # Initialize I2C bus and MCP4441 chip address dac.set_max() # Set Channel 2 output value to maximum

The output I get is:

Successfully wrote value 255 to register 6 Successfully read value 0 from register 6

I'm not sure what's going wrong. Can you help me troubleshoot this issue or provide some guidance on how to control the MCP4441 chip with a Raspberry Pi? Thanks in advance for your help!

Best regards, Nero

simonbiwer commented 1 week ago

Hi Nero,

great that you're using my repository as a reference. I'm not really familiar with microchip technology, so I can just try to take some guesses what could be the problem.

Are you using a MCP4441 or MCP4442 chip? You introduced with MCP4441 but your class is named MCP4442. Maybe there are differences in the registers or addresses that need to be used for your task.

Maybe a timing issue? The write progress may take some time and when you read back the value immediately it is not yet written? Maybe try to include some waiting and then read back the value.

Maybe try to use another implementation of the smbus library? There is also smbus2 (https://pypi.org/project/smbus2/).

I hope you can figure out the problem.

Best regards, Simon