MAX 5318 SPI having to convert an 8 bit or 16 bit value to 18 bit

Ive posted this in Networking but i feel it is now more of a programming question as Im already communicating with the DAC. I have 2 applications, the first application uses an 8 bit value while the second application uses a 16 bit value. The value needs to be converted to an 18 bit value for the DAC . The DAC wants 24 bits (4 register address bits [to actually change the value we use "DIN register write" which is 0001] | 18 data bits | 2 dont care bits). If D represents data bits, Byte1 would be 0001DDDD Byte2 would be DDDDDDDD and Byte3 would be DDDDDDXX

Still waiting on parts so I just defined n to go from 0 to 255 so I get a change in output. I used the map function to try and convert the 8 bit value to the 18 bit value but it doesnt seem to work. How can i accomplish this and how can i get it into the format above? Here is my current code

#define RESET 8

#define RDY 9

#define MZ 5

#define BUSY 6

#define LDAC 7

#define MOSI 12

#define MISO 11

#define SS 10

#define SCLK 13

#define TCSB 4

#define PD 3

#define DGND 2

 

#include <SPI.h>

#include <avr/io.h>

 



byte SensorData;

String binnum;

byte wordnum;

byte address;

unsigned int Wdata;

String DataString;

uint32_t DACOut_val;

int a, n;

 

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void setup() {

  Serial.begin(57600);

  pinMode(RDY , INPUT);

  pinMode(MISO , INPUT);

  pinMode(MOSI , OUTPUT);

  pinMode(RESET , OUTPUT);

  pinMode(MZ , OUTPUT);

  pinMode(BUSY , INPUT);

  pinMode(LDAC , INPUT);

  pinMode(TCSB , OUTPUT);

  pinMode(PD , OUTPUT);

  pinMode(SCLK , OUTPUT);

  pinMode(SS , OUTPUT);

  pinMode(DGND , OUTPUT);

 

  digitalWrite(SS, HIGH);

  digitalWrite(RESET, HIGH);

  digitalWrite(SS, HIGH);

  delay(10);

 

  digitalWrite(SS, LOW);

  SPI.begin();

  SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));

  SPCR |= (1 << MSTR);               // Set as Master

  SPCR |= (1 << SPR0) | (1 << SPR1); // divided clock by 128

  SPCR |= (1 << SPE);                // Enable SPI

 digitalWrite(RESET, HIGH);  // Set high for normal operation

  digitalWrite(MZ, LOW);      // Set output Voltage to midscale

  digitalWrite(TCSB, LOW);    // Set low for Straight Binary format or high for Two's Complement

  digitalWrite(PD, LOW);      // Set low for normal operation or high for Power Down mode

  digitalWrite(DGND, LOW);    // Set low for digital ground

 

 //  General Configuration Write Register (0x4)

  digitalWrite(SS, LOW);      // Set Slave Select LOW to begin data transfer

  SPI.transfer(0b01000111);   // Write first data byte

  SPI.transfer(0b10000000);   // Write second data byte

  SPI.transfer(0b00000000);   // Write third data byte

  digitalWrite(SS, HIGH);     // Set Slave Select HIGH to end data transfer

 

  //  GAIN Write Register (0x3)

  digitalWrite(SS, LOW);      // Set Slave Select LOW to begin data transfer

  SPI.transfer(0b00111111);   // Write first data byte

  SPI.transfer(0b11111111);   // Write second data byte

  SPI.transfer(0b11111100);   // Write third data byte

  digitalWrite(SS, HIGH);     // Set Slave Select HIGH to end data transfer

 

  //  OFFSET Register Write in Two's Complement (0x2)

  digitalWrite(SS, LOW);      // Set Slave Select LOW to begin data transfer

  SPI.transfer(0b00100000);   // Write first data byte

  SPI.transfer(0b00000000);   // Write second data byte

  SPI.transfer(0b00000000);   // Write third data byte

  digitalWrite(SS, HIGH);     // Set Slave Select HIGH to end data transfer

 

  //  Straight Binary DIN Write Register (TCSB = 0) (0x1)

  digitalWrite(SS, LOW);      // Set Slave Select LOW to begin data transfer

  SPI.transfer(0b00010000);   // Write first data byte

  SPI.transfer(0b00000000);   // Write second data byte

  SPI.transfer(0b00000000);   // Write third data byte

  digitalWrite(SS, HIGH);     // Set Slave Select HIGH to end data transfer

 

  // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

}

 

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void loop() {

    address = (0b0001);  // Write Register Address

    address = address * 16;

    SensorData = n;


    n++;

    if (n > 255) {

      n = 0;

    } 

    DACOut_val = map(SensorData, 0, 255, 0, 0x3FFFC); // Convert 8 bit to 18 bit

    byte First4bits = DACOut_val & 245760; // Grab first 4 bits

    byte Second8bits = DACOut_val & 16320; // Grab the following 8 bits

    byte Third6bits = DACOut_val & 63;     // Grab the last 6 bits

 

    Serial.print("DAC Out "); Serial.print(DACOut_val, BIN);

    Serial.print(",First4bits,"); Serial.print(First4bits, BIN);

    Serial.print(",Second8bits,"); Serial.print(Second8bits, BIN);

    Serial.print(",Third6bits,"); Serial.print(Third6bits, BIN);

    Serial.print(",n,"); Serial.println(n);

 

    byte FirstPart = highByte(DACOut_val >> 2);

    byte nib = FirstPart;     //the byte to split

    byte niblow = nib & 15;   //bitwise AND of nib and 0000 1111

    byte nibhigh = nib & 240; //bitwise AND of nib and 1111 0000

    byte FirstByte = (address | nibhigh);

    byte SecondPart = highByte(DACOut_val >> 2);

    byte ThirdPart =  lowByte(DACOut_val);

 

    byte Data1 = FirstByte;

    byte Data2 = SecondPart;

    byte Data3 = ThirdPart;

 

    digitalWrite(SS, LOW);  // Set Slave Select LOW to begin data transfer

    SPI.transfer(Data1);    // Write first data byte

    SPI.transfer(Data2);    // Write second data byte

    SPI.transfer(Data3);    // Write third data byte

    digitalWrite(SS, HIGH); // Set Slave Select HIGH to end data transfer

  }

}
void loop() {


 

  while (1)

Never a good sign.

Yes you are correct... While i was cleaning my code up, i must of forgot to get rid of that. That was when offset and gain registers were in void loop(). I realized that i only need to set most registers (except DIN write) only once so I moved those to void setup(). I dont need while (1) in my loop as thats what the loop does.