Advertisement
Electgpl

ARDUINO - AD9850 DDS Swep

Aug 24th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. // Define Pins used to control AD9850 DDS
  2. const int FQ_UD=10;
  3. const int SDAT=11;
  4. const int SCLK=9;
  5. const int RESET=12;
  6.  
  7.  
  8. double Fstart_MHz = 1;  // Start Frequency for sweep
  9. double Fstop_MHz = 10;  // Stop Frequency for sweep
  10. double current_freq_MHz; // Temp variable used during sweep
  11. long serial_input_number; // Used to build number from serial stream
  12. int num_steps = 100; // Number of steps to use in the sweep
  13. char incoming_char; // Character read from serial stream
  14.  
  15.  
  16. // the setup routine runs once when you press reset:
  17. void setup() {
  18.   // Configiure DDS control pins for digital output
  19.   pinMode(FQ_UD,OUTPUT);
  20.   pinMode(SCLK,OUTPUT);
  21.   pinMode(SDAT,OUTPUT);
  22.   pinMode(RESET,OUTPUT);
  23.  
  24.   // Configure LED pin for digital output
  25.   pinMode(13,OUTPUT);
  26.  
  27.  
  28.   // Set up analog inputs on A0 and A1, internal reference voltage
  29.   pinMode(A0,INPUT);
  30.   pinMode(A1,INPUT);
  31.   analogReference(INTERNAL);
  32.  
  33.   // initialize serial communication at 57600 baud
  34.   Serial.begin(57600);
  35.  
  36.  
  37.   // Reset the DDS
  38.   digitalWrite(RESET,HIGH);
  39.   digitalWrite(RESET,LOW);
  40.  
  41.   //Initialise the incoming serial number to zero
  42.   serial_input_number=0;
  43.  
  44. }
  45.  
  46. // the loop routine runs over and over again forever:
  47. void loop() {
  48.   //Check for character
  49.   if(Serial.available()>0){
  50.     incoming_char = Serial.read();
  51.     switch(incoming_char){
  52.     case '0':
  53.     case '1':
  54.     case '2':
  55.     case '3':
  56.     case '4':
  57.     case '5':
  58.     case '6':
  59.     case '7':
  60.     case '8':
  61.     case '9':
  62.       serial_input_number=serial_input_number*10+(incoming_char-'0');
  63.       break;
  64.     case 'A':
  65.       //Turn frequency into FStart
  66.       Fstart_MHz = ((double)serial_input_number)/1000000;
  67.       serial_input_number=0;
  68.       break;
  69.     case 'B':
  70.       //Turn frequency into FStart
  71.       Fstop_MHz = ((double)serial_input_number)/1000000;
  72.       serial_input_number=0;
  73.       break;
  74.     case 'C':
  75.       //Turn frequency into FStart and set DDS output to single frequency
  76.       Fstart_MHz = ((double)serial_input_number)/1000000;
  77.       SetDDSFreq(Fstart_MHz);
  78.       serial_input_number=0;    
  79.       break;
  80.     case 'N':
  81.       // Set number of steps in the sweep
  82.       num_steps = serial_input_number;
  83.       serial_input_number=0;
  84.       break;
  85.     case 'S':    
  86.     case 's':    
  87.       Perform_sweep();
  88.       break;
  89.     case '?':
  90.       // Report current configuration to PC    
  91.       Serial.print("Start Freq:");
  92.       Serial.println(Fstart_MHz*1000000);
  93.       Serial.print("Stop Freq:");
  94.       Serial.println(Fstart_MHz*1000000);
  95.       Serial.print("Num Steps:");
  96.       Serial.println(num_steps);
  97.       break;
  98.     }
  99.     Serial.flush();    
  100.   }
  101. }
  102.  
  103. void Perform_sweep(){
  104.   double FWD=0;
  105.   double REV=0;
  106.   double VSWR;
  107.   double Fstep_MHz = (Fstop_MHz-Fstart_MHz)/num_steps;
  108.  
  109.   // Start loop
  110.   for(int i=0;i<=num_steps;i++){
  111.     // Calculate current frequency
  112.     current_freq_MHz = Fstart_MHz + i*Fstep_MHz;
  113.     // Set DDS to current frequency
  114.     SetDDSFreq(current_freq_MHz*1000000);
  115.     // Wait a little for settling
  116.     delay(10);
  117.     // Read the forawrd and reverse voltages
  118.     REV = analogRead(A0);
  119.     FWD = analogRead(A1);
  120.     if(REV>=FWD){
  121.       // To avoid a divide by zero or negative VSWR then set to max 999
  122.       VSWR = 999;
  123.     }else{
  124.       // Calculate VSWR
  125.       VSWR = (FWD+REV)/(FWD-REV);
  126.     }
  127.    
  128.     // Send current line back to PC over serial bus
  129.     Serial.print(current_freq_MHz*1000000);
  130.     Serial.print(",0,");
  131.     Serial.print(int(VSWR*1000));
  132.     Serial.print(",");
  133.     Serial.print(FWD);
  134.     Serial.print(",");
  135.     Serial.println(REV);
  136.   }
  137.   // Send "End" to PC to indicate end of sweep
  138.   Serial.println("End");
  139.   Serial.flush();    
  140. }
  141.  
  142. void SetDDSFreq(double Freq_Hz){
  143.   // Calculate the DDS word - from AD9850 Datasheet
  144.   int32_t f = Freq_Hz * 4294967295/125000000;
  145.   // Send one byte at a time
  146.   for (int b=0;b<4;b++,f>>=8){
  147.     send_byte(f & 0xFF);
  148.   }
  149.   // 5th byte needs to be zeros
  150.   send_byte(0);
  151.   // Strobe the Update pin to tell DDS to use values
  152.   digitalWrite(FQ_UD,HIGH);
  153.   digitalWrite(FQ_UD,LOW);
  154. }
  155.  
  156. void send_byte(byte data_to_send){
  157.   // Bit bang the byte over the SPI bus
  158.   for (int i=0; i<8; i++,data_to_send>>=1){
  159.     // Set Data bit on output pin
  160.     digitalWrite(SDAT,data_to_send & 0x01);
  161.     // Strobe the clock pin
  162.     digitalWrite(SCLK,HIGH);
  163.     digitalWrite(SCLK,LOW);
  164.   }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement