Arnemi

esp_dac_dds_ino

Dec 14th, 2023
1,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * INFO:
  3.  * This code demonstrates how to use the ESP32 DAC in an interrupt based configuration.
  4.  * The sampling frequency and resolution of the samples and the DAC can be adjusted and experimented with
  5.  *
  6.  * Note: the ESP32 DAC has a fixed resolution of 8 bits, so the possibility for varying
  7.  * DAC resolution is simulated by representing the desired resolution within an 8 bit range.
  8.  */
  9.  
  10. /*
  11.  * Global constants for the array of samples to be written to the DAC.
  12.  * Default is a sine wave of 16 values between 0 and 255.
  13.  */
  14. const int sampleArrayLen = 16;
  15. const int sampleArray[sampleArrayLen] = {
  16.   0x80,
  17.   0xb0,
  18.   0xda,
  19.   0xf5,
  20.   0xff,
  21.   0xf5,
  22.   0xda,
  23.   0xb0,
  24.   0x80,
  25.   0x4f,
  26.   0x25,
  27.   0xa,
  28.   0x0,
  29.   0xa,
  30.   0x25,
  31.   0x4f,
  32. };
  33.  
  34. //Change these parameters in order to change the sampling properties of the system
  35. #define SAMPLING_PERIOD 100  //In microseconds, should be at least 50.
  36. #define DAC_RESOLUTION 8     //Resolution of the DAC. Must be between 1 and 8 bits.
  37.  
  38. //Hardware configuration
  39. #define DAC_PIN 26
  40.  
  41. // Variable for storing the timer properties. Leave this alone.
  42. hw_timer_t *timer = NULL;
  43.  
  44. // Constant with a mask for setting the correct DAC resolution. Leave this alone.
  45. const int resolution_mask = ~((1 << (8 - DAC_RESOLUTION)) - 1);
  46.  
  47. /*
  48.  * Interrupt service routine that runs every with a period SAMPLING_PERIOD.
  49.  * Used for sampling, signal processing and digital to analog conversion.
  50.  */
  51. void ARDUINO_ISR_ATTR onTimer() {
  52.   //Variable for storing the actual value to be written to the DAC.
  53.   static int sampleDAC;
  54.  
  55.   //Current index of the sampleArray. Defined as static as its only used in this function.
  56.   static int i = 0;
  57.  
  58.   //Using the DAC to convert and write the sampleDAC variable on DAC_PIN with the given resolution for the DAC.
  59.   dacWrite(DAC_PIN, (sampleArray[i] & resolution_mask));
  60.  
  61.   if (i >= sampleArrayLen - 1) {
  62.     //Reached end of sampleArray. Resetting i.
  63.     i = 0;
  64.   } else {
  65.     //Incrementing i.
  66.     i++;
  67.   }
  68. }
  69.  
  70. //--------The code below this point can be left alone--------
  71.  
  72. void setup() {
  73.   //Initializing timer with 1Mhz frequency. This means one tick every microsecond.
  74.   timer = timerBegin(1000000);
  75.   timerAttachInterrupt(timer, &onTimer);
  76.  
  77.   //Call the onTimer function with the given period (in microseconds), repeating te timer and with unlimited count.
  78.   timerAlarm(timer, SAMPLING_PERIOD, true, 0);
  79. }
  80.  
  81. void loop() {
  82.   //empty loop
  83. }
  84.  
Advertisement