need help with PWM
CON
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x '80MHz
act = 1, tb = 2, mode = 3, up = 4, down = 5
VAR long Lpsi, time, co2psi, t1, r1, p1, t2, r2, p2, t3, r3, p3, t4, r4, p4, stack[50]
PUB Main
Pwm(50, 1, 6)
PUB Pwm(duty, t, pin)
ctra[30..26] := %00100 ' Configure Counter A to NCO
ctra[5..0] := pin
frqa := 1
dira[pin]~~
duty :=(clkfreq/20)/(100/duty)
t := cnt
repeat
phsa := -duty 'sets duty percent
t += (clkfreq/20) '20hz pulse
waitcnt(t)
Im trying to tell this pwm method a specific duty cycle % values based on a 20hz cycle using PUB main . But with anything over 50 the output stops producing a signal and just stays high. What am I doing wrong here?
thanks

Comments
By "anything over 50" do you mean "duty over 50"???
If so, what happens to 100/duty when duty is greater than 50? Remember that operations like that will only give you integers.
Could that be a problem?
Also, I think there's at least one PWM object in the OBEX. Have you tried looking there?
duty := CONSTANT(clkfreq/2000) * duty
Since 2000 divides 80_000_000 evenly, there is no error and you avoid the integer division problem that ElectricAye pointed out.
In SPIN, 100/51 will yield an integer value of 1 .
Is that really what you want?
If you need to use fractional values, you will have to use the FloatMath object or use a trick like Tracy just pointed out.
-Phil
took "CONSTANT" out and it works perfect.
THANKS!!!!
I just realized that the constant _clkfreq cannot be used unless it is defined in the initial CONstants block. I had the idea that defining _xinfreq would automatically allow the _clkfreq to be used. So the OP cannot use _clkfreq, because he used _xinfreq. Is that right? It gives a complile error.
I made the same assumption you did, given the codependency of those constants. (I wonder what would happen if you specified all three, but with contradictory values. :devil:)
-Phil