Skanti antennetuner interface, software

SKANTI TUNER INTERFACE

/*
SKANTI 8750 KONTROLBOKS
OZ1OP JUL. 2018

*/

//------------------------------------------
//arduino pin def
//-----------------------------------------
const int swrFwdPin = A0;
const int swrRtnPin = A1;
const int relaeKeyPin = A2;
const int relaePttEnablePin = A3;
const int relaeTunePin = A4;
const int tprPin = A5;
const int tuneKnapPin = A6;
const int auxLedPin = A8;
const int swrLedPin = A9;
const int soundPin = A12;
const int stbyLedPin = A13;
const int encKnapPin = 9;
const int confKnapPin= 10;

//-----------------------------------------
//variable til negDebounce funktion
//------------------------------------------
//negDebounce bruges ved brydekontakter
unsigned long switch_time =0;
const int negDebounceCounter =100;

//------------------------------------------
//encoderfunktioner
//------------------------------------------
#include <Encoder.h>
Encoder Rencoder(20,21);

int encoderPos;
int minPos=1;
int maxPos=2;

//------------------------------------------
//variable til SWR måling
//-----------------------------------------
float fwd = 0; //analogRead fwd 0-1023)
float rtn = 0; //analogRead rtn 0-1023
float korrFwd = 1;
float maxSWR=1.8;
float SWR=0;
float tuneSWR=0;

//-----------------------------------------------
//variable til config
//---------------------------------------------
boolean autoTune = false;
String autoTuneString="AT-";

boolean sound = false;
String soundString="S-";

int tunePwr=20;
String tunePwrString="PC020;";

//-------------------------------------------
//variable til tune funktion
//-------------------------------------------

//-----------------------------------------
//variable til laes frekvens
//-----------------------------------------
String frekvensString="";
float frekvens;
char ch=' ';

//--------------------------------------------
//variabler til laesStatus
//--------------------------------------------
String modeString="";
String pwrString="";

//---------------------------------------------
//variable til error handling
//------------------------------------------
int error=1;
boolean sysKontrol=true;

//--------------------------------------------
// LCD
//--------------------------------------------
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

String lcd01; float lcdTal01;String lcd02;float lcdTal02;
String lcd11; float lcdTal11;String lcd12;float lcdTal12;
String lcd21; float lcdTal21;String lcd22;float lcdTal22;

//**********************************************
void setup() //********************************
//**********************************************

{
//-----------------------------------------------
// pinmode def
//-------------------------------------------------
pinMode(swrFwdPin,INPUT);
pinMode(swrRtnPin,INPUT);

pinMode(tuneKnapPin,INPUT_PULLUP);
pinMode(confKnapPin, INPUT_PULLUP);
pinMode(encKnapPin,INPUT_PULLUP);

pinMode(tprPin,INPUT);

pinMode(relaeKeyPin,OUTPUT);
pinMode(relaePttEnablePin,OUTPUT);
pinMode(relaeTunePin,OUTPUT);

pinMode(stbyLedPin,OUTPUT);
pinMode(swrLedPin,OUTPUT);
pinMode(auxLedPin,OUTPUT);
pinMode(soundPin,OUTPUT);

//-------------------------------------------
// LCD opsaetning
//-------------------------------------------
lcd.begin(20,4);
Serial.begin(9600);

//----------------------------------------------
// velkomst skaerm
//--------------------------------------------
lcd.print("SKANTI 8750 ktr.boks");
lcd.setCursor(0,1); lcd.print("OZ1OP");
delay(2000);
tjekStatus();
frekvens=0;
frekvensString="";
rydLcd();
lcd02="AMP off ";
lcd01="ATU stby ";
digitalWrite(relaePttEnablePin,HIGH); // AMP saettes offline

}
//********************************************
void loop(){ // *********************************
//********************************************
if(error==0){
lcd01="ATU stby ";
lcd02="AMP on ";
}

laesAktuelSWR();
laesFrekvens();

updatLcd();

if(negDebounce(tuneKnapPin)){
tune();
}
if(negDebounce(confKnapPin)){
configProgramMenu();
}

if(autoTune==true){
autotuneFunktion();
}
}

