Skip to content

Examples: Add waveform generator example. #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions examples/Waveform_Generator/Waveform_Generator.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// This example generates different waveforms based on user input on A12/DAC1.

#include "AdvancedDAC.h"

#define N_SAMPLES (256)
#define DEFAULT_FREQUENCY (16000)

AdvancedDAC dac1(A12);
uint8_t SAMPLES_BUFFER[N_SAMPLES];
size_t dac_frequency = DEFAULT_FREQUENCY;

void generate_waveform(int cmd)
{
switch (cmd) {
case 't':
// Triangle wave
Serial.print("Waveform: Triangle ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = abs((i % 255) - 127);
}
break;

case 'q':
// Square wave
Serial.print("Waveform: Square ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = (i % 255) < 127 ? 127 : 0;
}
break;

case 's':
// Sine wave
Serial.print("Waveform: Sine ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = sin(2 * 3.14 * (i / (float) N_SAMPLES)) * 127 + 127;
}
break;

case 'r':
// Sawtooth
Serial.print("Waveform: Sawtooth");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = i;
}
break;

case '+':
case '-':
Serial.print("Current frequency: ");

if (cmd == '+' && dac_frequency < 64000) {
dac_frequency *= 2;
} else if (cmd == '-' && dac_frequency > 1000) {
dac_frequency /= 2;
} else {
break;
}

dac1.stop();
delay(500);
if (!dac1.begin(AN_RESOLUTION_8, dac_frequency * N_SAMPLES, N_SAMPLES, 32)) {
Serial.println("Failed to start DAC1 !");
}
delay(500);
break;

default:
Serial.print("Unknown command ");
Serial.println((char) cmd);
return;
}

Serial.print(dac_frequency/1000);
Serial.println("KHz");
}

void setup() {
Serial.begin(115200);

while (!Serial) {

}


Serial.println("Enter a command:");
Serial.println("t: Triangle wave");
Serial.println("q: Square wave");
Serial.println("s: Sine wave");
Serial.println("r: Sawtooth wave");
Serial.println("+: Increase frequency");
Serial.println("-: Decrease frequency");

generate_waveform('s');

// DAC initialization
if (!dac1.begin(AN_RESOLUTION_8, DEFAULT_FREQUENCY * N_SAMPLES, N_SAMPLES, 32)) {
Serial.println("Failed to start DAC1 !");
while (1);
}
}

void loop() {
if (Serial.available() > 0) {
int cmd = Serial.read();
if (cmd != '\n') {
generate_waveform(cmd);
}
}

if (dac1.available()) {
// Get a free buffer for writing.
SampleBuffer buf = dac1.dequeue();

// Write data to buffer.
for (size_t i=0; i<buf.size(); i++) {
buf[i] = SAMPLES_BUFFER[i];
}

dac1.write(buf);
}
}