MCP320x 16-bit to MC format question

For general Flowcode discussion that does not belong in the other sections.
Post Reply
viktor_aust
Posts: 26
http://meble-kuchenne.info.pl
Joined: Fri May 17, 2024 1:04 am
Has thanked: 9 times
Been thanked: 4 times

MCP320x 16-bit to MC format question

Post by viktor_aust »

Hi all
MCP3201 16-bit to MC format question
After some research I found, that MCP3201 Tx the 16-bit ‘word’.

?,?,0(NULL), B11,B10,B9,B8,B7,,,B6,B5,B4,B3,B2,B1,B0,B1

?,?,0 should be removed and MSB B11,B10,B9,B8,B7 will get an extra zeros (at the end).
LSB-> B6,B5,B4,B3,B2,B1,B0,B1 should loose the last B1.

After 4 bits are removed, only 12-bit ‘word’ should be inside the modified msb+lsb data (8 bit + 8 bit).

Some suggest rx the two bytes, convert them to integer (adc_out_int = (msb << 8) | lsb) and only after that use the bitwise operator to move 3 msb ‘trash’ bits left and one bit right in lsb.

Some suggest to work with bytes:
adc_out_int = msb & 0x1F << 3 | lsb & 0xFE >> 1
OR
adc_out_int = msb & 0x1F << 7 | lsb & 0xFE >> 1 (not sure why 7?)
etc.

Question
SPI Master component allows to Rx the string.
However, with the warning about the NULL problem.
I tried, it does not work.
If I cannot Rx the string, is it possible to receive the characters one by one?
Do I have to use the lsb/msb bytes only?
Thanks.

mnfisher
Valued Contributor
Posts: 1896
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 894 times

Re: MCP320x 16-bit to MC format question

Post by mnfisher »

Looking at the datasheet - this is indeed the case - so as the SPI component reads the data in bytes (8 bits) there will be 4 bits to remove.

There are ways around this:

1) Mask/shift the appropriate bits in the bytes received before conversion to a word.
2) Convert to 16 bit word then mask and shift.
3) Bit-bang the SPI CLK pin and just receive 12 bits (obviously a little harder but the most fun :) )

The string receive doesn't quite do what you want (0 character is EOS (end of string) so I would probably use one of the byte array functions (or 3)

Martin

viktor_aust
Posts: 26
Joined: Fri May 17, 2024 1:04 am
Has thanked: 9 times
Been thanked: 4 times

Re: MCP320x 16-bit to MC format question

Post by viktor_aust »

Thank you Martin.
One more question.
The SPI Master component allows to receive the characters.
But (not like UART) in byte only 'word'.
Can I receive character by character?
Last edited by viktor_aust on Thu Apr 02, 2026 8:07 pm, edited 1 time in total.

viktor_aust
Posts: 26
Joined: Fri May 17, 2024 1:04 am
Has thanked: 9 times
Been thanked: 4 times

Re: MCP320x 16-bit to MC format question

Post by viktor_aust »

Looks like the spi protocol receive the bytes only:
SPI (Serial Peripheral Interface) receives and transmits bytes (8-bit data), not characters.

mnfisher
Valued Contributor
Posts: 1896
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 894 times

Re: MCP320x 16-bit to MC format question

Post by mnfisher »

Yes - the SPI does just send and receive bytes (8 bits) - however characters are also just bytes too... Strings - 0 is treated as the end of the string - so receiving a string will 'stop' at this point.

You could use Transaction to receive a byte at a time - just send 0 if no data to send (SPI is bidirectional (or full duplex) - data is clocked out at the same time as it is clocked in) or use TransactionArray()
so:
byte DataOut[2] = {0,0}
byte DataIn[2]
TransactionArray(.DataOut, .DataIn, 2)
.val = (DataIn[0] << 8) + DataIn[1] // assuming MSB LSB order
.val = .val >> 4 // Assuming 4 padding bits

Martin

viktor_aust
Posts: 26
Joined: Fri May 17, 2024 1:04 am
Has thanked: 9 times
Been thanked: 4 times

Re: MCP320x 16-bit to MC format question

Post by viktor_aust »

MCP3201 does not have the Din. Only Dout.
I cannot use the Transaction.
I am thinking to use the CAL SPI, but not sure how to set the parameters (MCP2101 can receive the msb first or lsb first).
I do not have some stable results with the MPC3201 Vin = floating.
Even when I connect the Vin to 5v or to Gnd the results are not stable.
As I understood the lsb is the important data parameter.
I have to check the SPI Master initialisation (something is wrong).
Thanks Martin

mnfisher
Valued Contributor
Posts: 1896
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 894 times

Re: MCP320x 16-bit to MC format question

Post by mnfisher »

To use the spi - probably use spi master.

To compile you will need to specify a DOUT (MOSI)- it won't be connected to any thing - but this is okay (toggling the clock pin transfers the data). If using transactions - the dataout will be just be dummy data which will be ignored. Note that some MCUs only support transactions - but if using an AVR or PIC you could use either transactions or not.

MISO is connected to DOUT on the adc.
CS to Chip select (if using an Arduino this just be D10 or set d10 to be an output - it defaults to the wrong pin)
Clock to clock
Vcc and gnd

Conversion is initiated by CS going low.

If pins are very tight (none spare for the 'dummy' MOSI) - bit-banging is relatively easy too.

Martin

viktor_aust
Posts: 26
Joined: Fri May 17, 2024 1:04 am
Has thanked: 9 times
Been thanked: 4 times

Re: MCP320x 16-bit to MC format question

Post by viktor_aust »

Appreciate your help Martin
Will do some tests

Post Reply