Skip to content

Instantly share code, notes, and snippets.

@jackdev23
Created December 18, 2013 18:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jackdev23/8027470 to your computer and use it in GitHub Desktop.
Arduino: WS_KL_1_1
////////////////////////////////
//WS -Swr/Wattmeter- KL V1.1 //
///////////////////////////////
/////////////////////////////
//WATTMETER/SWR analyzer KL V1.1 //
/////////////////////////////
/** Copyright 2014 Luca Facchinetti, IW2NDH
All trademarks referred to in source code and documentation are copyright their respective owners.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
If you offer a hardware kit using this software, show your appreciation by sending the author
a complimentary kit or a donation to a cats refuge ;-)
*/
#include <stdio.h>
#include <TimerOne.h>
#include "U8glib.h"
/**LCD5110 myGLCD(8,9,10,12,11);
*/
U8GLIB_PCD8544 u8g(8, 9, 11, 10, 13);
int scaleSetHigh = 5;
float DbCoupler = 18.0;//8 WOUND = 18.0 Db & 27 Db 14/140 Watt _ 10 WOUND = 20.0 Db & 29 Db 20/200 Watt
float step = 0.1;
bool scale200;
const int thresholdScale = 850; //966; //20 watt = 43 dBm , 43 - 20 = 23 dBm = 3.2Vrms = 4.5 Vp = 5V *(925/1023)
float PfromV(float volt){
float p ;
float rms;
rms = 0.707*(volt);
p = pow(rms,2);
p = p/(55.0*0.001);
p = 10 * log10(p);
p = p + DbCoupler;
p= p / 10.0;
p = pow(10,p);
p = p * 0.001 ;
return p;
}
void barra(u8g_uint_t pos, float val){
u8g.drawBox(0 , pos, 82*((min(val,5.0))/5.0), 6);
u8g.drawFrame(0 , pos, 82, 6);
}
void barraSwr(u8g_uint_t pos, float val){
u8g.drawBox(0 , pos, 82*((min(val-1.0,2.0))/2.0), 6);
u8g.drawFrame(0 , pos, 82, 6);
u8g.drawLine(41 , pos, 41, pos+5);
}
// the setup routine runs once when you press reset:
void setup() {
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
u8g.setDefaultForegroundColor();
u8g.setFont(u8g_font_5x7);
u8g.setRot180();
Serial.begin(9600);
pinMode(scaleSetHigh, OUTPUT);
Timer1.initialize(1000000); // set a timer of length 1000000 microseconds (or 1 sec - or 1Hz => )
Timer1.attachInterrupt( timerIsr ); // attach the service routine here
Timer1.stop();
digitalWrite(scaleSetHigh, LOW); // turn the scale on 20W (HIGH is the voltage level)
}
void set200Scale(){
scale200 = true;
DbCoupler = 27.0;
step = step / 3.0;
digitalWrite(scaleSetHigh, HIGH); // turn the scale on 200W (HIGH is the voltage level)
Timer1.resume();
delay(50);
}
void draw() {
// the loop routine runs over and over again forever:
float PForward;
float PReverse;
float ro;
float swr;
// read the input on analog pin 0
int gap;
int rev;
int fw = analogRead(A2); ///forward vicino antenna
if (fw > thresholdScale && !scale200) {
set200Scale();
fw = analogRead(A2); ///forward vicino antenna
}
gap = analogRead(A0);
rev = analogRead(A1);
float voltgap = gap * (5.0 / 1023.0) + step;
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltfw = fw * (5.0 / 1023.0);
// if (voltfw <= voltgap ) voltfw = 0.0 ;
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltrev = rev * (5.0 / 1023.0);
// if (voltrev <= voltgap ) voltrev = 0.0 ;
// print out the value you read:
// Serial.print(voltgap);
// Serial.print(" A0 \t");
// Serial.print(fw);
// Serial.print(" A2 \t");
// //Serial.print(PfromV(voltfw));
// Serial.print(" A1 \t");
// Serial.print(voltrev);
// Serial.println("\t");
// //Serial.println(PfromV(voltrev));
PForward = PfromV(voltfw);
PReverse = PfromV(voltrev);
if (voltgap > voltrev){
swr = 1.01;
}else{
ro = sqrt(PReverse/PForward);
swr = (1 + ro)/(1-ro);
}
u8g.setPrintPos(0, 14);
u8g.print("Forward");
u8g.setPrintPos(0, 29);
u8g.print("Reverse");
u8g.setPrintPos(25, 45);
u8g.print("SWR");
u8g.setPrintPos(0, 45);
if(!scale200) {
u8g.print("LOW");
}
else{
u8g.print("HIGH");
}
u8g.setPrintPos(50, 14);
u8g.print(PForward, 1);
u8g.setPrintPos(50, 29);
u8g.print(PReverse, 1);
u8g.setPrintPos(50, 45);
u8g.print(swr, 2);
barra(0,voltfw);
barra(16,voltrev);
barraSwr(31,swr);
}
void loop(void) {
u8g.firstPage();
do {
delay(10);
draw();
} while( u8g.nextPage() );
}
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
Timer1.stop();
digitalWrite(scaleSetHigh, LOW); // turn the scale on 20W (HIGH is the voltage level)
scale200 = false;
DbCoupler = 18.0;
step = 0.1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment