Serial read, SPI,14 bit DAC: problem

Dear all

I am making a DAC(ad5551) control program, I thought :when I typed in two numbers, then I can get two 16-bit binary, and SPI transfer them to 2 AD5551 chips. But sadly, in my program, the first “0” is disappeared and SPI,transfer is not allowed to do this. Also, to let a DAC work, do I need to set registers ?

PS: From the pic, the first number I type is 5,12, which is right. Then 2.5,6, there is a ‘0’ missed at the front of the binary.

#include <SPI.h>



const int slaveSelectPin0 = 10;  //input voltag
const int slaveSelectPin1 = 9;  //input voltag

unsigned long binary[2];
const int NUMBER_OF_FIELDS = 2; // how many comma separated fields we expect
int fieldIndex = 0; // the current field being received
float values[NUMBER_OF_FIELDS]; // array holding values for all the fields

void setup() {
  
  pinMode (slaveSelectPin0, OUTPUT);
  digitalWrite(slaveSelectPin0, HIGH);
  Serial.begin(9600);        // connect to the serial port
  // initialize SPI:
  SPI.begin(); 
  SPI.setBitOrder(MSBFIRST); 
  SPI.setBitOrder(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
}

void loop() {
   if (Serial.available() > 0) 
   {readmultiple();
    
     }            
}            
                
void readmultiple() 
{
for(fieldIndex = 0; fieldIndex < 2; fieldIndex ++)
{values[fieldIndex] = Serial.parseFloat();
}

 binary[0] = values[0] * 16383 / 5;
 binary[1] = values[1] * 16383 / 12; //---calculate output voltage
 
for (int i=0; i<2; i++) {
int zeros = 16 - String(binary[i],BIN).length();
 binary[i] = binary[i] << zeros;
 if (zeros > 2){
  binary[i] = binary[i] >> zeros - 2;
  }   

//if(i==0){                                                             
//SPI.transfer(slaveSelectPin0,highByte(binary[0]));         no matching function for call to 'SPIClass::transfer(const int&, uint8_t)'
//SPI.transfer(slaveSelectPin0,lowByte(binary[0]));
//delay(100);}

//if(i==1){
//SPI.transfer(slaveSelectPin1,highByte(binary[1]));
//SPI.transfer(slaveSelectPin1,lowByte(binary[1]));
//delay(100);
//} 
   Serial.println(binary[i]);

  Serial.println(binary[i],BIN);

delay(1000);

}
fieldIndex = 0; // ready to start over

}

![WFE~C(]}E![N1FY`YNS_``6.jpg|331x551

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

I don’t follow the code - you detect that 1 byte is available, than jump into
void readmultiple()
and never seem to actually do any Serial.read()'s to get the byte that is available and do anything with it?

Instead of all this
{
for(fieldIndex = 0; fieldIndex < 2; fieldIndex ++)
{values[fieldIndex] = Serial.parseFloat();
}
why not just wait for 2 bytes to come in
Serial.available() >1
and then do 2 serial reads?

values[0] = Serial.read();
values[1] = Serial.read();