//**********************************************
//loop funktioner************************************
//*********************************************

//-------------------------------------------
void configProgramMenu(){
//----------------------------------------------
minPos=1;
encoderPos=minPos;
maxPos=6; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;

while(true){
// der laves en loop funktion
// encoder aflaeses
//encoderen giver 4 pos pr klik.
//derfor divideres pos med 4
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<minPos){encoderPos=minPos;Rencoder.write(encoderPos*4);}

if(encoderPos==1){
lcd01="CONFIG 1/6";
lcd02="AUTOTUNE";

}

if(encoderPos==2){
lcd01="CONFIG 2/6";
lcd02="TUNE PWR";

}
if(encoderPos==3){
lcd01="CONFIG 3/6";
lcd02="SOUNDSIG";

}

if(encoderPos==4){
lcd01="CONFIG 4/6 ";
lcd02="MAX SWR ";

}

if(encoderPos==5){
lcd01="CONFIG 5/6 ";
lcd02="JUST SWR";
}

if(encoderPos==maxPos){
lcd01="CONFIG 5/7";
lcd02="EXIT ";

}
if(encoderPos==1&&posDebounce(encKnapPin)){configAutoTune();}
if(encoderPos==2&&posDebounce(encKnapPin)){configTunePwr();}
if(encoderPos==3&&posDebounce(encKnapPin)){configSound();}
if(encoderPos==4&&posDebounce(encKnapPin)){configMaxSwr();}
if(encoderPos==5&&posDebounce(encKnapPin)){configJusterSwr();}
if(encoderPos==maxPos&&posDebounce(encKnapPin)){
updatLcd();
break;}

updatLcd();
} // end while
} // end void

//----------------------------------------------
void configJusterSwr(){
//----------------------------------------------
float Jswr;

minPos=10;
maxPos=10000;
encoderPos=100;
Rencoder.write(encoderPos*4);
while(true){

//..........................................
// laes encoder og bestemm korrRtn
//..........................................
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<minPos){encoderPos=minPos;Rencoder.write(encoderPos*4);}

korrFwd=float(encoderPos);
korrFwd=korrFwd/100;
//end laes encoder og bestem korrRtn

//............................................
//laes SWR
//...........................................
fwd=0;rtn=0;

for( int i =0;i<10;i++){
fwd = fwd +analogRead(swrFwdPin);
delay(30);
rtn = rtn +analogRead(swrRtnPin);
delay(30);
}

fwd=fwd/10;
rtn=rtn/10;
SWR=(fwd*korrFwd+rtn)/(fwd*korrFwd-rtn);
// end laes SWR ....................................

//....................................................
//updater Lcd
//...................................................

lcd.setCursor(0,1);lcd.print(korrFwd,2);
lcd.setCursor(0,2);lcd.print(SWR);lcd.print(" ");
//end updater Lcd

//end updat Lcd....................................................

//......................................................
//exit
//..........................................................
if(posDebounce(encKnapPin)){
minPos=1;
maxPos=6;
encoderPos=5;
Rencoder.write(encoderPos*4);
lcd.setCursor(0,1);lcd.print("");
lcd.setCursor(0,2);lcd.print("");

lcd01="CONFIG ";

updatLcd;
break;
}
// end exit.............................................

} // end while
} // end void configJusterSwr()

