A "?" about the PWM object.
I was thinking I needed better duty resolution than 0-100 as is the current limit on the PWM object in the OB-Exchange.
Would I be exceeding some limit if I were to set the limit to 10_000?
Since I am not as fluent in assembly as I would like to be I can not guess on what being done. So this Is why I ask. If I substitute a part of the code with the following will serious problems surface?
Would I be exceeding some limit if I were to set the limit to 10_000?
{A simple pwm object based on code from AN001 - propeller counters
Author: Jev Kuznetsov
date : 16 Oktober 2007
usage
OBJ
pwm : pwmAsm
....
pwm.start( Pin) ' start pwm
pwm.SetPeriod( period ) ' set pwm period in clock cycles
pwm.SetDuty( duty) ' set duty in %
pwm.Stop
}
VAR
long cog
long sDuty ' order important (the variables are read from memory in this order)
long sPinOut
long sCtraVal
long sPeriod
PUB Start( Pin) : success
'start pwm on Pin @ 80 kHz
longfill(@sDuty, 0, 4)
sDuty := 50 ' default duty
sPinOut := |< Pin
sCtraVal := %00100 << 26 + Pin
sPeriod := 1000
stop
Success := (cog := Cognew(@entry,@sDuty) + 1)
PUB stop
'' Stop object - frees a cog
if cog
cogstop (cog~ -1)
longfill(@sDuty, 0, 4)
PUB SetPeriod(counts)
' set pwm period in clock cycles, frequency = (_clkfreq / period)
sPeriod := counts
PUB SetDuty(counts)
if (counts < 0)
counts := 0
if (counts > 100)
counts := 100
sDuty := counts * sPeriod / 100
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 t1,par 'get first parameter
rdlong value, t1
add t1,#4
rdlong pinOut, t1
or dira, pinOut ' set pinOut to output
add t1, #4
rdlong ctraval, t1
mov ctra, ctraval 'establish counter A mode and APIN
add t1, #4
rdlong period, t1
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 trips "value" cycles from now
jmp #:loop 'loop for next cycle
period res 1
time res 1
value res 1
t1 res 1
pinOut res 1
ctraval res 1
Since I am not as fluent in assembly as I would like to be I can not guess on what being done. So this Is why I ask. If I substitute a part of the code with the following will serious problems surface?
PUB SetDuty(counts)
if (counts < 0)
counts := 0
if (counts > 100000)
counts := 100000
sDuty := counts * sPeriod / 100000

Comments
Hum It seems to be working so far I just hate using something I am not completely familiar with. Thanks for the quick response Philldapill
That is, if I understand the PWM object correctly. Any "wizards" want to chime in?(PhiPi?)
My problem is I cant seem to find it. If I set the period to 8_000 it seems to work. If I go over this it wants to not work which is backwards as I calculate it to be
Given
frequency = (clkfreq / period)
8_000 = (80Mhz / period)
I get a total period of 10Khz
Any thing over 8_000 i.e. 10_000 should lower the period making it seemingly work better ? Am I thinking correctly here?