learnelectronics

Basic Signal Generator

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