P1 software PASM DAC
pic18f2550
Posts: 400
Hello,
I'm looking for a DAC PASM software solution for the P1.
I only found ADC (PASM) and PWM (SPIN).
pic18f2550
Posts: 400
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.
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.
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 )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 driveor clearer still
PUB dacinit( pinnum ) ctrb := %00110 << 26 | pinnum ' DUTY single-ended dira[pinnum] := true ' enable pin drive