Looking for the right code for I2C.

251 views
Skip to first unread message

Wim PE1PWR

unread,
Aug 18, 2024, 5:30:42 AM8/18/24
to Radioberry
Hi ham friends 
I am looking for the right codes to use a Arduino Nano for controlling mij BPF and LPF. The program works for almost every band. Only 160, 80 and 12 won't work. The nano works in combination with Pihpsdr. My home made transceiver is using a manuel band selector. I want to make it automatically. 
const int FILTER_160M = 8;
const int FILTER_80m = 4;
const int FILTER_40m = 802;
const int FILTER_30m = 401;
const int FILTER_20m = 101;
const int FILTER_17m = 164;
const int FILTER_15m = 264;
const int FILTER_12m = 464;
const int FILTER_10m = 232;
Hopes that somebody can tell me the right codes. A example 40m use code 802. Where can I find this code. 
My project is on GitHub. I have use a part of the ino design from somebody else.
Wim
PE1PWR 
73s

El Calvo

unread,
Aug 18, 2024, 7:33:06 PM8/18/24
to Wim PE1PWR, Radioberry

Hi,

The code for band switching in this link worked for me. The ptt part of the code did not work.

https://github.com/cinosh07/Radioberry-Filters-Interface



Juan WP3DN

--
You received this message because you are subscribed to the Google Groups "Radioberry" group.
To unsubscribe from this group and stop receiving emails from it, send an email to radioberry+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/radioberry/cdeb8eff-8152-4a25-a9ba-d5a37cb684afn%40googlegroups.com.

Wim PE1PWR

unread,
Aug 19, 2024, 1:27:25 AM8/19/24
to El Calvo, Radioberry
Hi Juan
Thanks for your reaction.
Correct thats a part of the program I use. I have made the board and have the same problem. 160, 80 and 12 are not the working code.
See the original ino file. This is made for a 4 band filter. And for Hermes lpf, bpf board with 4 bits.
I have to use a 9 band BPF. In this program 6, 10 and 12 use the same code. There for if you want to use the whole program it will give errors.
That's the reason why I want the correct code for 160, 80 and 12. When I have it it's a great project for the Radioberry because your bands wil switch automatically. Like a industrial transmitter.
Wim
PE1PWR
73

Op 19 aug 2024, om 01:33, El Calvo <3vol...@gmail.com> schreef:

Yado-san

unread,
Aug 19, 2024, 10:05:30 AM8/19/24
to Radioberry
Hi Wim,

I have only tried the N2ADR board, but the RB2 firmware also displays I2C values on the ALEX board.
If you check the I2C display value while changing the frequency and check the difference with the code,  may be able to find out why it doesn't work.

'filters.h', line 189-202
if (i2c_alex)
{
if (i2c_data != i2c_alex_data)
{
fprintf(stderr, "Set Alex data to output = %d \n", i2c_alex_data);
i2c_data = i2c_alex_data;
unsigned char ldata[3];
ldata[0] = 0x02;
ldata[1] = ((i2c_alex_data >> 8) & 0xFF);
ldata[2] = (i2c_alex_data & 0xFF);
fprintf(stderr, "Set Alex data 0 = %x \n", ldata[0]);
fprintf(stderr, "Set Alex data 1 = %x \n", ldata[1]);
fprintf(stderr, "Set Alex data 2 = %x \n", ldata[2]);
if (write(fd_i2c_alex, ldata, 3) < 0)
fprintf(stderr, "Error %d writing Alex data\n", errno);
}
}

And 'filters-interface.ino'

