Hi everybody,
I am trying to program this DAC: MAX532
to control a motor driver from Teency 3.5.
I need to write by SPI three 8-bit words.
For example I need to convert 4096 and 0 in:
1111 1111 1111 0000 0000 0000.
How can I convert two integer numbers into three 8-bit words (each)?
Thank you for the help!
Lorenzo
unsigned long x = 4096;
byte x0 = (x >> 16) & 255;
byte x1 = (x >> 8) & 255;
byte x2 = x & 255;
For example I need to convert 4096 and 0 in:
I don't understand your example - it looks to me like it has too many 1s.
Thank you CtrlAltElite!
How can I print all the resulting bits on the serial monitor to check if it is correct before sending to SPI?
How can I print all the resulting bits on the serial monitor to check if it is correct before sending to SPI?
If they were correct on the way in, why wouldn't they be correct on the way out?
I find hex simpler to work with than binary, but Serial.print can output in binary, without leading zeroes, if you want.
Unfortunately I have to use binary because I have to write those values on the DAC register through SPI.
I was trying to print in binary with all zeros...Is there a way?
Thank you again
use a small loop over the bits
for (byte mask = B10000000; mask; mask >>= 1)
{
Serial.print(value & mask);
}
Serial.println();
Or in opposite direction
for (byte mask = B00000001; mask; mask <<= 1)
{
Serial.print(value & mask);
}
Serial.println();
Unfortunately I have to use binary because I have to write those values on the DAC register through SPI.
Please rest assured that whatever base they values are printed out in, they will be written to a register in binary - there's no other way!
I was trying to print in binary with all zeros...Is there a way?
You could write a simple fall-through function that prints the leading zeroes for you, or do the loop and mask yourself, and print all the bits individually.
I haven't tested or even compiled this:
void printByte (byte val)
{
for (byte mask = 0x80; mask; mask >>= 1)
{
Serial.print (val & mask ? '1' : '0');
}
}
do you want to implement page 8 of the datasheet?
Note: code of CtrlAltElite is correct.
Thank you very much robtillaart & CtrlAltElit!
Going back to my problem...
Sorry, I need 2 numbers to be converted into 8+8+8 bits in total (12 for the first number and 12 for the second number)... How can I do it?
Yes! Page 8 of the datasheet! 
You have 24 bits.
Are these stored in a single 32 bit value (and if so, are the bits left or right justified?) or in two separate 16 bit values?
I have 2 numbers (I can decide in which format) and I have to transform them into 3 8-bit words.
Then, I will write them in SPI using the following code:
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(byte_0);
SPI.transfer(byte_1);
SPI.transfer(byte_2);
digitalWrite(slaveSelectPin, HIGH);
(That I hope it is the correct way...)
OK, take another look at my first reply.
Sorry, I don't understand the 255...
If I would have x1=2000 and x2=4000, how would it be the code to obtain byte_0, byte_1 and byte_2?
Thank you
Sorry, I don't understand the 255...
OK, use 0xFF, 0377, or 0b11111111 if you prefer.
Ok.
Sorry if I don't understand.
Could you give me an example that starts with 2 numbers x1 and x2 and convert them into 3 8-bit words (3 8-bit words in total, not 3 for each number ) please? 
The byte in the middle should contain "half binary size" of the first number and "half binary size" of the second one (as in page 8 and 12 of the datasheet that I have linked in my first post).
The OP want to merge two 12 bit numbers in 3 bytes
[ 0123 4567 8901 ] [ 0123 4567 8901 ] ==> [ 0123 4567 ] [8901 0123] [4567 8901]
in code:
byte1 = value1 >> 4;
byte2 = ( value1 << 4 ) | ( value2 >> 8 );
byte3 = value2 & 255;
Why don't you try some code, and get back if it doesn't do what you want?
That way I can correct what you've done, and you will learn something you can pass on to others, and others passing by here hopefully will learn something too, by seeing the common pitfalls.
Otherwise, I write all the code, you get a free-ride, and don't learn anything, and become another dependent, which benefits absolutely no-one.
robtillaart:
The OP want to merge two 12 bit numbers in 3 bytes
[ 0123 4567 8901 ] [ 0123 4567 8901 ] ==> [ 0123 4567 ] [8901 0123] [4567 8901]
in code:
byte1 = value1 >> 4;
byte2 = ( value1 << 4 ) | ( value2 >> 8 );
byte3 = value2 & 255;
Thank you Robotillar, I didn't understand the shift operator before.
This should solve my problem! 
I didn't understand the shift operator before.
So why the Hell didn't you say that, instead of saying you didn't understand the number 255?