generate a sine wave

Status
Not open for further replies.

Fatima_H

Active member
hello everyone
I just would like to generate a sine wave with a (100 Hz - 10MHz) range of frequency
I try to use the following code and I try to change the waiting time but it cannot be less than 1 so the highest value of freq. does not reach even 1KHz
how can I generate a sine wave with a high range of freq?

I don't know a a lot about teensy programming, so I cannot change the code deeply to be fit with my requirements.

the code is:
///////////////////////////////////////////////////////////////////////////////////////

// Simple DAC sine wave test on Teensy 3.1

float phase = 0.0;
float twopi = 3.14159 * 2;
elapsedMicros usec = 0;

void setup() {
analogWriteResolution(12);
}

void loop() {
float val = sin(phase) * 2000.0 + 2050.0;
analogWrite(A14, (int)val);
phase = phase + 0.02;
if (phase >= twopi) phase = 0;
while (usec < 500) ; // wait
usec = usec - 500;
}

///////////////////////////////////////////////////////
or you can find it here : https://www.pjrc.com/teensy/teensy31.html

thanks in advance
 
This approach is slow, mostly due to use of floating point math and the sin() function which uses 64 bit double precision.

For higher speed, you need to use a table-lookup approach. DMA is also needed to efficiently write to the DAC. Both of these can be found in the audio library.

But no matter how efficient the code, the DAC has limited analog bandwidth. Even if you could write at incredible speed, there's no way the hardware can generate a 10 MHz waveform. The DAC circuitry simply does not have bandwidth beyond a couple hundred kHz, even when driving the very light load of only 10X oscilloscope probe. The bandwidth is less when driving more capacitive load.

Looks like you previously started a similar thread. Generally you can get better help here by keeping the conversation about one thing in the same thread.
 
sir,
please, can you give me an example code about how to use LUT with DMA just to learn how this is can be done?
I tried to find them in the audio library but unfortunately could not find them! I find a ready function for amplitude, freq, and phase but not a code with LUT
 
sir,
please, can you give me an example code about how to use LUT with DMA just to learn how this is can be done?
I tried to find them in the audio library but unfortunately could not find them! I find a ready function for amplitude, freq, and phase but not a code with LUT

Here is one example for dma and dac:
https://forum.pjrc.com/threads/28101-Using-the-DAC-with-DMA-on-Teensy-3-1

or you can also look here:
https://hackaday.io/project/12543-s.../41575-dac-with-dma-and-buffer-on-a-teensy-32
 
You should not expect others to write code for you...

Defining an array as LUT and filling it with scaled values from, for example, a sine function over a {0...2pi} interval, setting up the PDB as DMA trigger, the DAC as DMA destination, and the LUT as DMA source (as in the DAC output object of the audio library) are quite basic tasks.
 
Have you simply used the audio library yet? If not, perhaps just using it to generate sine waves would be a good first step before you try to understand its internal code (which is probably well beyond beginner level, especially DMA).

can you give me an example code about how to use LUT with DMA just to learn how this is can be done?
I tried to find them in the audio library but unfortunately could not find them!

Here is the sine wave code:

https://github.com/PaulStoffregen/Audio/blob/master/synth_sine.cpp
https://github.com/PaulStoffregen/Audio/blob/master/data_waveforms.c

And here is the DAC code using DMA:

https://github.com/PaulStoffregen/Audio/blob/master/output_dac.cpp

This page describes the internal design of the audio library objects:

https://www.pjrc.com/teensy/td_libs_AudioNewObjects.html
 
Status
Not open for further replies.
Back
Top