Skip to content

Instantly share code, notes, and snippets.

@cversek
Last active January 12, 2024 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cversek/280d5b501e8ed5f0fe443e2ac9cc3f97 to your computer and use it in GitHub Desktop.
Save cversek/280d5b501e8ed5f0fe443e2ac9cc3f97 to your computer and use it in GitHub Desktop.
Generic CPU driven Direct Digital Synthesis Sine Wave code for Arduino, tested with STM32F405 Feather board
#define NS 64
// Sine Wave Look-Up-Table, generated in Python:
/*
from numpy import *
bits = 12
samps = 64
A = 2**(bits-1)
X = linspace(0.0,1.0,samps)
Y = A*sin(2*pi*X) + A
print(",".join(map(str,Y.astype('uint32'))))
*/
uint32_t Sine_LUT[NS] = {
2048,2251,2453,2651,2843,3027,3201,3364,
3514,3649,3768,3870,3954,4019,4064,4090,
4095,4080,4044,3989,3914,3821,3710,3583,
3440,3284,3115,2936,2748,2553,2353,2150,
1945,1742,1542,1347,1159, 980, 811, 655,
512, 385, 274, 181, 106, 51, 15, 0,
5, 31, 76, 141, 225, 327, 446, 581,
731, 894,1068,1252,1444,1642,1844,2047
};
const int LOOP_DELAY_MICROS = 1;
void setup() {
// Initialize the LED pin as an output
pinMode(A0, OUTPUT);
analogWriteResolution(12);
}
void loop() {
// Main loop code
for (int i = 0; i < NS - 1; i++){
analogWrite(A0,Sine_LUT[i]);
//No delay 64 samples on 168 Mhz STM32F405 (Optimized Fastest with LTO) gives 25 us period = 40kHz
//delayMicroseconds(LOOP_DELAY_MICROS);
}
}
@cversek
Copy link
Author

cversek commented Jan 12, 2024

Test: No delay 64 samples on 168 Mhz STM32F405 (Optimized Fastest with LTO)
Measured with Joulescope, shows period 25 us -> f = 40kHz
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment