PWM Problem
John Abshier
Posts: 1,116
I thought that this code, a modification to the PWM example in the counter application note would give me a 20kHz period.· The measured values (Parallax oscilloscope) are approximately 50Hz (actual value 48Hz).· I'm off by a factor of 400.· I did a simple Spin test to toggle a pin at 1kHz and it tested out OK on the oscilloscope.
''Demonstration of PWM version of NCO/PWM counter mode
CON _clkmode = xtal1 + pll8x
_xinfreq = 10_000_000
VAR long parameter
PUB go | x
cognew(@entry, @parameter)
repeat
parameter := 1000 'a constant here locks to value x percent high
waitcnt(1_000_000 + cnt) 'wait a little while before next update
DAT
'assembly cog which updates the PWM cycle on APIN
'for audio PWM, fundamental freq which must be out of auditory range (period < 50µS)
org
entry mov dira, diraval 'set APIN to output
mov ctra, ctraval 'establish counter A mode and APIN
mov frqa, #1 'set counter to increment 1 each cycle
mov time, cnt 'record current time
add time, period 'establish next period
:loop rdlong value, par 'get an up to date pulse width
waitcnt time, period 'wait until next period
neg phsa, value 'back up phsa so that it
jmp #:loop 'loop for next cycle
diraval long 3 'APIN=0 BPIN=0
ctraval long %00101 << 26 + (%000001 << 9)+ 0 'NCO-D/PWM APIN=0 BPIN = 1
period long 4000 '20kHz (50µS period) (_clkfreq / period)
time res 1
value res 1

Comments