//------------------------------------------
void configMaxSwr(){
//------------------------------------------

minPos=10;
encoderPos=20;
maxPos=30; // antal menupunkter
Rencoder.write(encoderPos*4);
lcd11="MAX SWR ";
lcd12=" ";
updatLcd;

while(true){
// der laves en loop funktion
// encoder aflaeses
//encoderen giver 4 pos pr klik.
//derfor divideres pos med 4
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<minPos){encoderPos=minPos;Rencoder.write(encoderPos*4);}

lcdTal11=float(encoderPos)/10;
updatLcd();

if(posDebounce(encKnapPin)){
maxSWR=float(encoderPos);
maxSWR=maxSWR/10;

lcd11=" ";
lcd12=" ";
updatLcd:

//....................................................
minPos=1;
encoderPos=4;
maxPos=5; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//....................................................
break;
}

} // end while

} //end void

//------------------------------------------
void configTunePwr(){
//------------------------------------------
minPos=1;
encoderPos=10;
maxPos=50;
// antal menupunkter
Rencoder.write(encoderPos*4);
lcd11="T PWR ";
lcd12="Watt ";
updatLcd;

while(true){
// der laves en loop funktion
// encoder aflaeses
//encoderen giver 4 pos pr klik.
//derfor divideres pos med 4
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<minPos){encoderPos=minPos;Rencoder.write(encoderPos*4);}

if(posDebounce(encKnapPin)){
tunePwr=encoderPos;
tunePwrString=encoderPos;
if(encoderPos>=10){tunePwrString="PC0"+tunePwrString;}
if(encoderPos<10){tunePwrString="PC00"+tunePwrString;}
tunePwrString=tunePwrString+";";
lcd11=" ";
lcd12=" ";
updatLcd:

//...........................................
minPos=1;
encoderPos=2;
maxPos=5;// antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//......................................................
break;
}
lcd11=("T PWR ");
lcd12="Watt ";
lcdTal11=encoderPos;
updatLcd();
} // end while

} //end void

//-------------------------------------------
void configSound(){
//------------------------------------------
encoderPos=1;
maxPos=2; // antal menupunkter
Rencoder.write(encoderPos*4);
lcd11="SOUND SIG ";
updatLcd;

while(true){
// der laves en loop funktion
// encoder aflaeses
//encoderen giver 4 pos pr klik.
//derfor divideres pos med 4
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<1){encoderPos=1;Rencoder.write(encoderPos*4);}

if(encoderPos==1){
lcd12="ON ";
}

if(encoderPos==2){
lcd12="OFF ";
}

updatLcd();

if(posDebounce(encKnapPin)&&encoderPos==1){
sound=true;
soundString="S+ ";
lcd11=" ";
lcd12=" ";

//.................................................
minPos=1;
encoderPos=3;
maxPos=5; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//....................................................
break;
}

if(posDebounce(encKnapPin)&&encoderPos==2){
sound=false;
soundString="S- ";
lcd11=" ";
lcd12=" ";

//.................................................
minPos=1;
encoderPos=3;
maxPos=5; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//....................................................
break;
}

} // end while
} //end void
//-------------------------------------------
void configAutoTune(){
//-------------------------------------------

encoderPos=1;
maxPos=2; // antal menupunkter
Rencoder.write(encoderPos*4);
lcd11="AUTOTUNE ";
updatLcd;

while(true){
// der laves en loop funktion
// encoder aflaeses
//encoderen giver 4 pos pr klik.
//derfor divideres pos med 4
encoderPos=Rencoder.read()/4;
if (encoderPos>maxPos){encoderPos=maxPos;Rencoder.write(encoderPos*4);}
if (encoderPos<1){encoderPos=1;Rencoder.write(encoderPos*4);}

if(encoderPos==1){
lcd12="ON ";
}
if(encoderPos==2){
lcd12="OFF ";
}

updatLcd();

if(encoderPos==1&&posDebounce(encKnapPin)){
autoTune=true;
autoTuneString="AT+ ";
lcd11=" ";
lcd12=" ";
//.................................................
minPos=1;
encoderPos=1;
maxPos=5; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//....................................................
break;
}

if(encoderPos==2&&posDebounce(encKnapPin)){
autoTune=false;
autoTuneString="AT- ";
lcd11=" ";
lcd12=" ";
//.................................................
minPos=1;
encoderPos=1;
maxPos=5; // antal menupunkter
Rencoder.write(encoderPos*4);
nulStilLcdTekst();
lcd01="CONFIG ";
updatLcd;
//....................................................

break;
}
} // end while
}// end void

