Hi, I'm hoping someone more knowledgeable will have the generosity to help me understand some code I'm looking at modifying.
Below is a code block used to drive an MCP4911 (10-bit SPI DAC).
(dat is a number from 0-1023)
digitalWrite(LDAC, HIGH) ;//spi communication
digitalWrite(SS, LOW) ;
SPI.transfer((dat >> 6) | 0x30) ;
SPI.transfer((dat << 2) & 0xff) ;
digitalWrite(SS, HIGH) ;
digitalWrite(LDAC, LOW) ;
What I can't understand are the two SPI.transfer lines.
I understand >> 6 means shift the bits 6 bits to the right, and the | 0x30 is some kind of bit-setting "mask", but this translates to 110000 so I'm not sure what bits exactly it is acting on.
I understand << 2 means shift the bits 2 bits to the left and similarly 0xff is 11111111 so we are changing 8 of the bits.
What I can't understand is why we are shifting the bits 6 increments and then 2 increments, and how the 6-bit and 8-bit binary masks relate to what is two bytes.
I feel like I'm missing some knowledge involving the 'order' of the bits (I have looked at the datasheet and this code for hours now and it looks like the first 4 bits are to set the DAC commands, then data is the next 10 bits, then 2 bits are ignored... How this relates to shifting 6 bits across and 2 back I don't know.
I'm trying to understand this code in order to translate the larger piece of code to work with a different DAC (MCP7425, an I2C 12-bit DAC). I have a different piece of reference code that's closely related to the same function as the SPI code above, but this is shifting the bits by 8 positions.
Wire.beginTransmission(0x60);
Wire.write((MCP_OUT >> 8) & 0x0F);
Wire.write(MCP_OUT);
Wire.endTransmission();
Again, I have studied the code and datasheet at length and it looks to me like the first 4 bits should be command data, then the next 12 bits are the data-data.
As there are 2-bits of difference in how much the two codes are shifting the bits, I am assuming this is related to the DAC's bit counts somehow, but I don't have the intellect to figure out what is going on.
I would really appreciate any advice about this.

