-
Notifications
You must be signed in to change notification settings - Fork 4
/
QRP_POWER_METER.ino
172 lines (144 loc) · 6.34 KB
/
QRP_POWER_METER.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// https://g8gyw.github.io/
//
// Copyright (c) 2021 Mike G8GYW
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// Use of the MegunoLINK filter library is licensed by the copyright owners under the terms of the
// GNU Lesser General Public License: https://github.com/Megunolink/MLP/blob/master/LICENSE.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// This program is designed to run on an Atmega328P with internal 8MHz clock and internal bandgap voltage reference.
// It takes the outputs from a Stockton bridge using a 10:1 transformer turns ratio and 1N5711 Scottky diodes,
// applies calibration factors then calculates and displays the average forward power, VSWR and battery voltage.
// ATMEGA FUSE VALUES
// E:FF H:D7 L:E2
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Filter.h>
#define VERSION "v1.06"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DEFINE VARIABLES
float Vfwd; // Forward voltage
float Vrev; // Reverse voltage
float Pfwd; // Forward power
float Prev; // Reverse power
float SWR; // VSWR
float Gamma; // Reflection coefficient
float Vbat; // Battery voltage
// CALIBRATION FACTORS
// The tolerance on the Atmega328P's internal voltage reference is 1.0 to 1.2 Volts.
// The actual voltage should be measured on pin 21 and entered below.
// The response of the 1N5711 diodes follows a curve represented by the equation:
// Pfwd = aVfwd^2 + bVfwd (and the same for Prev and Vrev)
// where a and b are constants determined by plotting Power In vs Vfwd and performing a curve fit.
// An excellent tool for this can be found at https://veusz.github.io/
// The values of a and b below can be adjusted if necessary to improve accuracy (try adjusting b first).
const float IntRef = 1.1;
const float a = 1.0;
const float b = 0.75;
// FUNCTIONS TO CALCULATE FORWARD AND REVERSE POWER
float CalculatePfwd ()
{
float Vadc0 = analogRead(A0); // Read ADC0 (pin 23)
Vadc0 = constrain(Vadc0, 1, 1023); // Prevent divide-by-zero when calculating Gamma
Vfwd = 2.8 * ((Vadc0 + 0.5) * IntRef / 1024); // Scaled up by R3 & R4
Pfwd = a * sq(Vfwd) + b * Vfwd;
return Pfwd;
}
float CalculateSWR ()
{
float Vadc1 = analogRead(A1); // Read ADC1 (pin 24)
Vrev = 2.8 * ((Vadc1 + 0.5) * IntRef / 1024); // Scaled up by R7 & R8
Prev = a * sq(Vrev) + b * Vrev;
if (Prev < 0.01)
{ //exclude residual values
Prev = 0;
}
Gamma = sqrt(Prev / Pfwd); // Calculate reflection coefficient
SWR = (1 + Gamma) / (1 - Gamma);
SWR = constrain(SWR, 1, 99.9);
return SWR;
}
// ADC FILTER
// A recursive filter removes jitter from the readings using the MegunoLINK filter library:
// https://www.megunolink.com/documentation/arduino-libraries/exponential-filter/
// Create new exponential filters with a weight of 90 and initial value of 0
// Adjust these values as required for a stable display
ExponentialFilter<float> FilteredPfwd(50, 0);
ExponentialFilter<float> FilteredVSWR(50, 0);
void setup()
{
ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // Clear prescaler bits
ADCSRA |= bit (ADPS1) | bit (ADPS2); // Set prescaler to 64
// Enable the internal voltage reference and disable unused digital buffers
analogReference (INTERNAL);
bitSet (DIDR0, ADC3D);
bitSet (DIDR0, ADC4D);
bitSet (DIDR0, ADC5D);
analogRead (A2); // Read ADC2
delay (500); // Allow ADC to settle
float Vpot = analogRead (A2); // Read ADC again
Vbat = 4.9 * ((Vpot + 0.5) * IntRef / 1024); // Calculate battery voltage scaled by R9 & R10
// Display startup screen
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize with the I2C address 0x3C.
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(" POWER/SWR METER ");
display.setCursor(0, 16);
display.print(" 12 Watts maximum ");
display.setCursor(0, 32);
display.print("Battery volts = ");
display.print(Vbat);
display.setCursor(0, 48);
display.print("--------");
display.print(VERSION);
display.print("--------");
display.display();
delay(5000); // delay 5 seconds
}
void loop()
{
float Power = CalculatePfwd(); // Get new value of forward power
FilteredPfwd.Filter(Power); // Apply filter to new value
float SmoothPower = FilteredPfwd.Current(); // Return current value of filter output
float VSWR = CalculateSWR(); // Get new value of VSWR
FilteredVSWR.Filter(VSWR); // Apply filter to new value
float SmoothVSWR = FilteredVSWR.Current(); // Return current value of filter output
// Display Power and VSWR
display.clearDisplay();
display.setTextSize(2);
display.setCursor (12, 12);
display.print("PWR: ");
display.setCursor (60, 12);
display.print(SmoothPower, 1); // Display forward power to one decimal place
display.print("W");
display.setCursor (12, 36);
display.print("SWR: ");
display.setCursor (60, 36);
if (Pfwd < 0.01)
{ display.print("**.*"); // Blank invalid readings
}
else {
display.print(SmoothVSWR, 1); // Display VSWR to one decimal place
}
display.display();
}