[Python] Reading from MT6816 magnetic encoder with SPI connection using Python on Raspberry Pi...

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am totally new to the field, don't really know what I'm doing and in need of help. I'm trying to read an absolute angle using a MT6816 14-bit magnetic encoder via SPI connection with Raspberry Pi.

    I have the following question:


    1. On the hardware side, is it simply just wiring up the necessary connections (3.3V, MOSI, MISO, SCK, GND, CE01)?


    2. For the coding part, I'm using Thonny and it seems like spidev library is pre-installed as the 'Raspberry Pi SPI loopback test code'(https://gist.github.com/fnishio/b2063941b82f2cf1b935) ran without any error. Thus, I assumed no other setup is needed other that enabling SPI in raspi-config. Am I right? Or are there ways to test if SPI connection works?


    3. To my understanding according to the MT6816 datasheet(https://www.compel.ru/item-pdf/c51a62edcf72a97fdb3c3d9108c3c3d5/pn/magntek~mt6816ct-std.pdf : page 23), what I need (i.e. the 14-bit angle data) is stored in the register addresses 0x03 and 0x04[7:2]. I have no idea how to access into said addresses and read those values.

    I wired up the 6 wires and played around with the code(again didn't really know what I was doing). I moved the magnet around the sensor expecting swinging range of values, the output just switched back and forth between [0, 0, 0] and [255, 255, 255].

    import spidev
    import time

    spi = spidev.SpiDev()
    spi.open(0, 0)
    spi.threewire = False
    spi.mode = 3

    def BytesToHex(Bytes):
    return ''.join(["0x%02X " % x for x in Bytes]).strip()

    try:
    while True:
    resp = spi.xfer2([0x8300, 0x8400, 0x8500])
    #print(BytesToHex(resp))
    print(resp)
    time.sleep(0.1)

    except KeyboardInterrupt:
    spi.close()
    print("Ctrl-C pressed")


    I'm eager to learn a lot! I'll appreciate any help you can offer.

    Continue reading...

Compartilhe esta Página