Band decoder for HF amplifiers

I needed a simple controller for my HF amplifier that I could connect to band-data port on my K3 – also having 3 buttons on front panel chosing auto or manual band data.

I actually connected LED’s to each relay to track status – pretty basic but works. There is no interlock so it’s possible to push the front panel button while in TX – it’s easy to fix – tie a input into the PTT line and disable the buttons. Will be in v2 šŸ˜‰

 

Using a teensy2++ Atmel breakout. Could use any Arduino board out there. I use ULN 2803 buffers (surface mount) – anything will work.

The input diodes are pain in the #”$#% if using single side PCB and SMD so I omitted them, but better having them.

 

code:

I don’t comment to much, the code is overly complex as I reuse some of the code from more complex applications but it works very well. 

I borrowed some parts of the BCD decoding from remoteQTH.

 

#include <stdio.h>
#include <Arduino.h>

#define PORT_BANDA			38	  // SV5 -10
#define PORT_BANDB			39	  // SV5 -9
#define PORT_BANDC			40    // SV5 -8
#define PORT_BANDD			41    // SV5 -7


#define PORT_LPF_80m	  	10		// SV26 - 9
#define PORT_LPF_4030m		11		// SV26 - 8
#define PORT_LPF_2015m		12		// SV26 - 7
#define PORT_LPF_1012m		13		// SV26 - 6
#define PORT_LPF_6m		  	16		// SV26 - 5



#define PORT_OUTPUT_160LED    22
#define PORT_OUTPUT_AUTOLED   23
#define PORT_OUTPUT_CTRL_ON   21
#define PORT_OUTPUT_CTRL_STBY 20




#define PORT_ANT1RELAY		26		// SV27 - 8
#define PORT_ANT2RELAY		25		// SV27 - 8
#define PORT_ATTRELAY    	24		// SV27 - 8


#define PORT_FP_BANDUP        5
#define PORT_FP_BANDDOWN      4
#define PORT_FP_ANT  		      3
#define PORT_FP_ATTENUATOR	  1

//#define PORT_FP_RESET         44
//#define PORT_FP_STANDBY       39
//#define PORT_FP_DISPLAY		  40



boolean BCDmatrix[4][15] = {
 /*

=======[ Input BCD ]====================================================================

          BCD 1 --> */ { 0,  1,  0,  1,  0,  1,  0,  1,  0,  1,  0,  1,  0,  1,  0 }, /*
          BCD 2 --> */ { 0,  0,  1,  1,  0,  0,  1,  1,  0,  0,  1,  1,  0,  0,  1 }, /*
          BCD 3 --> */ { 0,  0,  0,  0,  1,  1,  1,  1,  0,  0,  0,  0,  1,  1,  1 }, /*
          BCD 4 --> */ { 0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1 }, /*
                         |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
                         V   V   V   V   V   V   V   V   V   V   V   V   V   V   V
-------------------------------------------------------------------------------------
Band # in matrix table   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14
Yaesu BCD                   160 80  40  30  20  17  15  12  10  6m  <--- free --->     FT-1000MP ?
-------------------------------------------------------------------------------------

========================================================================================
*/
};

uint16_t BUTTSTATUS ; // status of the BUTTons
 
#define P_BANDDOWN 		(1<<1)
#define P_BANDUP 		  (1<<2)
#define P_RESET 		  (1<<3)
#define P_OPER 			  (1<<4)
#define P_ATTENUATOR 	(1<<5)
#define P_LONGPUSH 		(1<<6)
#define P_ANT 			  (1<<7)
#define P_DISPLAY 		(1<<8)


uint16_t    STATUS=0;
#define ALARM         (1<<1)
#define PTT           (1<<5)
#define ATTENUATOR    (1<<10)
#define BLIKK         (1<<4)


uint8_t band=0;
uint8_t currentautoband=0;
uint8_t currentband=0;
uint8_t autoband = 0;
unsigned long prevtimer_10ms = 0; 
unsigned long prevtimer_100ms = 0; 
uint8_t ANTENNA = 1;

