Slow sinewave code

Status
Not open for further replies.

cartere

Well-known member
Any thought on why this is so slow? Can only get 514 Hz out of it with the .1 phase inc.

#define ARM_MATH_CM4
#include <arm_math.h>

float phase = 0.0;
float twopi = 3.14159 * 2;
float val = 0.0;

//elapsedMicros usec = 0;

void setup() {
analogWriteResolution(12);
}

void loop() {
val= arm_sin_f32(phase)*2000.0+2050.0;
analogWrite(A14, (int) val);
phase = phase + 0.1;
if (phase >= twopi) phase = 0.0;
 
19.7 Khz with some jitter


#define ARM_MATH_CM4
#include <arm_math.h>

float phase = 0.0;
float twopi = 3.14159 * 2;
float val = 0.0;
float inc = 0.0;
int valtable[65];
int i=0;

//elapsedMicros usec = 0;

void setup() {
analogWriteResolution(12);
for(inc=0.0; inc<=twopi; inc=inc+.1){
valtable=(int)(arm_sin_f32(inc)*2000.0+2050.0);
i++;
}
}
void loop() {
for(i=0; i<62; i++) {
analogWrite(A14, valtable);
}


}
 
I don't know how fast the DAC is but 62 samples per cycle and 19.7kHz means that you are writing just over 1 million samples per second. That might be the limit of the DAC?

The jitter is because you write one cycle and then exit from the loop function. Try this:
Code:
void loop() {
  while(1) {
    analogWrite(A14, valtable[i++]);
    if(i >= 62)i = 0;
  }
}

Pete
 
The DAC's typical full-scale settling time is 15 us, according to the datasheet. That's approx 33 kHz full-scale bandwidth. The worst case (over temperature, supply voltage, etc) is about half that speed. It very likely might be able to reproduce higher frequency signals at a reduced signal amplitude. There's no specs in the datasheet regarding small signal bandwidth vs slew rate limiting.

Of course, you can write to the DAC at extreme speed. Just know the actual analog hardware is speed limited to audio band or slightly ultrasonic range. At least the good news is it will (probably) act like a nice low-pass filter, rather than horrible aliasing as if you'd decimated the data rate is software without properly low pass filtering.
 
Status
Not open for further replies.
Back
Top