Shop OBEX P1 Docs P2 Docs Learn Events
smartpin pwm — Parallax Forums

smartpin pwm

Seen some very exciting high speed MHZ waveforms generated on a smartpin but I only saw these going as slow as 1hz.

Could it be slower, like to generate a signwave for example running as slow as a one cycle over a minute.

Comments

  • jmgjmg Posts: 15,148

    The DOCs do not seem to have a formula or limits on the opcode
    SETXFRQ {#}D Set streamer NCO frequency to D.
    At low frequencies you could run an interrupt and do LUT -> DAC on a INT+SW DDS down to any low milliHz value.

  • pik33pik33 Posts: 2,350

    There is a 16-bit prescaler and 16-bit range in PWM mode which gives 0.058 Hz at 250 MHz CPU clock,

  • evanhevanh Posts: 15,192

    A real-time computed sine wave could be programmed into a DAC dithering smartpin. Slowest sample rate using a 4 MHz sysclock is 61.3 Hz but there's no upper limit to the number of computed samples per sine cycle.

  • @evanh, you might be tired.

    How do you exactly sample a DAC?

    Mike

  • JonnyMacJonnyMac Posts: 8,929
    edited 2021-12-09 18:06

    A friend wanted to generate some lowish (< 1kHz) sine waves for a simple audio project -- I came up with this (intended to run in its own cog). As Evan points out, changing the timing between the steps affects the frequency, so this could be modified to go slower.

    pri sine_out(pin, freq, level) | t, x, y
    
    '' Put sine wave on pin
    '' -- freqeuncy in Hz (1 to ~1000)
    '' -- level is output, 0 to 100%
    
      freq  := clkfreq / (freq << 8)                                ' convert to ticks
    
      level := (0 #> level <# 100) * 127 / 100
    
      pinstart(pin, P_DAC_DITHER_PWM | P_DAC_75R_2V | P_OE, 256, $8000)
    
      x := 0
    
      t := getct()
      repeat
        y := (qsin(level, x++, 256) + 128) << 8
        wypin(pin, y)
        waitct(t += freq)
    
  • evanhevanh Posts: 15,192

    @msrobots said:
    @evanh, you might be tired.
    How do you exactly sample a DAC?

    They're still called samples whether coming from an ADC or going to a DAC.

  • Hmmm,

    I see that @JonnyMac is using P_DAC_DITHER_PWM

    Is this not what I was asking about in my FlexBasic-PWM thread re: generating 16bits at higher frequencies?

  • pik33pik33 Posts: 2,350
    edited 2021-12-09 21:50

    There are 2 modes of DAC 8-to-16 bit extension in P2: PWM and random dither.

    PWM needs sample rate=clock/(256*n) but it is very precise, gives over 1 MHz sample rate at 16 bit resolution and is simply noise free. Connect it to an audio amplifier: this is the best DAC I ever connected to the audio amplifier. You can get over 24bit resolution in accoustic band using a noise shaper, but who cares, at 1.25 MHZ sample rate you have 110 dB SNR in accoustic band without any tricks. This thing doesn't have any noise you can hear unless you set your 200 W amplifier to the full volume. I tried 3-bit sine wave on such an amplifier, the sound was stil pure. With a noise shaper in theory these DACs can give SNR over 160 dB (!!!). Compared to analog circuit noise, power supply noise, etc, this is simply zero.

    This mode switches between 2 adjacent 8-bit levels using PWM, that is why this 256 divider is needed

    Random dither is... random. The average value is 16 bit, but the noise is equally spread in the band instead of being 1.25 MHz and its harmonic as in PWM mode . This means you can hear a part of this noise. A very rough calculation - this is 1 bit noise at 8-bit resolution, but it is spread equally into 150 MHz band, so the hearable noise is something about -83 dB The "full power amplifier test" gives something like this. Also, the "3 bit sine wave test" was not as good as in PWM mode: there were distortions hearable.
    This mode doesn't require any particular sample rate. I have a Paula emulator started: it needs 3.5 MHz sample rate, so I will use this mode there. Good enough for Amiga modules and other retrocomputer audio things.

  • @pik33

    The OP was a question of how slow it can be, not how fast.

    Still very Interesting info, thank you.

  • pik33pik33 Posts: 2,350

    It can be as slow as you want :) Set an interrupt from the timer, up to several seconds is available, then make a software counter as long as you wish,

Sign In or Register to comment.