PDA

View Full Version : PWM - Dutycycle affecting frequency?



flotulopex
- 17th May 2007, 10:23
Hi,

I'm just playing around with PWM (...and PicMultiCalc).

While making some experiments, I notice that changing the dutycycle will affect the Period's duration (frequency).

First, I set dutycylce to 127 (=50%). The frequency is 92,24kHz.
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1640&stc=1&d=1179393197">

Second, same test at 64 (=25%) dutycycle. The frequency is now 46,02kHz.
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1639&stc=1&d=1179393197">

In my comprehension of PWM, dutycycle would affect only the ratio of the HIGH against the LOW signal.

Is this okay or am I measuring something wrong?

mister_e
- 17th May 2007, 12:27
i guess something is wrong in your code. Can you post it here?

Acetronics2
- 17th May 2007, 12:57
Triggering parameters and signal amplitude ... please !!!

Alain

mister_e
- 17th May 2007, 13:09
36 mVPP :eek: looks like the scope is set in AC mode.

flotulopex
- 17th May 2007, 15:08
So, here's the code:
' Fuses
@ DEVICE PIC12F683,FCMEN_OFF
@ DEVICE PIC12F683,IESO_OFF
@ DEVICE PIC12F683,BOD_OFF
@ DEVICE PIC12F683,CPD_OFF
@ DEVICE PIC12F683,PROTECT_OFF
@ DEVICE PIC12F683,MCLR_OFF
@ DEVICE PIC12F683,WDT_OFF
@ DEVICE PIC12F683,INTRC_OSC_NOCLKOUT

'-------------------------------------------------------------------------------
' Registers 76543210
ANSEL = %00000000 'Disable analog inputs
ADCON0 = %00000000 'ADC is OFF
CMCON0 = %00000111 'Comparator is OFF
OPTION_REG = %10000000 'Pull-Ups disabled...
TRISIO = %00000000 'All PORTBs are Outputs
GPIO = %00000000 'All PORTBs are Low

'-------------------------------------------------------------------------------
' Defines

'-------------------------------------------------------------------------------
' Variables

'-------------------------------------------------------------------------------
' Program
MAIN:
PWM GPIO.2,64,1000
goto main
end

And here is how the oscillo is set:
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1649&stc=1&d=1179410753">

Acetronics2
- 17th May 2007, 15:15
Hi,Flotul ...

Not good to change PWM parameters all the time !!! ( a re-write is a change ... )

DO NOT use PWM in such a tight loop !!!

And try it removing the loop ... will work fine,then !!!

Alain

skimask
- 17th May 2007, 15:25
MAIN:
PWM GPIO.2,64,1000
goto main
end[/code]



Try adding a 'DEFINE NO_CLRWDT 1' and see if the freq still changes, or changes as much.

PBP (by default) adds a ClearWDT instruction before every PBP instruction. So, when you get down to the smaller duty cycle, that instruction executes more and more often, affecting your PWM frequency.