//-------------------------------------------
void laesAktuelSWR(){
//-------------------------------------------
//laeser aktuel SWR
//hvis rtn > fwd er det fordi antennen modtager stråling
//fra en anden antenne. I så fald sættes SWR til 1 og tekst sættes til 'SWR neg'

fwd=0;rtn=0;

for( int i =0;i<10;i++){
fwd = fwd +analogRead(swrFwdPin);
delay(20);
rtn = rtn +analogRead(swrRtnPin);
delay(20);
}

fwd=fwd/10;
rtn=rtn/10;
SWR=(fwd*korrFwd+rtn)/(fwd*korrFwd-rtn);

//rtn korrigeres for evt u symmetri i retningskobleren med korrRtn
//og SWR udregnes

if (SWR>maxSWR&&fwd>4){
//hvis SWR er høj saettes AMP offline
//SWR led slukker og soundsignal afgives
lcd21="ERR ";
error=1;
lcd22="SWR ";
lcdTal22=SWR;
digitalWrite(relaePttEnablePin,HIGH);
lcd02="AMP off ";
//swrLed blinker
digitalWrite(swrLedPin,HIGH);

delay(50);

digitalWrite(swrLedPin,LOW);
if(sound){
fejlsound();}

}

if (SWR<=maxSWR&&fwd>4){
//hvis SWR er ok, saettes AMP on
digitalWrite(relaePttEnablePin,LOW);
digitalWrite(swrLedPin,HIGH);
lcd02="AMP on ";
error=0;
lcd22="SWR*";
lcdTal22=SWR;
}

if(fwd<4){
//SWR udeffineret
SWR=0;
lcd22="SWR n/a";
lcdTal22=0;
error=0;
}

}

//---------------------------------------------
void laesFrekvens(){
//---------------------------------------------
//laeser frekvens og opdaterer LCD

frekvensString="";
Serial.print("ZZFA;");
delay(100);
while(Serial.available()){
ch=Serial.read();
if(isDigit(ch)){
frekvensString=frekvensString+ch;
}
}
frekvens=frekvensString.toInt();
lcd21="kHz ";
lcdTal21=frekvens/1000;
}

//----------------------------------------------
void tune(){
//----------------------------------------------
lcd.setCursor(0,0);lcd.print("ATU tune AMP off ");

//................................................
//saetter AMP offline
digitalWrite(relaePttEnablePin,HIGH); //amp tx enable afbrydes
delay(300); //venter på relae

//................................................
//laeser TRCV mode
modeString="";
Serial.print("MD;"); // request mode info
delay(100); // venter paa svar fra TRCV
while(Serial.available()){
ch=Serial.read();
modeString=modeString+ch;
}

//.............................................
//laeser PWR
pwrString="";
Serial.print("PC;"); // request pwr info
delay(100); // venter paa svar fra TRCV
while(Serial.available()){
ch=Serial.read();
pwrString=pwrString+ch;
}

//...............................................
//saetter TRCV i tune mode
Serial.print("ZZZTX0;"); //rx mode
delay(200); //venter
Serial.print("ZZTX1;"); //tx mode
Serial.print("MD3;"); //cw mode
Serial.print(tunePwrString); //saetter tune output power

//..................................................
//aktiverer tuning sekvens
digitalWrite(relaeTunePin,HIGH); // giver tuning signl
delay(50); // holder signalet i 50 ms
digitalWrite(relaeTunePin,LOW); // slukker tuning signsl
while(digitalRead(tprPin)==HIGH){ // afventer tpr
}
while (digitalRead(tprPin)==LOW){ // tpr modtaget
digitalWrite(relaeKeyPin,HIGH); // key down
}
digitalWrite(relaeKeyPin,LOW); // key up

//..................................................
// laeser tuneSWR

delay(200);
Serial.write("ZZTX1;");
digitalWrite(relaeKeyPin,HIGH);
delay(200);
fwd=0;rtn=0;
for( int i =0;i<25;i++){
fwd = fwd +analogRead(swrFwdPin);
rtn = rtn +analogRead(swrRtnPin);
}

fwd=fwd/25;
rtn=rtn/25;
tuneSWR=(fwd*korrFwd+rtn)/(fwd*korrFwd-rtn);

digitalWrite(relaeKeyPin,LOW);
Serial.write("ZZTX0;");

//...................................................
//tuning faerdig

//reetablerer status
Serial.print("ZZTX0;"); // reetablerer RX mode
Serial.print(modeString); // reetablerer mode
Serial.print(pwrString); // reetablerer pwr
digitalWrite(relaePttEnablePin,LOW); // reetablerer AMP online

tuneRapport(); //kalder tuneRapport funktion
delay(100);
}

