LOPY SPI Mikroe click module

  • Hello, has anyone tried to read through SPI any Mikroe Click module using a Pycom board?
    I would like to use this module to read a pressure sensor, but I have not been able to decipher how to read it.
    someone could help me?, thanks.

  • @johncaipa said in LOPY SPI Mikroe click module:

    adc = (((buf[0] << 8) | buf[1]) & 0xfff) >>1

    That one is wrong. You have to shift the raw value befor masking.
    The lowest bit of the returned value will not be at bit0 of adc, so you have to shift to to the right. Reason: The adc starts shifting out values at the 2nd (or third?) clock cycle, and SPI will create 16 clock cycles. So there are like 14 bits in the result buffer, of which only the high order 12 bits are valid. The others have to be discarded and the value has to be properly aligned.
    If you use a separate CS pin, you can simply use spi.read_into().

    adc = ((buf[0] << 8) | buf[1]) >> x) & 0xfff

    x should be a value between 1 and 3. Best is to determine it experimentally.

    The conv variable and write_readinto() is only used to create the CS signal at the MOSI pin. By using an appropriate pattern for conv, like 0x8000, 0xc000, of 0xe000, you can shift the CS signal in clock steps, making the final shift of the result obsolete. So doing that save a pin and saves a few instructions.

  • @robert-hh thanks but that example did not work for me, neither MOSI-> CS, for this I used an independent pin, but based on your example and the manufacturer's examples it seems that I could do it, although I do not understand the additional byte displacement:

    from machine import Pin
    from machine import SPI
    #SPI  pins.
    SPI_CLK  = 'P19'
    SPI_MOSI = 'P20'
    SPI_MISO = 'P21'
    SPI_CS   = 'P23'
    SPI_LOW  = 0
    SPI_HIGH = 1
    spi_cs = Pin(SPI_CS, mode = Pin.OUT)
    # configure the SPI master @ 1MHz
    # this uses the SPI non-default pins for CLK, MOSI and MISO ('P19(G6)','P20(G7)','P21(G8)')
    spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, polarity=1, phase=1, bits=16, pins=(SPI_CLK, SPI_MOSI, SPI_MISO))
    buf = bytearray(2)
    conv = b'\xe0\x00'
    spi.write_readinto(conv, buf)
    adc = (((buf[0] << 8) | buf[1]) & 0xfff) >>1
    mA =  adc * 102.4 / 4096.0 / 4.99 # Vref=2048 A=20 Vref/A = 102.4 ; 12bit=4096 ; Shunt R = 4R99

    what do you think?

  • @johncaipa No, but it looks quite straightforward.
    a) enable an SPI bus. Connect Clk->Clk, MOSI->CS, MISO->SDO

    • read 2 bytes from SPI, while sending 0x00
    • shift the value you got to the proper position

    I made a driver here for the ADS7818, where MOSI is used for CS, which nicely synchronizes it with the read. Ignore the inverted part.
    Edit: On the click module, Vref is 2.048V, as it uses a MAX6106 voltage reference

Log in to reply

Pycom on Twitter