The problem with the PWM command, being software driven, is that it's frequency is affected by everything else in the program as a whole. It doesn't matter how short you make the program (as you've made the program as practically short as it can get), it'll still be affected by the 'goto main', it'll even be affected if the 'goto main' jumps across a page boundary.

PWM driven from an interrupt or a hardware module is about the only way to go if you want to keep a stable base frequency.

There's nothing wrong with your program, or your setup, or your 'scope settings...ya just need a bit more understanding the logic that's running underneath the PWM command...

Acetronics2
- 17th May 2007, 15:57
Heuuuu, ReuTeuFeuMeu ....

[ PBP (by default) adds a ClearWDT instruction before every PBP instruction ]

That's really new, skimask ... 2.48 release ???

Worse than "ON INTERRUPT" ... if I understand.

Alain

skimask
- 17th May 2007, 16:44
Heuuuu, ReuTeuFeuMeu ....
[ PBP (by default) adds a ClearWDT instruction before every PBP instruction ]
That's really new, skimask ... 2.48 release ???
Worse than "ON INTERRUPT" ... if I understand.
Alain

Check any .lst file, you'll see it. And it's explained (it's a bit vague, have to read between the lines a bit) under the CLEARWDT command.

Compile a program, see how many bytes it takes up...
Then add the 'DEFINE NO_CLRWDT 1', see how many fewer bytes it takes up.

Incidentally, I didn't RTFM before I found this out awhile back. I was actually RTF.lst file and wondering what all those extra instructions were for :D Then I RTFM'd and found the info and confirmed that info with a quick search here on 'CLEARWDT'.

Acetronics2
- 17th May 2007, 17:16
Hi,Skimask

When Compiling a project, I Always have the MPLAB Program window under the eyes ... and can see how much CLRWDT are really added in the HEX ...

Reading the manual, you'll find "automatically inserted at appropriate places" ... which is RIGHT !!!

Just have a look to it to be convinced

Alain

OK for the DEFINE ... stops inserting CLRWDT.

I sometimes use it if too close from the prog space limit ... to get some room.

skimask
- 17th May 2007, 17:28
Reading the manual, you'll find "automatically inserted at appropriate places" ... which is RIGHT !!!

I'm still searching for what I was talking about. I KNOW I read somewhere awhile back that PBP was inserting the CLEARWDT before each PBP command (not PIC instruction) starting with one of the newer versions. It used to be after the command in the older versions, and it used to be not nearly as often as needed. Back when I first got PBP 2.3.something, the WDT would kick out once in awhile, and the .lst file showed CLEARWDT almost at random places, really didn't correspond well with how much time a specific sequence of instructions would use or anything.
At any rate, yes, 'automatically inserted at appropriate places'...in which the appropriate place would be before an instruction vs. after it.

Regardless, the faster you run thru the PWM routine posted earlier, with the smaller duty cycles, the greater the percentage of CLEARWDT instructions executed per unit time.

Although I don't know in this case if those 'extra' CLEARWDT instructions would account for the 2x difference in frequency. Hardware/Interrupt driven PWM is still the way to go.

mister_e
- 17th May 2007, 23:37
Sorry to interrupt, but i wonder why using pwm while the PIC have a CCP?

And... Does this thread remind something familiar ;) ?
http://www.picbasic.co.uk/forum/showthread.php?t=6300&highlight=duty

flotulopex
- 18th May 2007, 07:57
You're right Steeve,

I could, I should and I actually use the CCP module of my 12F683.

But not all "small" PICs have this option so I want to make sure I can get the same result in both cases (HPWM & PWM).

The other aspect of the problem is costs related. For your information, the PIC's prices in Switzerland are "slightly" different as those I can see in MICROCHIP's website or on components suppliers out of my country. Where a 12F683 costs $ 0,87 in USA, I will pay CHF 5,49 ($ 4,47)!!!

So, even for fun, I don't like to throw money out of my window ;)

Acetronics2
- 18th May 2007, 08:14
Hi,Flotul

Did you read what Melabs tell about PWM wave shape at $ 5.51 ??? ( First of all ... I thought you talked about Hpwm !!! ( RTFQuestion for me ...).

I think your scope gave you the Low. Freq. ENVELOPE ot the signal ... may be ( surely ...) you should have a different picture on an analog scope !!!

Digital scopes are fine ... yes, but with a comfortable sampling frequency !!!

~ 200 Euros ( 180 with your 20% "discount" ... ) scopes have their limits ... you just found some ...


You won't have a neat wave shape with PWM ... Never. Try to borrow a 50 or 100 Mhz analog scope ... to see what PWM looks like.

Alain

flotulopex
- 18th May 2007, 09:32
You might be right: my cheap oscillo has its limits.

I can grab a LeCroy at the office but to me, its like a "Jumbo Jet 747" (must have at least 30 contextual buttons on the front pannel and a 3 kilos user manual...).

But maybe I'll try this anyway.

Nothing is impossible...