Advertisement
learnelectronics

Signal generator v1.5

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