//---------------------------------------------------
void autotuneFunktion(){
//---------------------------------------------------

if (SWR>=1.8){
tune();
}
}

//---------------------------------------------------
void updatLcd(){
//---------------------------------------------------

//linje 0
//.................................................
lcd.setCursor(0,0);
lcd.print(lcd01);
if(lcdTal01>0){
lcd.print(lcdTal01,1);
}
lcd.setCursor(12,0);
lcd.print(lcd02);
if(lcdTal02>0){
lcd.print(lcdTal02,1);
}

//linje 1
//............................................
lcd.setCursor(0,1);
lcd.print(lcd11);
if(lcdTal11>0){
lcd.print(lcdTal11,1);
lcd.print(" ");
}
lcd.setCursor(12,1);
lcd.print(lcd12);
if(lcdTal12>0){
lcd.print(lcdTal12,1);
}

//linje 2
//.........................................
lcd.setCursor(0,2);
lcd.print(lcd21);
if(lcdTal21>0&&lcdTal21<30000){
lcd.print(lcdTal21,1);
lcd.print(" ");
}
lcd.setCursor(12,2);
lcd.print(lcd22);
if(lcdTal22>0){
lcd.print(lcdTal22,1);
}

//linje 3
//.........................................
lcd.setCursor(0,3);lcd.print(autoTuneString);
lcd.setCursor(5,3);lcd.print(soundString);
lcd.setCursor(8,3);lcd.print("TP");lcd.print(tunePwr);lcd.print("W ");
lcd.setCursor(14,3);lcd.print("S");lcd.print(maxSWR,1);

}

//******************************************************
//sub rutiner ******************************************
//******************************************************

//------------------------------------------------------
void tuneRapport(){
//------------------------------------------------------
//sub rutine til void tune()
//tunerapport kaldes efter endt tuning.

lcd12="SWR "; lcdTal12= tuneSWR;
if(tuneSWR>=maxSWR){
//tuning proces gennemført korrekt, men resulterer i høj SWR
digitalWrite(relaePttEnablePin,HIGH);
digitalWrite(swrLedPin,LOW);
fejlsound();
lcd02="AMP off ";
lcd11="*";
}

if(tuneSWR<=maxSWR){
// tuning succes
digitalWrite(swrLedPin,HIGH);
retsound();
lcd01="ATU stby ";
lcd02="AMP on ";
lcd11="kHz ";
lcdTal11=frekvens/1000;
digitalWrite(relaePttEnablePin,LOW);
}

if (fwd<=2){
// fejl: ingen tune pwr
digitalWrite(relaePttEnablePin,HIGH);
fejlsound();
lcd02="AMP off ";
lcd11="ERR ";
lcd12="NO PWR ";
digitalWrite(swrLedPin,LOW);
digitalWrite(stbyLedPin,HIGH);
}
}

