gfvalvo:
Instead of getting wrapped around the axle with the endianness of a uint32_t, you could use an array of bytes:
SPISettings mySpiSettings(3000000, MSBFIRST, SPI_MODE0);
uint8_t bytesToSend[3];
uint8_t address = 7;
uint16_t voltage = 0xC80;
bytesToSend[0] = (address << 3) & 0xF8;
bytesToSend[0] |= (voltage >> 11) & 0x7;
bytesToSend[1] = (voltage >> 3) & 0xFF;
bytesToSend[2] = (voltage << 5) & 0xE0;
SPI.beginTransaction(mySpiSettings);
SPI.transfer(bytesToSend, 3);
SPI.endTransaction();
Thank you very much! I implemented your code, but unfortunately, I was not able to change a voltage on any of the output channels.
So, I had a closer looked at your code, line by line, and I have a few questions:
1.
bytesToSend[0] = (address << 3) & 0xF8;
& 0xF8 is not really needed, right?
bytesToSend[0] |= (voltage >> 11) & 0x7;
Here the same, do you really need & 0x7 ?
- I understand this line (no question here, just to make sure I understood it correctly)
bytesToSend[1] = (voltage >> 3) & 0xFF;
0-0-D1-D2-D3-D4-D5-D6-D7-D8-D9-D10-D11-D12-D13-D14
3
0-0-0-0-0-D1-D2-D3-D4-D5-D6-D7-D8-D9-D10-D11 & 00000000.11111111
D4-D5-D6-D7-D8-D9-D10-D11
bytesToSend[2] = (voltage << 5) & 0xE0;
Here I would get this:
0-0-D1-D2-D3-D4-D5-D6-D7-D8-D9-D10-D11-D12-D13-D14
<< 5
D4-D5-D6-D7-D8-D9-D10-D11-D12-D13-D14 -0-0-0-0-0 (&00000000.11100000)
0-0-0-0-0- D12-D13-D14
Don't we have to left shift this result? Hence:
(((voltage << 5) & 0xE0) << 5);
Also, a more general question: Since the Arduino is little-endian, don't we have to fill in the array in reversed order?
Thanks a lot for your help!