PWM oddness

Coyoteboy

Senior Member
OK, I'm pretty up to speed on the smaller 'axes but I'm clearly missing something here...I'm trying to generate a 5KHz 50% DC on C.1 of a 40x2 4MHz resonator, my initial try was:

pwmout C.1 , 199, 400

Scoped the pin, excellent, I seem to have a 50% DC output. On closer inspection though I'm at 10KHz and fairly regularly I get a blip of ~100% extended "off" time.

I have literally nothing else going on on the chip at the time, so it's not "busy" for any reason I can see, and it's always extended off times, not on. I figured I'd mis-calculated so I used the wizard and got the same command, so I checked the resonator and its definitely 4MHz.

What's going on?!

I have a feeling it is something to do with the fact that I'm using an external 4MHz res (as I would have with the non-X 40 part) and the X2 is defaulting to using its internal resonator at 8MHz? I've only just noticed this addition now I'm scurrying through the commands datasheet, both of these points would be better placed on the basic parts datasheets IMO.

Edit again:
On closer inspection, the 10kHz was due to running at 8MHz, however the extended low time still remains a problem.
 
Last edited:

hippy

Technical Support
Staff member
How are you specifying the operating frequency ? "SETFREQ EM16" is what you'd have with an external 4MHz plus the x4 PLL. The chip shouldn't be defaulting to internal 8MHz unless there were a problem.

Also, how does your code use PWMOUT ? If it's in a loop you can get glitches when it executes and resets the internal PWM timer, which is why PWMDUTY came into existence.
 

Coyoteboy

Senior Member
I was not specifying my frequency at all initially - assuming it would work as the normal default 4MHz from the external osc as on a previous 28 project I created, and that further modification upwards would require tweaks and higher external oscs. Now I delve deeper into the datasheets it becomes more obvious!

As for the code for the PWM, I'm simply looping main at the moment as a tester:
main:
pwmout
goto main

Presumably this glitch is where the PWM duty is being reset then, however as the duty is not changing I was expecting no change/glitch to occur. That does explain my issue, thanks!
 

Technical

Technical Support
Staff member
The X2 chip runs at internal 8MHz until you tell it otherwise e.g. 'setfreq em16' in your case

Your test program is constantly 'restarting' the pwm timer. This is what gives the glitches.

Your test program should look like this:

pwmout x,x,x
main:
goto main

or, to test two speeds

pwmout x,x,x
pause 10000
pwmduty y,y
main:
goto main
 
Top