const float freq = 60.0f;
const int N = 256; // 256 samples/cycle
const uint32_t Ts_us = (uint32_t)llroundf(1e6f / (freq * N));
uint16_t lut[N]; // store the sine wave here
void setup() {
analogWriteResolution(12);
for (int i = 0; i < N; ++i){
lut[i] = 2048 + (1000.0 * sin(2 * PI * i / N));
}
}
void loop() {
static uint32_t t_next = micros();
for (int i = 0; i < N; ++i) {
analogWrite(DAC0, lut[i]); // output the sine wave values
t_next += Ts_us;
while ((int32_t)(micros() - t_next) < 0) { /* spin */ }
}
}