Arduino Math using Nano

I am developing code to have a si5351 replace the crystals of a vintage 2m radio. I need to be able to calculate the frequency of both the receive and transmit crystals. My code for the transmit crystal frequency works but I can't get the the receive code to work. The receive crystal is much higher in frequency, than the transmit crystal. Anyway here is a code fragment, I am grateful for any help.

union longlong  
{
  unsigned long long c;
  unsigned long      a;
  unsigned long      b;

};
union longlong Mylonglong;
unsigned long long ReceiverFreq = 147165000;
void setup() {
  Serial.begin(57600);
  ReceiverFreq = ReceiverFreq - 10700000;
  ReceiverFreq = ReceiverFreq / 3 ;

  Mylonglong.c = ReceiverFreq;
  Serial.print("rec freq ");
  Serial.println(Mylonglong.a, DEC);

  ReceiverFreq = ReceiverFreq * 100L;

  Mylonglong.c = ReceiverFreq;
  Serial.print("rec freq ");
  Serial.println(Mylonglong.a, DEC); // the result I wanr is 454883300
}

void loop() {

}

Please give us the actual formula for each type of crystal.

This is my output:

rec freq 45488333
rec freq 253866004

Thanks:
For receive: CrystalFrequency = (Receive_frequency - 10700000) / 3
my Receive_frequency is 147165000
The Receive crystal frequency has to be multiplied by 100 to work with the si5351
For Transmit : transmitFRequency / 24
My Transmit frequency 147765000
The Transmit crystal frequency has to be multiplied by 100 to work with the si5351

That's what I get. I need to get 4548833300 .

Why not make it easy on yourself and use floats and mHz with decimal points instead of everything in Hz and guess at the decimal point?

Why do you need the names a and b that refer to the same long?
Maybe a and b should be part of a struct?

4,548,833,300 = about 4.5 Billion
An unsigned long can only hold about 4.29 billion. You have an overflow.

1 Like

... or tack on a "00" in text

My first thought as well, but the float math is not accurate enough to give me an accurate crystal frequency.

I am trying to convert it to unsigned long long.

It will certainly give you any frequency for any crystal you can buy. Remember ALL crystals have a error value that is NOT constant and varies by both temperature and other changes in the circuitry.

This is the only way I know of to get Serial.print to print out an unsigned long long, it's only used for debug.

Your "way" does not work.

It IS an unsigned long long:

unsigned long long ReceiverFreq = 147165000;
  ReceiverFreq = ReceiverFreq - 10700000;
  ReceiverFreq = ReceiverFreq / 3 ;
  ReceiverFreq = ReceiverFreq * 100L;

It's too bad that Serial.print() can't do unsigned long long.

Try this:

void printULL(unsigned long long value)
{
  if (value == 0)
    return;
  printULL(value/10);
  Serial.print(int(value % 10));
}
1 Like

You are correct. I just converted an SSB rig to use the si5351. I keep the si5351 in a temperature controlled enclosure to ensure that it stays exactly on frequency.

You're more knowledgeable than me, I did what I could with what I know. Thanks for sharing.

Nonsense, nothing ever stays exactly on frequency.

works great for printing out an unsigned long long. Thanks for sharing

You didn't notice the * *? Drift is less than 1 Hz. Good enough for me. I control the enclosure temperature to +- .2 Celcius.