void receiveEvent(int bytes) {
      :
      if (byteCount == 0) {
        byte1 = x;
      }
      if (byteCount == 1) {
        byte2 = x;
        command = command + (byte2 * 100);
      }
      if (byteCount == 2) {
        byte3 = x;
        command = command + byte3;

Yado-san, jg1twp

 
2024年8月19日月曜日 14:27:25 UTC+9 Wim PE1PWR:

Wim PE1PWR

unread,
Aug 19, 2024, 11:15:25 AM8/19/24
to Radioberry
Thanks Yado San.
With this code it must be come right. 
I give it a go and let you know if its works 
Kind regards
Wim
PE1PWR 
73s

Op maandag 19 augustus 2024 om 16:05:30 UTC+2 schreef Yado-san:
Message has been deleted

pa3gsb

unread,
Sep 6, 2024, 4:13:25 AM9/6/24
to Radioberry
Hi All,

I asked chatgpt with my idea to solve it on a generic way

See the arduino code (as said code from chatgpt so i did not compile)

determine the lpf can be easily replaced by writing the right output pin.

Hope this helps to make the idea clear.

73 Johan
PA3GSB


raspberry pi (firmware)

if (i2c_filters_board) {

        if (i2c_data != i2c_alex_data) {


            i2c_data = i2c_alex_data;
            unsigned char ldata[3];
            ldata[0] = 0x02;
            ldata[1] = ((i2c_alex_data >> 8) & 0xFF);
            ldata[2] = (i2c_alex_data & 0xFF);
            if (write(fd_i2c_filter, ldata, 3) < 0)

                fprintf(stderr, "Error %d writing Alex data\n", errno);
            fprintf(stderr, "Set Alex data 0 = %x \n", ldata[0]);
            fprintf(stderr, "Set Alex data 1 = %x \n", ldata[1]);
            fprintf(stderr, "Set Alex data 2 = %x \n", ldata[2]);
        }
    }
    //*************************************************
    //              Send Mox Status
    //*************************************************

    if (i2c_filters_board & ((buffer[11] & 0x01) == 0x01 || (buffer[11] & 0x01) == 0x00)) {
        currentMox = ((buffer[11] & 0x01) == 0x01) ? 1 : 0;
        currentCW = cw;
        if (currentMox != previousMox || currentCW != previousCW) {
            previousMox = currentMox;
            previousCW = currentCW;
           
           
            unsigned char ldata[3];
            ldata[0] = 0x03;
            ldata[1] = ((buffer[11] & 0x01) == 0x01) ? 1 : 0;
            ldata[2] = currentCW;
            if (write(fd_i2c_filter, ldata, 3) < 0)

                fprintf(stderr, "Error %d writing Alex data\n", errno);
            fprintf(stderr, "PTT data 0 = %x \n", ldata[0]);
            fprintf(stderr, "PTT data 1 = %x \n", ldata[1]);
            fprintf(stderr, "PTT data 2 = %x \n", ldata[2]);
        }
    }
    //*************************************************
    //     Send Frenquency to filter companion
    //*************************************************
    if (i2c_filters_board & (buffer[523] & 0xFE) == 0x12) {

        if (currentfreq != previousfreq) {

            previousfreq = currentfreq;
            unsigned tempFreq = currentfreq;
            unsigned char ldata[9];
            ldata[0] = 0x04;
            ldata[1] = (tempFreq / 10000000U) % 10;
            ldata[2] = (tempFreq / 1000000U) % 10;
            ldata[3] = (tempFreq / 100000U) % 10;
            ldata[4] = (tempFreq / 10000U) % 10;
            ldata[5] = (tempFreq / 1000U) % 10;
            ldata[6] = (tempFreq / 100U) % 10;
            ldata[7] = (tempFreq / 10U) % 10;
            ldata[8] = (tempFreq / 1U) % 10;

            if (write(fd_i2c_filter, ldata, 9) < 0)
                fprintf(stderr, "Error %d setting frequency \n", errno);
            else
                fprintf(stderr, "Set Filters frequency to = %d \n", currentfreq);

        }
    }  
       
}


Arduino code:


#include <Wire.h>

const int I2C_SLAVE_ADDRESS = 0x22; // Het I2C-adres van de Arduino
unsigned long currentfreq = 0;
unsigned long previousfreq = 0;

void setup() {
  Wire.begin(I2C_SLAVE_ADDRESS); // Initialiseer I2C als slave
  Wire.onReceive(receiveEvent);   // Registreer de handler voor ontvangen data
  Serial.begin(9600);             // Start seriële communicatie voor debugging
}

void loop() {
  // De loop blijft leeg, alles wordt afgehandeld in receiveEvent()
  delay(100);
}

// Functie om data te ontvangen van Raspberry Pi
void receiveEvent(int numBytes) {
  if (numBytes > 0) {
    unsigned char receivedData[numBytes];
   
    // Lees alle ontvangen bytes van I2C
    for (int i = 0; i < numBytes; i++) {
      receivedData[i] = Wire.read();
    }

    // Check wat voor commando we ontvangen hebben
    switch (receivedData[0]) {
      case 0x02: // Voor commando 0x02 (3 bytes totaal)
      case 0x03: // Voor commando 0x03 (3 bytes totaal)
        if (numBytes == 3) {
          handleOtherCommands(receivedData);
        }
        break;

      case 0x04: // Voor commando 0x04 (9 bytes totaal)
        if (numBytes == 9) {
          // Herbouw de frequentie uit de ontvangen cijfers
          unsigned long tempFreq = (receivedData[1] * 10000000UL) +
                                   (receivedData[2] * 1000000UL) +
                                   (receivedData[3] * 100000UL) +
                                   (receivedData[4] * 10000UL) +
                                   (receivedData[5] * 1000UL) +
                                   (receivedData[6] * 100UL) +
                                   (receivedData[7] * 10UL) +
                                   (receivedData[8] * 1UL);

          // Update de huidige frequentie als deze anders is dan de vorige
          if (tempFreq != previousfreq) {
            previousfreq = tempFreq;
           
            // Debugging: print de ontvangen frequentie
            Serial.print("Ontvangen Frequentie: ");
            Serial.println(tempFreq);
           
            // Vertaal de frequentie naar LPF-waarde en toon op de seriële monitor
            handleLPFFilters(tempFreq);
          }
        }
        break;

      default:
        // Onbekend commando of fout
        Serial.println("Onbekend commando ontvangen");
        break;
    }
  }
}

// Functie om frequentie naar LPF-waarde te vertalen
void handleLPFFilters(unsigned long frequency) {
  unsigned char lpf = 0;

  // Bepaal de juiste LPF-waarde op basis van frequentie
  if (frequency > 32000000) lpf = 0x10;  /* bypass */
  else if (frequency > 22000000) lpf = 0x20; /* 12/10 meters */
  else if (frequency > 15000000) lpf = 0x40; /* 17/15 meters */
  else if (frequency > 8000000) lpf = 0x01;  /* 30/20 meters */
  else if (frequency > 4500000) lpf = 0x02;  /* 60/40 meters */
  else if (frequency > 2400000) lpf = 0x04;  /* 80 meters */
  else lpf = 0x08;  /* 160 meters */

  // Debugging: print de LPF-waarde
  Serial.print("LPF Waarde: 0x");
  Serial.println(lpf, HEX);
}

// Functie voor het verwerken van andere commando's (0x02 en 0x03)
void handleOtherCommands(unsigned char receivedData[3]) {
  // We kunnen hier de logica voor de commando's 0x02 en 0x03 implementeren.
  // Omdat je zei dat deze genegeerd kunnen worden, kun je hier eventueel iets anders toevoegen als je wilt.

  // Debugging: print het ontvangen commando en de 2 extra bytes
  Serial.print("Commando ontvangen: 0x");
  Serial.println(receivedData[0], HEX);
  Serial.print("Data byte 1: 0x");
  Serial.println(receivedData[1], HEX);
  Serial.print("Data byte 2: 0x");
  Serial.println(receivedData[2], HEX);
}

Op vrijdag 6 september 2024 om 07:43:04 UTC+2 schreef Yado-san:
Hi Wim,

I wondered why Ardiono's code was like this.

This is probably the code for the Generic Filter Switching Board. (when I2C=0x22,  or Alex board).  
If so, the relationship between frequency and code from the RB2 firmware should be as shown in the table below.

fileter1.png

fileter2.png

If the firmware is modified, it should be possible to separate 10m and 12m. (e.g  FILTER_12m = 432;)

fileter4.png


fileter3.png

Yado-san, jg1twp




2024年8月20日火曜日 0:15:25 UTC+9 Wim PE1PWR:
Reply all
Reply to author
Forward
0 new messages