Update to AD8302

This commit is contained in:
jupfi 2023-08-08 17:09:36 +02:00
parent 14f911ff03
commit eff4b850ba
3 changed files with 63 additions and 22 deletions

View file

@ -31,5 +31,5 @@
#define REFLECTION_PIN 15 #define REFLECTION_PIN 15
// Filter Bank // Filter Bank
#define FILTER_SWITCH_A 22 #define FILTER_SWITCH_A 23
#define FILTER_SWITCH_B 23 #define FILTER_SWITCH_B 22

View file

@ -16,11 +16,14 @@ struct FrequencyRange
uint32_t MATCHING_CENTER_POSITION; uint32_t MATCHING_CENTER_POSITION;
}; };
const Filter FG_71MHZ = {71000000U, HIGH, HIGH}; const Filter FG_71MHZ = {71000000U, LOW, LOW};
const Filter FG_120MHZ = {120000000U, LOW, HIGH}; const Filter FG_120MHZ = {120000000U, LOW, HIGH};
const Filter FG_180MHZ = {180000000U, LOW, LOW}; const Filter FG_180MHZ = {180000000U, HIGH, LOW};
const Filter FG_260MHZ = {260000000U, HIGH, LOW}; const Filter FG_260MHZ = {260000000U, HIGH, LOW};
// All fitlers
const Filter FILTERS[] = {FG_71MHZ, FG_120MHZ, FG_180MHZ, FG_260MHZ};
// Settings for 100MHz -18dB // Settings for 100MHz -18dB
//#define TUNING_STEPPER_HOME 34250U //#define TUNING_STEPPER_HOME 34250U
//#define MATCHING_STEPPER_HOME 45000U //#define MATCHING_STEPPER_HOME 45000U

View file