void setup()
{

//  pinMode(PORT_LPF_160m,OUTPUT);
  pinMode(PORT_LPF_80m,   OUTPUT);
  pinMode(PORT_LPF_4030m, OUTPUT);
  pinMode(PORT_LPF_2015m, OUTPUT);
  pinMode(PORT_LPF_1012m, OUTPUT);
  pinMode(PORT_LPF_6m,    OUTPUT);

  pinMode(PORT_ANT1RELAY,OUTPUT);
  pinMode(PORT_ANT2RELAY,OUTPUT);
  pinMode(PORT_ATTRELAY,OUTPUT);


  pinMode(PORT_BANDA,	INPUT_PULLUP);
  pinMode(PORT_BANDB,	INPUT_PULLUP);
  pinMode(PORT_BANDC,	INPUT_PULLUP);
  pinMode(PORT_BANDD,	INPUT_PULLUP);

  pinMode(PORT_FP_BANDUP,   INPUT_PULLUP);
  pinMode(PORT_FP_BANDDOWN,   INPUT_PULLUP);
  pinMode(PORT_FP_ATTENUATOR, INPUT_PULLUP);
  pinMode(PORT_FP_ANT,     INPUT_PULLUP);

  pinMode(PORT_OUTPUT_160LED,OUTPUT);
  pinMode(PORT_OUTPUT_AUTOLED,OUTPUT);
  pinMode(PORT_OUTPUT_CTRL_ON,OUTPUT);
  pinMode(PORT_OUTPUT_CTRL_STBY,OUTPUT);


   digitalWrite(PORT_ANT1RELAY,LOW);
   digitalWrite(PORT_ANT2RELAY,LOW);
   digitalWrite(PORT_ATTRELAY,LOW);

// misc stuff - not really needed - using it to turn on a second controller.
  digitalWrite(PORT_OUTPUT_CTRL_ON, HIGH);
  delay(500);
  digitalWrite(PORT_OUTPUT_CTRL_ON, LOW);
  delay(500);
  digitalWrite(PORT_OUTPUT_CTRL_STBY, HIGH);
  delay(500);
  digitalWrite(PORT_OUTPUT_CTRL_STBY, LOW);
  

  UPDATE_BAND();
}


