Function / Arbitrary waveform generator from Chip's DDS code
Rayman
Posts: 14,744
in Propeller 2
Need a function generator for a certain test where need to sweep sine wave from 1 MHz to 20 MHz in 2ms with a sync. Seems we have ancient function generators here and also a very fancy one that is way overkill for this.
Ordered one from Mouser.
But, in the meantime, adapted this code from Chip's DDS example to do the job.
It seems wouldn't be too hard to turn this in to a more useful thing that could do the function generator's job. May try to integrate into Mixed Signal Scope code as time allows...
One kind of limitation with this code is that the LUT is 8 bit.
The one just bought for ~$1k is 14 bit. Better ones are 16-bit.
Is there an easy way to get closer to 16 bit?
Comments
Code is currently rigged to output on P0..P3 with sync on P5.
Some back of the envelope calculations: If the DAC is running at 100 million samples per second, then the 2mS sweep time would be 200,000 samples. That many 16 bit samples could fit in hub RAM. So if a reduced sample rate is ok, it would be possible to pre-calculate the sweep and just play it back.
Maybe it would be possible to do a weighted average of 2 DACs, one at full output and another attenuated to 1/16. That would theoretically give 12 bit output. In practice the linearity of these DACs is unlikely to be that good. You will have at least 4096 potentially distinct voltage levels. The linearity adjustment could be baked into the pregenerated data.
One of the annoying things with the P2 is I think the DDS output can only go to the onboard DACs. In addition, since the LUT is 512 samples long you can't really generate a sine wave with more than 8 bits anyway. But, you can have more than 8 bit resolution for the RMS power of a sine wave. Here is some code I wrote a while back that generates a sine wave with 11 bit amplitude adjustment. https://forums.parallax.com/discussion/comment/1548401/#Comment_1548401
The smart pin dithered DAC modes might be usable at fsys/4 rate like this. That would be 10+ bit resolution when low pass filtered. This would go with the pre-calculated data.
Thanks @SaucySoliton
Maybe 8-bit is enough... I was looking around at happened on this page with what looks like an FPGA that can do similar things as P2:
https://www.infineon.com/cms/en/design-support/tools/sdk/psoc-software/psoc-3-5-components/
Also, it looked good enough on the scope for what I'm doing, finding the resonant frequency...
Chip has said the DAC's are 'quite good'.
If you use multiple COGS here and sync them, you could maybe do
I'm not sure what the drift/tracking of the DACs is like, but they could be calibrated from a high precision external ADC.
eg If we assume 0.25 LSB errors, the LSB DAC can use ~2 bits to correct for that, and a further ~6 bits would give ~14b of calibrated DAC.
The better your filter is the less the resolution of the sine table matters. With a perfectly steep low pass filter and a single frequency you could theoretically output a square wave and get a perfectly smooth sine wave. Of course this is not possible with a sweep because you need a broader bandwidth. The first stepper drives I produced had tunable filters. They had only 1/10 microstep resolution but with the filters tuned just a bit above the step frequency they turned the motors perfectly smooth with no audible noise.
So I think a 3th order low pass filter with a corner frequency around 30..40MHz a resolution of 8 bit should be enough, I think.