@ -32,9 +32,8 @@ Stepper matcher = {matching_stepper, matching_driver, DIAG1_PIN_M2, "Matcher"};
// ADC DAC Module // ADC DAC Module
AD5593R adac = AD5593R(23, I2C_SDA, I2C_SCL); AD5593R adac = AD5593R(23, I2C_SDA, I2C_SCL);
bool DACs[8] = {0,1,1,0,0,0,0,0}; bool DACs[8] = {0, 0, 1, 1, 0, 0, 0, 0};
bool ADCs[8] = {1,0,0,0,0,0,0,0}; bool ADCs[8] = {1, 1, 0, 0, 0, 0, 0, 0};
boolean homed = false; boolean homed = false;
@ -92,7 +91,7 @@ void setup()
adf4351.begin(); adf4351.begin();
adf4351.setrf(25000000U); adf4351.setrf(25000000U);
adf4351.pwrlevel = 2; // This equals -4dBm*/ adf4351.pwrlevel = 0; // This equals -4dBm*/
adf4351.setf(START_FREQUENCY); adf4351.setf(START_FREQUENCY);
pinMode(FILTER_SWITCH_A, OUTPUT); pinMode(FILTER_SWITCH_A, OUTPUT);
@ -192,9 +191,8 @@ void loop()
} }
else if (command == 'f') else if (command == 'f')
{ {
Serial.println("Frequency sweep...");
uint32_t resonance_frequency = findCurrentResonanceFrequency(START_FREQUENCY, STOP_FREQUENCY, FREQUENCY_STEP / 2); uint32_t resonance_frequency = findCurrentResonanceFrequency(START_FREQUENCY, STOP_FREQUENCY, FREQUENCY_STEP / 2);
Serial.println("Resonance is at:"); Serial.print("r");
Serial.println(resonance_frequency); Serial.println(resonance_frequency);
// calculates Reflection loss for a given frequency // calculates Reflection loss for a given frequency
@ -265,7 +263,7 @@ uint32_t validateInput(float frequency_MHz)
Serial.println("Invalid input: frequency too low"); Serial.println("Invalid input: frequency too low");
return 0; return 0;
} }
else if (frequency_Hz > STOP_FREQUENCY) else if (frequency_Hz > 300000000U)
{ {
Serial.println("Invalid input: frequency too high"); Serial.println("Invalid input: frequency too high");
return 0; return 0;
@ -285,6 +283,14 @@ int readReflection(int averages)
return reflection / averages; return reflection / averages;
} }
int readPhase(int averages)
{
int phase = 0;
for (int i = 0; i < averages; i++)
phase += (adac.read_ADC(1) * 1000);
return phase / averages;
}
void getCalibrationValues() void getCalibrationValues()
{ {
uint32_t resonance_frequency = findCurrentResonanceFrequency(START_FREQUENCY, STOP_FREQUENCY, FREQUENCY_STEP); uint32_t resonance_frequency = findCurrentResonanceFrequency(START_FREQUENCY, STOP_FREQUENCY, FREQUENCY_STEP);
@ -384,8 +390,7 @@ uint32_t automaticTM(uint32_t target_frequency)
// 24mV/dB slope // 24mV/dB slope
float calculateRL(uint32_t frequency) float calculateRL(uint32_t frequency)
{ {
adf4351.setf(frequency); setFrequency(frequency);
delay(100);
float reflection = readReflection(64); float reflection = readReflection(64);
@ -402,6 +407,7 @@ int32_t findCurrentResonanceFrequency(uint32_t start_frequency, uint32_t stop_fr
{ {
int maximum_reflection = 0; int maximum_reflection = 0;
int current_reflection = 0; int current_reflection = 0;
int current_phase = 0;
uint32_t minimum_frequency = 0; uint32_t minimum_frequency = 0;
float reflection = 0; float reflection = 0;
@ -410,17 +416,16 @@ int32_t findCurrentResonanceFrequency(uint32_t start_frequency, uint32_t stop_fr
for (uint32_t frequency = start_frequency; frequency <= stop_frequency; frequency += frequency_step) for (uint32_t frequency = start_frequency; frequency <= stop_frequency; frequency += frequency_step)
{ {
adf4351.setf(frequency); //adf4351.setf(frequency);
setFrequency(frequency);
delay(5); // This delay is essential! There is a glitch with ADC2 that leads to wrong readings if GPIO27 is set to high for multiple microseconds. // delay(5); // This delay is essential! There is a glitch with ADC2 that leads to wrong readings if GPIO27 is set to high for multiple microseconds.
current_reflection = readReflection(4); current_reflection = readReflection(4);
current_phase = readPhase(4);
// Send out the frequency identifier f with the frequency value // Send out the frequency identifier f with the frequency value
Serial.print("f"); Serial.println(String("f") + frequency + "r" + current_reflection + "p" + current_phase);
Serial.print(frequency);
Serial.print("r");
Serial.println(current_reflection);
if (current_reflection > maximum_reflection) if (current_reflection > maximum_reflection)
{ {
@ -458,6 +463,39 @@ int32_t findCurrentResonanceFrequency(uint32_t start_frequency, uint32_t stop_fr
return minimum_frequency; return minimum_frequency;
} }
void setFrequency(uint32_t frequency)
{
// First we check what filter has to be used from the FILTERS array
// Then we set the filterbank accordingly
for (int i = 0; i < sizeof(FILTERS) / sizeof(FILTERS[0]); i++)
{
// For the first filter we just check if the frequency is below the fg
if ((i == 0) && (frequency < FILTERS[i].fg))
{
digitalWrite(FILTER_SWITCH_A, FILTERS[i].control_input_a);
digitalWrite(FILTER_SWITCH_B, FILTERS[i].control_input_b);
break;
}
// For the last filter we just check if the frequency is above the fg
else if ((i == sizeof(FILTERS) / sizeof(FILTERS[0]) - 1) && (frequency > FILTERS[i].fg))
{
digitalWrite(FILTER_SWITCH_A, FILTERS[i].control_input_a);
digitalWrite(FILTER_SWITCH_B, FILTERS[i].control_input_b);
break;
}
// For the filters in between we check if the frequency is between the fg and the fg of the previous filter
else if ((frequency < FILTERS[i].fg) && (frequency > FILTERS[i - 1].fg))
{
digitalWrite(FILTER_SWITCH_A, FILTERS[i].control_input_a);
digitalWrite(FILTER_SWITCH_B, FILTERS[i].control_input_b);
break;
}
}
// Finally we set the frequency
adf4351.setf(frequency);
}
// Tries out different capacitor position until iteration depth is reached OR current_resonancy frequency matches the target_frequency // Tries out different capacitor position until iteration depth is reached OR current_resonancy frequency matches the target_frequency
int32_t bruteforceResonance(uint32_t target_frequency, uint32_t current_resonance_frequency) int32_t bruteforceResonance(uint32_t target_frequency, uint32_t current_resonance_frequency)
{ {