| Posted: 07:07am 31 Jul 2026 |
|
|
|
I wondered why index 99 and 100 were both 65532 in the original code, and why index 100 never gets to 65535. Turns out only one change is actually needed, and the change is to the formula, with the rest of the code unchanged.
NOTE: This is for the Inverter code that poida previously posted near the start of the thread. With NPWM = 200, changing the sine array algorithm from, (float)(NPWM-1) to (float)NPWM, now generates the correct array dimension with index 0 = 0, 1 = 1029 and 199 = 1029, instead of zero, and removes the compressed double array value at index 99 and 100, the SPWM frequency remains 20kHz.
Nothing else is changed, other code stays as it was originally.
Like the following:
#define NPWM 200 . . for (t = 0, i = 0; i < NPWM; i++, t += 3.14159 / (float)NPWM { u = 65535.0 * sin(t); l[i] = (uint16_t)u; } . . pcount ++; if (pcount >= NPWM) { .... ... .... }
Both methods shown will result in the 1/2 sine array being built correctly for this HW & SW implementation used to generate the intended 50hz/60hz SPWM output waveform.
BTW: The brilliant Inverter code written by poida has run faultlessly for years, this just removes a tiny wrinkle that some have noticed with test gear, I admit that I missed it, as the code output was so good compared to anything else before it.
It had been bought up way back by wiseguy and resurrected again here by InPhase, thanks to both for investigating, testing and pushing us  : Edited 2026-08-01 10:18 by KeepIS |