learnelectronics

Basic Signal Generator

Aug 16th, 2020
3,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ApplySignal.ino
  3.  * 2018 WLWilliams
  4.  *
  5.  * Basic Signal Generator
  6.  * Learnelectronics
  7.  * Aug 15 2020
  8.  * www.youtube.com/learnelectronics
  9.  *
  10.  * Library code found at: https://github.com/Billwilliams1952/AD9833-Library-Arduino
  11.  *
  12.  */
  13.  
  14. #include <AD9833.h>     // Include the library
  15.  
  16. #define FNC_PIN 4       // Can be any digital IO pin
  17.  
  18. //--------------- Create an AD9833 object ----------------
  19. // Note, SCK and MOSI must be connected to CLK and DAT pins on the AD9833 for SPI
  20. AD9833 gen(FNC_PIN);       // Defaults to 25MHz internal reference frequency
  21.  
  22.  
  23. int rate = 100;
  24. int pot = 0;
  25.  
  26.  
  27.  
  28. void setup() {
  29.     // This MUST be the first command after declaring the AD9833 object
  30.     gen.Begin();              
  31.  
  32.     // Apply a 1000 Hz sine wave using REG0 (register set 0). There are two register sets,
  33.     // REG0 and REG1.
  34.     // Each one can be programmed for:
  35.     //   Signal type - SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, and HALF_SQUARE_WAVE
  36.     //   Frequency - 0 to 12.5 MHz
  37.     //   Phase - 0 to 360 degress (this is only useful if it is 'relative' to some other signal
  38.     //           such as the phase difference between REG0 and REG1).
  39.     // In ApplySignal, if Phase is not given, it defaults to 0.
  40.     gen.ApplySignal(SINE_WAVE,REG0,rate);
  41.    
  42.     gen.EnableOutput(true);   // Turn ON the output - it defaults to OFF
  43.     // There should be a 1000 Hz sine wave on the output of the AD9833
  44.  
  45.     pinMode(A0,INPUT);
  46.    
  47.     Serial.begin(9600);
  48. }
  49.  
  50. void loop() {
  51.  
  52. pot = analogRead(A0);
  53. rate = map(pot, 0, 1023, 1000, 10000);
  54.  
  55.   gen.ApplySignal(SINE_WAVE,REG0,rate);
  56.  
  57. Serial.println(rate);
  58.  
  59.    
  60. }
Add Comment
Please, Sign In to add comment