//-----------------------------------------------------------
boolean negDebounce(int pin){ //----------------------------
//-----------------------------------------------------------
//debounce funktion til brug ved brydekontakter
boolean state;
boolean previosState;
previosState=digitalRead(pin);
for(int counter=0;counter<negDebounceCounter;counter++){
delay(1);
state=digitalRead(pin);
if(state!=previosState){
counter=0;
previosState=state;
}
}
if (state==HIGH){
switch_time=millis();
while(digitalRead(pin)==HIGH)
{}
switch_time=millis()-switch_time;
if(switch_time>100){
return true;
}
}
else{
return false;
}
}

//-----------------------------------------------------------
boolean posDebounce(int pin){ //----------------------------
//-----------------------------------------------------------
//debounce funktion til brug ved sluttekointakter
boolean state;
boolean previosState;
previosState=digitalRead(pin);
for(int counter=0;counter<negDebounceCounter;counter++){
delay(1);
state=digitalRead(pin);
if(state!=previosState){
counter=0;
previosState=state;
}
}
if (state==LOW){
switch_time=millis();
while(digitalRead(pin)==LOW)
{}
switch_time=millis()-switch_time;
if(switch_time>100){
return true;
}
}
else{
return false;
}
}

//---------------------------------------------
void rydLcd(){
//-------------------------------------------
lcd.setCursor(0,0);lcd.print(" ");
lcd.setCursor(0,1);lcd.print(" ");
lcd.setCursor(0,2);lcd.print(" ");
lcd.setCursor(0,3);lcd.print(" ");

}

//----------------------------------------------------
void rydLed(){
//----------------------------------------------------
digitalWrite(stbyLedPin,LOW);
digitalWrite(swrLedPin,LOW);
digitalWrite(auxLedPin,LOW);
}

//----------------------------------------------------
void fejlsound(){
//----------------------------------------------------
if(sound){
digitalWrite(soundPin,HIGH);
delay(700);
digitalWrite(soundPin,LOW);
delay(60);
digitalWrite(soundPin,HIGH);
delay(100);
digitalWrite(soundPin,LOW);
}
}

//-------------------------------------------------------
void retsound(){
//-------------------------------------------------------
if(sound){
digitalWrite(soundPin,HIGH);
delay(60);
digitalWrite(soundPin,LOW);
}
}

//---------------------------------------------
void tjekStatus(){
//---------------------------------------------
//tjekker om ATU er fysisk tilkoblet til kongtrolboksen
//tjekker om TRCV returnerer frekvensoplysning
//error 1 = fejl på PC/TRCV
//error 2 = fejl på Tuner
rydLcd();
lcd.setCursor(0,0);
lcd.print("SYS KONTROL");
sysKontrol=true;
while(sysKontrol){
laesFrekvens();
if(frekvens>10000&&digitalRead(tprPin)==HIGH){
error=0;
lcd.setCursor(0,2);
lcd.print("SYS KONTROL OK");
digitalWrite(stbyLedPin,HIGH);

delay(2000);
lcd01="SYS stby ";
sysKontrol=false;
}

if(frekvens<10000){
error=1;
lcd.setCursor(0,2);
lcd.print("FEJL PC/TRCV ");
rydLed();
fejlsound();
}

if(digitalRead(tprPin)==LOW){
error=2;
lcd.setCursor(0,2);
lcd.print("FEJL TUNER ");
rydLed;
fejlsound();
}
}
}

//--------------------------------------------------
void nulStilLcdTekst(){
//--------------------------------------------------
lcd01=" ";lcdTal01=0;lcd02=" ";lcdTal01=0;
lcd11=" ";lcdTal11=0;lcd12=" ";lcdTal11=0;
lcd21=" ";lcdTal21=0;lcd22=" ";lcdTal22=0;

}

Del denne side
FacebooktwitterlinkedinmailFacebooktwitterlinkedinmail