63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
|
|
#include "Waveforms.h"
|
|
|
|
#define PWM_BIT_WIDTH 10
|
|
#define PWM_PIN 5 // PWM-Pin für DAC0 auf dem Arduino Nano 328
|
|
|
|
int wf_wave0 = 0;
|
|
int wf_pos = 0;
|
|
int wf_sample;
|
|
|
|
bool wf_outputEnabled = true; // Variable to control waveform output state
|
|
|
|
void initWaveformGenerator()
|
|
{
|
|
setWaveform(WAVEFORM_SINUS);
|
|
// Call the function to set default frequency, here you might want to specify a default frequency
|
|
setWaveformFrequency(WAVEFORM_DEFAULT_FREQ_HZ);
|
|
}
|
|
|
|
void setWaveform(int waveform0)
|
|
{
|
|
if (waveform0 >= 0 and waveform0 < WAVEFORM_MAXWAVEFORM_NUM)
|
|
{
|
|
// Set default waveforms
|
|
wf_wave0 = waveform0;
|
|
}
|
|
}
|
|
|
|
void setWaveformFrequency(int frequency)
|
|
{
|
|
if (frequency >= 1 and frequency < 8000)
|
|
{
|
|
wf_sample = map(frequency, 0, (2^PWM_BIT_WIDTH)-1, 0, WAVEFORM_ONE_HZ_SAMPLE);
|
|
wf_sample = constrain(wf_sample, 0, WAVEFORM_ONE_HZ_SAMPLE);
|
|
} else {
|
|
wf_sample = map(WAVEFORM_DEFAULT_FREQ_HZ, 0, (2^PWM_BIT_WIDTH)-1, 0, WAVEFORM_ONE_HZ_SAMPLE);
|
|
wf_sample = constrain(wf_sample, 0, WAVEFORM_ONE_HZ_SAMPLE);
|
|
}
|
|
}
|
|
|
|
void enableWaveformOutput()
|
|
{
|
|
wf_outputEnabled = true;
|
|
}
|
|
|
|
void disableWaveformOutput()
|
|
{
|
|
wf_outputEnabled = false;
|
|
}
|
|
|
|
void pollWaveformGenerator()
|
|
{
|
|
if (wf_outputEnabled)
|
|
{
|
|
analogWrite(PWM_PIN, waveformsTable[wf_wave0][wf_pos]); // write the selected waveform on DAC0
|
|
|
|
wf_pos++;
|
|
if (wf_pos == WAVEFORM_MAX_SAMPLES_NUM) // Reset the counter to repeat the wave
|
|
wf_pos = 0;
|
|
|
|
delayMicroseconds(wf_sample); // Hold the sample value for the sample time
|
|
}
|
|
}
|