|
My reason for doing something like this is to adjust timing accuracy for a slower frequency output.
I am generating a sinewave output at between 0.1 and 50.1 Hz, with 0.1Hz accuracy. To update the sinewave sample I am using a 16-bit counter, running at about 75kHz. The counter's period is filled with a value calculated to give 180 samples per wave. So a 10Hz wave would have a count of 41.667. Using a count of 42 produces a sine wave at 9.9Hz. That error is unacceptable.
So to solve it I have 2 values: a count of 41 and an 8-bit error correction value. In assembler (which is what I'm more familiar with) what I would do is recalculate that error of .667 into 171 by multiplying by 256 (all of these values are precalculated anyway). Then every sample I add the error value to a rolling 8-bit counter. When the counter rolls over the counter period is updated for a period of step+1. The samples have a small amount of jitter (+/-13us) but the overall waveform has a very accurate frequency.
I know that this can be done by using a 16-bit rolling counter, checking for values above 255 and then subtracting 256, but that adds extra overhead I don't want.
static uint16 errTotal = 0;
errTotal += errValue;
if (errTotal >= 256)
{
// Not Efficient Rollover errTotal -= 256;
++tickCount;
}
I know with some other 8-bit processors (such as Freescale) their compiler has built-in macros that allow for quick checks of some of the accumulator status bits. Specifically the carry bit. I've written my previous example code in C for the freescale micro almost exactly as above. What I want to know is if I can do something similar for the PSoC, or if I have to use the less efficient workaround.
|