Smart-Pin DAC with Noise
JonTitus
Posts: 193
in Propeller 2
For several hours I have tried and failed (see code below) to get a Smart-Pin to produce what I think it should in the DAC_MODE = DAC Noise setting. Documentation doesn't include an example, and most of the Forum comments haven't helped. I assume I should see random voltages on my scope, but all I see is a short logic-0 pulse on the P20 output when the program starts. I'm revising the SMART PIN section of the v33 documentation, but it's difficult without an assembly-language program I can use as an example, test on my P2 board, and explain. I welcome a working example with comments. It will help others, too. Thanks. --Jon
CON
freq = 160_000_000
dat
org 0
dirl #20 'Set DAC at Smart-Pin P20
wrpin DACconfig, #20
nop
wxpin DACperiod, #20
nop
wypin DACvolt, #20
dirh #20
.myloop nop
jmp #.myloop
DACconfig long %0000_0000_000_10100_00000000_01_00010_0 'Random dither
'DACconfig long %0000_0000_000_10100_00000000_01_00011_0 'PWM dither
DACperiod long $0000_0000_0000_0000 ' X15:X0, no period?
DACvolt long $0000_0000_0000_FF00 ' Bits Y15:Y0, voltage? How set?

Comments
Here is an (untested) example for 16bit random output:
dat org dirl #20 'Set DAC at Smart-Pin P20 wrpin DACconfig, #20 wxpin DACperiod, #20 dirh #20 .myloop wypin DACvolt, #20 'write 16bit DAC value getrnd DACvolt 'new random value and DACvolt, ##$FFFF 'mask lower 16 bits .waitper testp #20 wc 'wait for IN raise (period end) if_nc jmp #.waitper 'rate=clkfreq/256 ~ 78kHz jmp #.myloop DACconfig long %0000_0000_000_10100_00000000_01_00010_0 'Random dither 'DACconfig long %0000_0000_000_10100_00000000_01_00011_0 'PWM dither DACperiod long 256 DACvolt long $8000 'initial value (middle)AndyHere is a little Spin2 code, that outputs a sine wave with a 16bit DAC. It works with Fastspin and with PNUT.
_clkfreq = 180_000_000 smprate = 44100 sinfreq = 1000 dacpin = 39 pub sine_out() | smp, phs pinstart(dacpin, P_DAC_DITHER_RND | P_DAC_600R_2V | P_OE, clkfreq/smprate, 0) 'smartDAC repeat _,smp := polxy($7FFF, phs) 'cordic sine function wypin(dacpin, smp+$8000) 'write sine sample to dac phs += sinfreq FRAC smprate 'step angle for sine freq repeat until pinread(dacpin) 'wait for dac sample periodAndyIf I'm seeing it right, DDS mode is 8-bit DAC output, whereas the smartpin mode above is 8-bit "real" plus 8-bit dither.
I'd guess that at audio frequencies the smartpin mode would be a lot better...
I agree.