Altering / understanding library for frequency synthesizer chip ADF4351

Hello all,

I have been using a great library to control an Analog Devices chip, adf4351. It uses SPI to communicate between the Arduino and the ADF4351. Long story short, 6 registers need to be written, and all of them are 32 bit integers.
I'm currently in the process of moving to a different Analog Devices chip. Basically everything is the same between the two chips (regarding the SPI communication), except for the number of registers and how long they are. My question is, in the library above; is there anything forcing the registers to be 32 bit long, or is the length determined by the setf function within the ADF4351 class?

Second question, during the writeDev function in the ADF4351 class, I don't understand what is happening during the loop

void ADF4351::writeDev(int n, Reg r)
{
byte txbyte ;
int i ;
digitalWrite(pinSS, LOW) ;
delayMicroseconds(10) ;
i=n ; // not used
for ( i = 3 ; i > -1 ; i--) {
txbyte = (byte) (r.whole >> (i * 8)) ;
SPI.transfer(txbyte) ;
}

digitalWrite(pinSS, HIGH) ;
delayMicroseconds(5) ;
digitalWrite(pinSS, LOW) ;
}

I am guessing the 32 bit register is broken up into 4 bytes and transferred one byte at a time, but i dont understand the syntax. Follow up, is this function forcing the registers to be multiples of 8? Or is that simply how the communication is done? I ask because the ADF4153 has 4 registers to write: 24, 24, 16, and 12 bits.

Thanks in advance,
ps: is there a way to post code better than a quote?

ps: is there a way to post code better than a quote?

Use </> code-tags

  byte  txbyte ;
  int i ;
  digitalWrite(pinSS, LOW) ;
  delayMicroseconds(10) ;
  i=n ; // not used
  for ( i = 3 ; i > -1 ; i--) {
    txbyte = (byte) (r.whole >> (i * 8)) ;
    SPI.transfer(txbyte) ;
  }

It is not the most elegant way of doing it, but yes r.whole is shifted out for first 24 bits, then 16 bits then 8, and then 0. This means that after storing the result in an 8 bit variable, any other bits are 'chopped' of, and only the 8 least significant bits are sent. So it send them a a byte at a time MSB first.

Deva_Rishi:
other bits are 'chopped' of, and only the 8 least significant bits are sent.

Thanks for the help! Couple of follow ups if you don't mind: I understand this family of ICs need their shift registers to be written MSB first. However, is there a reason the transfer is done a byte at a time? Is that simply how SPI is done, or just a convention chosen by the library author?

This would be a problem when trying to try write to a 12 bit shift register in 8 bit bytes.

Thanks for your patience!
Sami

I understand this family of ICs need their shift registers to be written MSB first.

Most of them do, but the SPI library does allow for it to be done the other way around.

is there a reason the transfer is done a byte at a time? Is that simply how SPI is done, or just a convention chosen by the library author?

to be honest i am not sure of SPI is dependent on a byte at a time, but it is the way it is setup in SPI.h,

This would be a problem when trying to try write to a 12 bit shift register in 8 bit bytes.

First of all, all bytes are 8 bit, 4 bits are a Nibble (half a byte, the one how called it that certainly had a sense of humor. and 16 bits are a Word. There is no real name for 12 bits (other than 3 Nibbles of course) If that is really a problem i can't tell you, you see if you write 16 bits to a 12 bit register, the 4 bits that were sent first, simply fall out at the other end, so unless the reading of the register occurs after 12 clock pulses, there is no problem. Still if that would be the issue, than either you can modify SPI.h or just go for a slower transfer, setting each of the 12 bits, followed by a clock pulse.
Within the SPI.h library there is a function for writing a Word as well, so adding a specific 12-bit version shouldn't be to complicated, but i think i would first try to just send 16 bits of which the first 4 are '0', first.