Advertisement
learnelectronics

Signal generator v1.5

Aug 17th, 2020
3,775
1
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 v1.5
  6.  * Learnelectronics
  7.  * Aug 17 2020
  8.  * www.youtube.com/learnelectronics
  9.  *
  10.  * Library code found at: https://github.com/Billwilliams1952/AD9833-Library-Arduino
  11.  *
  12.  */
  13. #include <Wire.h>                                                       //I2c library for the OLED display
  14. #include <Adafruit_SSD1306.h>                                           //OLED driver
  15.  
  16. #define OLED_RESET 4                                                    //necessary for the OLED
  17. #include <AD9833.h>                                                     // Include the library
  18.  
  19. #define FNC_PIN 5                                                       // Can be any unused digital IO pin
  20.  
  21.  
  22. AD9833 gen(FNC_PIN);                                                    // Defaults to 25MHz internal reference frequency
  23.  
  24. Adafruit_SSD1306 display(OLED_RESET);                                   // create object for the OLED
  25.  
  26. int rate = 100;                                                         //inital rate of sig gen
  27. int pot = 0;                                                            //initial value for potentiometer
  28. const int  buttonPin = 8;                                               //waveform switch on pin 8
  29. int buttonPushCounter = 0;                                              //count how many times the button has been pushed
  30. int buttonState = 0;                                                    //is the button high or low
  31. int lastButtonState = 0;                                                //remember the last state
  32.  
  33.  
  34.  
  35. void setup() {
  36.    
  37.     gen.Begin();                                                        // This MUST be the first command after declaring the AD9833 object              
  38.  
  39.                                                                         // Apply a 1000 Hz sine wave using REG0 (register set 0). There are two register sets,
  40.                                                                         // REG0 and REG1.
  41.                                                                         // Each one can be programmed for:
  42.                                                                         //   Signal type - SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, and HALF_SQUARE_WAVE
  43.                                                                         //   Frequency - 0 to 12.5 MHz
  44.                                                                         //   Phase - 0 to 360 degress (this is only useful if it is 'relative' to some other signal
  45.                                                                         //           such as the phase difference between REG0 and REG1).
  46.                                                                         // In ApplySignal, if Phase is not given, it defaults to 0.
  47.     gen.ApplySignal(SINE_WAVE,REG0,rate);                               //start up the sig gen
  48.    
  49.     gen.EnableOutput(true);                                             // Turn ON the output - it defaults to OFF
  50.    
  51.  
  52.     pinMode(A0,INPUT);                                                  //set up analog0 to read the pot
  53.     pinMode(8,INPUT_PULLUP);                                            //set up dig 8 for input with internal pullup
  54.    
  55.     Serial.begin(9600);                                                 //start serial comms for debugging
  56.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                          //start up OLED
  57.   display.display();                                                    //show display buffer
  58.  
  59.   display.clearDisplay();                                               //clear OLED
  60. }
  61.  
  62. void loop() {
  63.  
  64. buttonState = digitalRead(buttonPin);                                   //digital read of pin8
  65. pot = analogRead(A0);                                                   //analog read of A0
  66. rate = map(pot, 0, 1023, 1000, 10000);                                  //map the value of the analog read to frequencies
  67.  
  68. if (buttonState != lastButtonState) {
  69.                                                                         // if the state has changed, increment the counter
  70.     if (buttonState == HIGH) {
  71.                                                                         // if the current state is HIGH then the button went from off to on:
  72.       buttonPushCounter++;
  73.       Serial.println("on");
  74.       Serial.print("number of button pushes: ");
  75.       Serial.println(buttonPushCounter);
  76.      
  77.     } else {
  78.                                                                         // if the current state is LOW then the button went from on to off:
  79.       Serial.println("off");
  80.     }
  81.                                                                         // Delay a little bit to avoid bouncing
  82.     delay(50);
  83.   }
  84.                                                                         // save the current state as the last state, for next time through the loop
  85.   lastButtonState = buttonState;
  86. if (buttonPushCounter > 2){
  87.   buttonPushCounter = 0;                                                //reset the number of button pushes
  88. }
  89.  
  90.   display.setTextSize(2);                                               //send freq & wave type to OLED buffer
  91.   display.setTextColor(WHITE);
  92.   display.setCursor(0,0);
  93.   display.clearDisplay();
  94.   display.print("Freq:");
  95.   display.println(rate);
  96.   display.println("");
  97.   display.print("Type:");
  98.  
  99.   switch (buttonPushCounter) {                                          //determine which waveform to output
  100.     case 1:
  101.       display.print("Sine");
  102.       gen.ApplySignal(SINE_WAVE,REG0,rate);                             //output desired waveform
  103.       break;
  104.     case 2:
  105.       display.print("Tri");
  106.       gen.ApplySignal(TRIANGLE_WAVE,REG0,rate);                         //output desired waveform
  107.       break;
  108.     case 3:
  109.       display.print("Squar");
  110.       gen.ApplySignal(SQUARE_WAVE,REG0,rate);                           //output desired waveform
  111.     break;
  112.     default:
  113.       display.print("Squar");
  114.       gen.ApplySignal(SQUARE_WAVE,REG0,rate);                           //if it gets out of sorts just do this
  115.     break;
  116.   }
  117.   display.display();                                                    //show whats in the OLED buffer
  118.  
  119.   //gen.ApplySignal(SINE_WAVE,REG0,rate);
  120.  
  121.  
  122.  
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement