@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().
spi.read_into(buf)
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.