Shop OBEX P1 Docs P2 Docs Learn Events
P1 software PASM DAC — Parallax Forums

P1 software PASM DAC

Hello,
I'm looking for a DAC PASM software solution for the P1.
I only found ADC (PASM) and PWM (SPIN).

Comments

  • Hi,
    Pwm plus a RC Filter does the trick.

  • evanhevanh Posts: 16,032
    edited 2024-06-24 07:21

    Filtered PDM (duty-cycle counter mode) is far better than filtered PWM. PWM filters noisily in the mid-level range, worst when set at 50%, therefore it requires an oversized capacitor to compensate. This of course makes the filter response much slower. Whereas PDM has consistence noise at all set levels, allowing for an optimised filter.

  • evanhevanh Posts: 16,032
    edited 2024-06-24 10:44

    Here's an example using counterB. It's configured for 0 to 10000. If you prefer 16-bit resolution then just delete the scaling line from dacset().

    PUB  tester | val
    
        dacinit( 16 )    ' pin #16 has R-C filter attached
    
        repeat
            repeat val from 0 to 10000
                dacset( val )
            waitms( 500 )
    
            repeat val from 10000 to 0
                dacset( val )
            waitms( 500 )
    
    
    PUB  dacinit( pinnum )
    
        ctrb := %00110 << 26 | pinnum    ' DUTY single-ended
        dira := 1 << pinnum    ' enable pin drive
    
    
    PUB  dacset( percent )    ' PDM percentage in hundreths of percent
    { duty = 4G * percent / 10000
    }
        percent := (||percent << 16 + 5000) / 10000 <# $ffff    ' scaled and bounded
        frqb := percent << 16 | percent    ' 16bit -> 32bit
    
    
    PUB  waitms( duration ) | timer, msec
    
        timer := cnt
        msec := clkfreq >> 10    ' one millsecond
        repeat duration
            timer += msec
            waitcnt( timer )
    
  • evanhevanh Posts: 16,032
    edited 2024-06-24 13:00

    Oops, small bug - DIRA should be OR'd with rather than overwritten.

    PUB  dacinit( pinnum )
    
        ctrb := %00110 << 26 | pinnum    ' DUTY single-ended
        dira |= 1 << pinnum    ' enable pin drive
    

    or clearer still

    PUB  dacinit( pinnum )
    
        ctrb := %00110 << 26 | pinnum    ' DUTY single-ended
        dira[pinnum] := true    ' enable pin drive
    
Sign In or Register to comment.