Created
February 5, 2014 18:43
-
-
Save jackdev23/8830350 to your computer and use it in GitHub Desktop.
Arduino: test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Jack Dev on 10/10/13. | |
// | |
// | |
/* | |
AnalogReadSerial | |
Reads an analog input on pin 0, prints the result to the serial monitor. | |
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. | |
This example code is in the public domain. | |
*/ | |
#include <AH_AD9850.h> | |
//CLK - D6, FQUP - D7, BitData - D8, RESET - D9 | |
//AH_AD9850(int CLK, int FQUP, int BitData, int RESET); | |
AH_AD9850 AD9850(5, 6, 7, 4); | |
double freqStart; | |
//adc variables | |
float Adcphase; | |
float Adcmagnitude; | |
//swr etc. variables | |
float Returnloss; | |
float Angle ; | |
float Mag ; | |
float F ; | |
float G ; | |
float Rr ; | |
float Ss ; | |
float Ximp ; | |
float Rimp ; | |
float Zimpl ; //Offset Z0 | |
float Zimp ; | |
float Swr ; | |
float Rrsqrtmp ; | |
float Sssqrtmp ; | |
float Tmpsa ; | |
float Tmpsb ; | |
float Tmpsc ; | |
//from radians to degrees | |
const float R2d = 57.296; | |
//Standard characteristic impedance | |
const float Z0 = 51.1; | |
byte SWR[84]; | |
double lastFreq ; | |
float minSwr; | |
float maxSwr; | |
void Docalcswr(){ | |
float Tmpsa ; | |
float Tmpsb ; | |
float Tmpsc ; | |
float refer = 5.0*analogRead(A1)/1023; | |
Adcphase = 5.0*analogRead(A2)/1023; | |
Adcmagnitude = 5.0*analogRead(A0)/1023; | |
Returnloss = (60.0 * (Adcmagnitude - (refer/2)) /refer); | |
Returnloss = 19.0 - Returnloss - 2.7; | |
Angle = (180.0 * Adcphase /refer); | |
Tmpsa = Returnloss / 20.0; | |
Tmpsb = 10.0; | |
Mag = pow(Tmpsb , Tmpsa); | |
Mag = 1.0 / Mag; | |
Tmpsa = 1.0 + Mag; | |
Tmpsb = 1.0 - Mag; | |
Tmpsc = Tmpsa / Tmpsb; | |
Swr = abs(Tmpsc); | |
} | |
void Docalcall(){ | |
Docalcswr(); | |
float Tmpsa ; | |
float Tmpsb ; | |
float Tmpsc ; | |
float Rrsqrtmp ; | |
float Sssqrtmp ; | |
float F ; | |
float G ; | |
float Rr ; | |
float Ss ; | |
Tmpsa = Angle / R2d; | |
F = cos(Tmpsa); | |
G = sin(Tmpsa); | |
Rr = F * Mag; | |
Ss = G * Mag; | |
Tmpsa = 1.0 - Rr; | |
Rrsqrtmp = Tmpsa * Tmpsa; | |
Sssqrtmp = Ss * Ss; | |
Tmpsa = Rrsqrtmp + Sssqrtmp; | |
Tmpsb = 2.0 * Ss; | |
Tmpsc = Tmpsb / Tmpsa; | |
Tmpsa = Tmpsc * Z0; | |
Ximp = abs(Tmpsa); | |
Tmpsa = Rr * Rr; | |
Tmpsb = 1.0 - Tmpsa; | |
Tmpsa = Tmpsb - Sssqrtmp; | |
Tmpsb = Rrsqrtmp + Sssqrtmp; | |
Tmpsc = Tmpsa / Tmpsb; | |
Tmpsa = Tmpsc * Z0; | |
Rimp = abs(Tmpsa); | |
Tmpsa = Rimp * Rimp; | |
Tmpsb = Ximp * Ximp; | |
Tmpsc = Tmpsa + Tmpsb; | |
Zimpl = sqrt(Tmpsc); | |
} | |
void sweep(double f){ | |
double freq; | |
for (int k = 0;k < 84; k++){ | |
freq = f + 8*k*1000.0; | |
AD9850.set_frequency(freq); //set 2.5kHz frequency | |
delay(10); | |
Docalcall(); | |
Serial.print (" refer: "); | |
// Serial.print (refer,3); | |
Serial.print (" freq: "); | |
Serial.print (freq,0); | |
Serial.print (" Angle: "); | |
Serial.print(Angle); | |
Serial.print (" Returnloss: "); | |
Serial.print(Returnloss); | |
Serial.print (" Rimp: "); | |
Serial.print(Rimp); | |
Serial.print (" Ximp: "); | |
Serial.print(Ximp); | |
Serial.print (" Zimpl: "); | |
Serial.print(Zimpl); | |
Serial.print (" Swr: "); | |
Serial.println(Swr); | |
if (Swr < 1.1) Swr = 1.1; | |
if (Swr > 3.0) Swr = 3.0; | |
// SWR[k] = 46 -(8*Swr); //MAX SWR = 5.6 | |
SWR[k] = 54 -(18*Swr); //MAX SWR = 3.0 | |
if (minSwr > Swr) minSwr = Swr; | |
if (maxSwr < Swr) maxSwr = Swr; | |
/* Serial.print(freq); | |
Serial.print (" "); | |
Serial.println(Swr);*/ | |
} | |
lastFreq = freq; | |
} | |
// the setup routine runs once when you press reset: | |
void setup() { | |
Serial.begin(9600); | |
AD9850.reset(); //reset module | |
delay(1000); | |
AD9850.powerDown(); //set signal output to LOW | |
// initialize serial communication at 9600 bits per second: | |
freqStart = 7000000.0; | |
AD9850.set_frequency(0,0,freqStart); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
sweep(freqStart); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment