Author Topic: Manual Frequency/Channel Selection Inconsistently Not Working  (Read 1575 times)

zkorange

  • NewMember
  • *
  • Posts: 8
Manual Frequency/Channel Selection Inconsistently Not Working
« on: November 21, 2020, 11:51:51 AM »
I'm using a LoRa Module on a Moteino with a helical antenna and the RFM95 Radiohead Library, trying out manually selecting a frequency or channel using a rotary DIP switch.  Here's the relevant code:

Code: [Select]
/*calculates the channel based on the dip switch*/
int getChannel() {
  uint8_t dip = 0;
  if (!digitalRead(DIP_0_PIN)) dip+=1;
  if (!digitalRead(DIP_1_PIN)) dip+=2;
  if (!digitalRead(DIP_2_PIN)) dip+=4;
  if (!digitalRead(DIP_3_PIN)) dip+=8;
  return dip;
}

/* Sets Tx/Rx Channel and frequency */
void setChannel(){
  if (getChannel() != channel){

    /*need a delay so it doesn't try to read
    the channel between clicks of the dip switch*/
    delay(100);

    /*Set channel based on the dip switch*/
    channel = getChannel();
    Serial.print("Channel selected: ");
    Serial.println(channel);

    /*Set operating frequency based on the channel selected*/
    if (!radio.setFrequency(902.2 + (channel * 1.6)))
    {
      Serial.println("Selected frequency not supported by this radio.");
      while (1);
    }
    Serial.print("Transmitting and receiving at ");
    Serial.print(902.2 + (channel * 1.6));
    Serial.println(" mhz...");

  }
}

I call the setChannel() function in the main loop for both the Rx and Tx units.  This all seems to work fine when both the Tx and Rx unit are set to the same frequency on boot up.  And if I change the frequency of the Tx unit, predictably the Rx unit stops receiving until I change the frequency back to match.

HOWEVER, if I change the frequency of the Rx unit and change it back to match, it no longer receives messages from the Tx... even though they are seemingly set to the same frequency.  When I reset the Rx unit entirely, it goes back to receiving fine.  And sometimes, but not always, after the Rx frequency has changed, if I change the Tx frequency and change it back they reconnect again.

I have no idea where to begin diagnosing this, so any guidance would be appreciated.  Also, if anyone knows if it's possible to confirm that a radio is set to a particular frequency after setting it, that might be helpful.

Thank you!

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Manual Frequency/Channel Selection Inconsistently Not Working
« Reply #1 on: November 21, 2020, 06:07:53 PM »
Read back the actual frequency registers to ensure what you wrote is what you get back, I think this tripped me up at least once.
That, and ensure nothing else has changed in the registers.
The RFM69 library has a readAllRegs() function that is useful to do a diff/compare before/after you made your changes, so you can quickly highlight if anything is out of place or not as you expect. You will of course need a register reference/the datasheet close by to tell what is going on, and getting a little familiar with the register themselves. Time consuming, but satisfying when you get to find and fix the bug.

zkorange

  • NewMember
  • *
  • Posts: 8
Re: Manual Frequency/Channel Selection Inconsistently Not Working
« Reply #2 on: November 22, 2020, 08:08:19 AM »
Thanks, Felix!

Do you happen to know if the RadioHead LoRa library has a similar function?

And would that be a register reference/datasheet for the RF Module?

Sounds like a fun challenge indeed!

zkorange

  • NewMember
  • *
  • Posts: 8
Re: Manual Frequency/Channel Selection Inconsistently Not Working
« Reply #3 on: November 22, 2020, 08:42:25 AM »
Update:

I found two functions that could be useful in the RH library: spiRead() and printRegisters().

I added spiRead() before and after setFrequency(), reading out the specific registers that are changed by the setFrequency() function and they appear to be changed and changed back correctly.

I also put in printRegisters() before and after and found that three of the registers were changing and changing back reliably.

Here's the new code snippet:

Code: [Select]
void setChannel(){
  if (getChannel() != channel){

    delay(100);

    /*Set channel based on the dip switch*/
    channel = getChannel();
    Serial.print("Channel selected: ");
    Serial.println(channel);

    Serial.println("Registers before:");
    Serial.println(radio.spiRead(RH_RF95_REG_06_FRF_MSB));
    Serial.println(radio.spiRead(RH_RF95_REG_07_FRF_MID));
    Serial.println(radio.spiRead(RH_RF95_REG_08_FRF_LSB));
    radio.printRegisters();

    /*Set operating frequency based on the channel selected*/
    if (!radio.setFrequency(902.2 + (channel * 1.6)))
    {
      Serial.println("Selected frequency not supported by this radio.");
      while (1);
    }
    Serial.print("Transmitting and receiving at ");
    Serial.print(902.2 + (channel * 1.6));
    Serial.println(" mhz...");

    Serial.println("Registers After:");
    Serial.println(radio.spiRead(RH_RF95_REG_06_FRF_MSB));
    Serial.println(radio.spiRead(RH_RF95_REG_07_FRF_MID));
    Serial.println(radio.spiRead(RH_RF95_REG_08_FRF_LSB));
    radio.printRegisters();

  }
}

Here are the serial readouts (sorry it's a little long):

Code: [Select]
Channel selected: 7
Registers before:
228
192
0
1: 85
6: E4
7: C0
8: 0
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 11
14: 0
15: 76
16: 0
17: 75
18: 24
19: 27
1A: 63
1B: 29
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 0
26: 4
27: 0
Transmitting and receiving at 913.40 mhz...
Registers After:
228
89
154
1: 85
6: E4
7: 59
8: 9A
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 11
14: 0
15: 76
16: 0
17: 75
18: 27
19: 27
1A: 63
1B: 29
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 0
26: 4
27: 0
Channel selected: 8
Registers before:
228
89
154
1: 85
6: E4
7: 59
8: 9A
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 11
14: 0
15: 76
16: 0
17: 75
18: 24
19: 27
1A: 63
1B: 2A
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 0
26: 4
27: 0
Transmitting and receiving at 915.00 mhz...
Registers After:
228
192
0
1: 85
6: E4
7: C0
8: 0
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 11
14: 0
15: 76
16: 0
17: 75
18: 24
19: 27
1A: 63
1B: 29
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 0
26: 4
27: 0
Channel selected: 7
Registers before:
228
192
0
1: 85
6: E4
7: C0
8: 0
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 12
14: 0
15: 77
16: 0
17: 75
18: 64
19: D0
1A: 29
1B: 2B
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 12
26: 4
27: 0
Transmitting and receiving at 913.40 mhz...
Registers After:
228
89
154
1: 85
6: E4
7: 59
8: 9A
9: 8F
A: 9
B: 2B
C: 20
D: 11
E: 0
F: 0
10: 0
11: 0
12: 0
13: 12
14: 0
15: 77
16: 0
17: 75
18: 64
19: D0
1A: 29
1B: 2A
1C: 40
1D: 72
1E: 74
1F: 64
20: 0
21: 8
22: 11
23: FF
24: 0
25: 12
26: 4
27: 0

Note the changes of registers 7, 8, and 18.

Still the problem persists.  Thoughts?