void buttonscan()
{

// uint8_t buttons = readButtons();

 uint16_t buttons = 0;
 buttons=0xffff;





 buttons  &= ~((digitalRead(PORT_FP_BANDUP)) << 1);
 buttons  &= ~((digitalRead(PORT_FP_BANDDOWN)) << 2);
// buttons  &= ~((digitalRead(PORT_FP_RESET)) << 3);
// buttons  &= ~((digitalRead(PORT_FP_STANDBY)) << 4);
 buttons  &= ~((digitalRead(PORT_FP_ATTENUATOR)) << 5);
 buttons  &= ~((digitalRead(PORT_FP_ANT)) << 7);
// buttons  &= ~((digitalRead(PORT_FP_DISPLAY)) << 8);


// ***** BANDDOWN ******
 


// BANDUP
 
 if ( (buttons & P_BANDUP) && (!(BUTTSTATUS & P_BANDUP)) ) // we pushed it
 {
 	BUTTSTATUS |= P_BANDUP; // we pushed it
 }
 else if ((buttons & P_BANDUP) && (BUTTSTATUS & P_BANDUP)) // we are pushing it
 {
 
 }
 else if ((!(buttons & P_BANDUP)) && (BUTTSTATUS & P_BANDUP)) // we released it
 {
 	BUTTSTATUS &= ~P_BANDUP;
 	if (!(STATUS & PTT))
 	{
 	if (band < 6 ) { band++; UPDATE_BAND(); // setband(band); } } } // ***** BANDDOWN ****** if ( (buttons & P_BANDDOWN) && (!(BUTTSTATUS & P_BANDDOWN)) ) // we pushed it { BUTTSTATUS |= P_BANDDOWN; // we pushed it } else if ((buttons & P_BANDDOWN) && (BUTTSTATUS & P_BANDDOWN)) // we are pushing it { } else if ((!(buttons & P_BANDDOWN)) && (BUTTSTATUS & P_BANDDOWN)) // we released it { BUTTSTATUS &= ~P_BANDDOWN; if (!(STATUS & PTT)) { if (band > 0 ) 
  			{
		  		band--;
                 UPDATE_BAND();
          //setband(band);
			}

 	}
 }
 
// ***** BANDDOWN ******


// *****   ANTENNA SELECTOR ****** 
 if ( (buttons & P_ANT) && (!(BUTTSTATUS & P_ANT)) ) // we pushed it
 {
 	BUTTSTATUS |= P_ANT; // we pushed it
 }
 else if ((buttons & P_ANT) && (BUTTSTATUS & P_ANT)) // we are pushing it
 {
 
 }
 
 else if ((!(buttons & P_ANT)) && (BUTTSTATUS & P_ANT)) // we released it
 {
  BUTTSTATUS &= ~P_ANT;


  if ( (!(STATUS & PTT))  && (!(STATUS & ALARM)) ) 
  {
    ANTENNA++;
    if (ANTENNA > 4) ANTENNA = 1;
    SET_ANTENNA();
	
  }
  
 
 }




// ATTENUATOR 
 if ( (buttons & P_ATTENUATOR) && (!(BUTTSTATUS & P_ATTENUATOR)) ) // we pushed it
 {
 	BUTTSTATUS |= P_ATTENUATOR; // we pushed it
 }
 else if ((buttons & P_ATTENUATOR) && (BUTTSTATUS & P_ATTENUATOR)) // we are pushing it
 {
 
 }
 
 else if ((!(buttons & P_ATTENUATOR)) && (BUTTSTATUS & P_ATTENUATOR)) // we released it
 {
 	BUTTSTATUS &= ~P_ATTENUATOR;

 	if ( (!(STATUS & PTT))  && (!(STATUS & ALARM)) )

 	{
 		
		STATUS ^=ATTENUATOR;

		if (STATUS & ATTENUATOR) 
		  {
		  	digitalWrite(PORT_ATTRELAY,HIGH);
		  }
		else
		  {
		  	digitalWrite(PORT_ATTRELAY,LOW);
		  }
		
    }
 }
  // ATTENUATOR 




}  // Buttonscan




void READ_BAND()
{


    int BCD1,BCD2,BCD3,BCD4;
    int bandBCD;
 

 BCD1 =  digitalRead(PORT_BANDA);
 BCD2 =  digitalRead(PORT_BANDB);
 BCD3 =  digitalRead(PORT_BANDC);
 BCD4 =  digitalRead(PORT_BANDD);


for (bandBCD=0; bandBCD<15; bandBCD++){ if (BCDmatrix[0][bandBCD]==BCD1 && BCDmatrix[1][bandBCD]==BCD2 && BCDmatrix[2][bandBCD]==BCD3 && BCDmatrix[3][bandBCD]==BCD4){ autoband=bandBCD; } } } void UPDATE_BAND() { if ( (band>0) && (band<7) ) 
  {
    setband(band);
    digitalWrite(PORT_OUTPUT_AUTOLED,LOW);
  }
// 

 else if (band==0)    // we are in auto 
  {
   digitalWrite(PORT_OUTPUT_AUTOLED,HIGH);
//    lcd.setCursor(5,3);
//    lcd.print("AUTO:");

   if (autoband==0)
    {
   // lcd.print("NOBAND!");
   // STATUS |= INHIBIT;      // let's inhibit transmit at this point
    }
    else if (autoband == 1)
    {
     setband(1);
    }
    else if (autoband == 2)
    {
     setband(2);

    }
    else if ((autoband == 3) || (autoband == 4) )    // 40-30
    {
     setband(3);

    }
      
    else if ((autoband == 5) || (autoband == 6) || (autoband == 7))    // 40-30
    {
     setband(4);
    }
    else if ((autoband == 8) || (autoband == 9) )    // 40-30
    {
     setband(5);
    }
  else if (autoband == 10)
    {
     setband(6);
    }
    
   currentband = band;
   currentautoband = autoband;
  }
}


void setband(int i)
{
 
   int matrix[6][6] =
  {

    {0,0,0,0,0,0}, // 160     1
    {0,0,0,0,1,0}, // 80      2
    {0,0,0,1,0,0}, // 40-30   3
    {0,0,1,0,0,0}, // 20-15   4
    {0,1,0,0,0,0}, // 10-12   5
    {1,0,0,0,0,0} // 6       6

  };

switch (i) {


case 1:  //160

 digitalWrite(PORT_LPF_80m,    LOW);
 digitalWrite(PORT_LPF_4030m,  LOW);
 digitalWrite(PORT_LPF_2015m,  LOW);
 digitalWrite(PORT_LPF_1012m,  LOW);
 digitalWrite(PORT_LPF_6m,     LOW);

 digitalWrite(PORT_OUTPUT_160LED,HIGH);
 break;

case 2:  // 80


 digitalWrite(PORT_LPF_80m,    HIGH);
 digitalWrite(PORT_LPF_4030m,  LOW);
 digitalWrite(PORT_LPF_2015m,  LOW);
 digitalWrite(PORT_LPF_1012m,  LOW);
 digitalWrite(PORT_LPF_6m,     LOW);
 digitalWrite(PORT_OUTPUT_160LED,LOW);
 break;

case 3:  //40-30

  digitalWrite(PORT_LPF_80m,    LOW);
  digitalWrite(PORT_LPF_4030m,  HIGH);
  digitalWrite(PORT_LPF_2015m,  LOW);
  digitalWrite(PORT_LPF_1012m,  LOW);
  digitalWrite(PORT_LPF_6m,     LOW);
    digitalWrite(PORT_OUTPUT_160LED,LOW);
 break;


case 4:  // 20/15

  digitalWrite(PORT_LPF_80m,    LOW);
  digitalWrite(PORT_LPF_4030m,  LOW);
  digitalWrite(PORT_LPF_2015m,  HIGH);
  digitalWrite(PORT_LPF_1012m,  LOW);
  digitalWrite(PORT_LPF_6m,     LOW);
    digitalWrite(PORT_OUTPUT_160LED,LOW);
 break;

case 5: // 10-12

  digitalWrite(PORT_LPF_80m,    LOW);
  digitalWrite(PORT_LPF_4030m,  LOW);
  digitalWrite(PORT_LPF_2015m,  LOW);
  digitalWrite(PORT_LPF_1012m,  HIGH);
  digitalWrite(PORT_LPF_6m,     LOW);
  digitalWrite(PORT_OUTPUT_160LED,LOW);
 break;

case 6:  // 6m

  digitalWrite(PORT_LPF_80m,    LOW);
  digitalWrite(PORT_LPF_4030m,  LOW);
  digitalWrite(PORT_LPF_2015m,  LOW);
  digitalWrite(PORT_LPF_1012m,  LOW);

  digitalWrite(PORT_LPF_6m,     HIGH);
  
  digitalWrite(PORT_OUTPUT_160LED,LOW);
 break;
}
// STATUS &= ~INHIBIT;  // ok to transmit

}



void SET_ANTENNA()
{


switch (ANTENNA) {

 if ( (ANTENNA<1) || (ANTENNA>4) )
 {
  STATUS |= ALARM;
 }
 else
 {


case 1:
  digitalWrite(PORT_ANT1RELAY,LOW);
  digitalWrite(PORT_ANT2RELAY,LOW);

 break;

case 2:
  digitalWrite(PORT_ANT1RELAY,HIGH);
  digitalWrite(PORT_ANT2RELAY,HIGH);

 break;

case 3:
  digitalWrite(PORT_ANT1RELAY,LOW);
  digitalWrite(PORT_ANT2RELAY,HIGH);

 break;

case 4:
  digitalWrite(PORT_ANT1RELAY,HIGH);
  digitalWrite(PORT_ANT2RELAY,LOW);
 break;
 }

}

}



void loop()
{


// timed intervals
  
   unsigned long timer10ms= millis();

  // 10ms stuff to do

  if (timer10ms - prevtimer_10ms >= 10) {
    buttonscan();
    prevtimer_10ms = timer10ms;
    
   }

   unsigned long timer100ms= millis();

  if (timer100ms - prevtimer_100ms >= 100) {
    
    prevtimer_100ms = timer100ms;

 

    READ_BAND();

    if ( (band != currentband) || (currentautoband != autoband) )
       { 
       	 UPDATE_BAND();
       }

   
   }

}