Arduino DDS Sinewave Generator

Rabobsel:
Hi, I'm an interested hobby electronics just learning Arduino, but I'm not very well versed in C-programming.
I would be glad if you could answer a few questions about the project that can be found under the link in the first post "Arduino DDS Sinewave Generator by Martin Nawrath".

Can you please quote the link here, rather than expect people to search for it... I found a slightly
different copy with different line numbers in places...

The program runs flawlessly on my arduino uno, but despite reading some passages in the user manual of the Atmega8 and tormenting google, I do not understand some parts of the program code.

1.)
To save myself the manual entry of 255 sine values into an array, I tried to do this with the following code:

#include <math.h>
#include <avr/pgmspace.h>

int sinusArray[] = {};

for (int i = 0; i < 255; i++)
{
const int sinusArray PROGMEM = {sin((2PIi)/255)};

  • }*
    -----------------------------------------------------------------------------------------------
    As you expect, this does not work for several reasons:
    - You can not change const variables, but variables in flash memory must be const type.
    - You can not release or write to flash memory from a running program, but a for loop can only be used in a running program.
    [/quote]
    Indeed - you need to use an array initializer with precomputed values to set up PROGMEM
    > What are the advantages of saving the sine values in flash memory and not in the RAM or EEPROM ?
    Far more memory available in flash, Uno has 32k flash, 1k EEprom, 2k SRAM IIRC
    > Do you know a way to realize what I wanted to do, or the manual entry is the only possible way ?
    What manual entry? Just stick the data in a generated header file as is normally done
    for big tables.
    > 2.)
    > In the comments the author makes some calculations that I can not understand:
    >
    > In line 33, the author names a "measured" PWM-clock of 31376.6 Hz, but how did he measure it?
    With a frequency meter I guess, or something with one built in like a 'scope.
    > In line 114, the author gives a calculated value of 31472.55 Hz for the same measure, I assume he calculated it with the formula f (PWM) = f (clk) / (N * 510) from the Atmega8 user manual.
    > I have read the whole section in the user manual about the phase correct PWM mode, but there is no reference to the value 510 or where it came from. How does this value of 510 come about?
    In phase correct mode the timer counts up then down 0, 1, 2, 3, ...., 0xFE, 0xFF, 0xFE, ..., 2, 1, 0, 1, ...
    > In line 117 the author calls a runtime of 8 milliseconds. Where did he get this value from?
    > I did not find anything about runtime computation in the User Guide.
    Misprint, obviously meant to be 8µs
    > 3.)
    > In line 73, the author uses an infinite loop (while (1) {}) in the "voidLoop () {}" method. I'm surprised, because I thought so far that the "voidLoop () {}" method itself is already an endless loop.
    Can only speculate that's to avoid the time penalty of all the serial checking that happens in the loop that
    calls loop(), but yes it seems pointless.
    > 4.)
    > In lines 123 and 127 the author shifts the value of the variable "phacculow" and "phaccuhigh" by 24 digits to the right. I do not understand why he is doing this, because the variable gets a completely different value then.
    You think a 2^32 entry sine table is practical?
    > 5.)
    > From line 131 to 134, the author uses the following code:
    > -----------------------------------------------------------------------------------------------
    > if(icnt1++ == 125) { // increment variable c4ms all 4 milliseconds
    > c4ms++;
    > icnt1=0;
    > }
    > -----------------------------------------------------------------------------------------------
    > I think I understand why this code exists, but I do not understand how the author comes to the values used. (125 and 4 milliseconds).
    > Is the 4 milliseconds halfway through 8 milliseconds runtime from the prvious calculation and so rests half a period ?
    > Are the 125 almost half the cycle time for the array with the 254 sine values so it resets after an half-sine-wave?
    4ms is 125 cycles of the PWM waveform (approximately). This is so c4ms variable can be used for timing in loop(). The normal timer0 interrupt is disabled to prevent jitter in running the timer2 ISR.
    > I apologize for the long text and thank you, if you have read this far.
    >
    > Sincerely,
    > Rabobsel