Go Down

### Topic: convert two integers into three 8-bit words (Read 718 times)previous topic - next topic

#### Lorenzo2691

#15
##### Feb 20, 2018, 11:46 pmLast Edit: Feb 20, 2018, 11:57 pm by Lorenzo2691
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).

#### robtillaart

#16
##### Feb 20, 2018, 11:53 pmLast Edit: Feb 20, 2018, 11:53 pm by 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;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### CtrlAltElite

#17
##### Feb 20, 2018, 11:57 pm
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.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

#### Lorenzo2691

#18
##### Feb 21, 2018, 12:06 am
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!

#### CtrlAltElite

#19
##### Feb 21, 2018, 12:20 am
Quote
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?
Has there been an outbreak of the stupid virus, and I didn't get the memo?

#### Lorenzo2691

#20
##### Feb 21, 2018, 12:28 am
So why the Hell didn't you say that, instead of saying you didn't understand the number 255?
I am sorry. Thank you for your help CtrlAltElite

#### Lorenzo2691

#21
##### Feb 22, 2018, 10:29 pm
In case someone will need it, I report here my code for the DAC MAX532 in unipolar configuration.

Code: [Select]
`#include <SPI.h>int channel_A = 0;int channel_B = 0;float pi=3.14;float freq=1.0f;// set pin 10 as the slave select for the digital pot:const int slaveSelectPin = 10;int cycles=0;void setup() {  Serial.begin(115200);  // set the slaveSelectPin as an output:  pinMode(slaveSelectPin, OUTPUT);  // initialize SPI:  //SPI.setClockDivider(SPI_CLOCK_DIV16);    SPI.setMOSI(11);  //SPI.setMISO(12);  SPI.setSCK(13);    SPI.begin();}      void loop() {         float t=millis()/1000.0f;            channel_A=(int) (2000.0f*sin(t*2*pi*freq)+2000.0f);      channel_B=(int) (2000.0f*sin(t*2*pi*freq+pi/2)+2000.0f);      SPI_write(channel_A, channel_B);      delay(1);}void SPI_write(int channel_A, int channel_B) {  byte byte_0 = channel_B >> 4;  byte byte_1 = ( channel_B << 4 ) | ( channel_A >> 8 );  byte byte_2 = channel_A & 255;      digitalWrite(slaveSelectPin, LOW);  SPI.transfer(byte_0);  SPI.transfer(byte_1);  SPI.transfer(byte_2);  digitalWrite(slaveSelectPin, HIGH);}`

#### robtillaart

#22
##### Feb 23, 2018, 01:57 pm
Thanks for posting the final solution!
Not everybody does that
+1
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### CtrlAltElite

#23
##### Feb 23, 2018, 02:02 pm
Code: [Select]
`float pi=3.14;` Use M_PI instead.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

Go Up