PWM - Duty Cycle and Period Overflow Values

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

PWM - Duty Cycle and Period Overflow Values

Post by Jay Dee »

Hi, Just been playing around with the PWM and had trouble getting it working, this will be well known to most but for those that don't, just like me about 1/2 a hour ago... :)

If you reduce the Period Overflow value to increase the PWM frequency... the output in hardware can go to a permanent logic High.
This might be because of the Duty cycle value is greater than the Period overflow value.

If you have a Period Overflow of 255, the valid (8bit) Dutycycle values will be from 0-255. so 50% is 128
If you have a Period Overflow of 100, the valid (8bit) Dutycycle values will be from 0-100. so 50% is 50
If you have a Period Overflow of 4, the valid (8bit) Dutycycle values will be from 0-4. so 50% is 2

In summery, you can increase the PWM frequency using a smaller overflow value but this decreases the resolution of the PWM dutycycle.
J.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Benj »

Hello,

You might be interested in knowing that you can get more resolution using the setduty10bit function.

If your period is say 4 then the setduty macro can be between 0 (0%) and 4 (100%)

However if you multiply the value by 4 to convert from 8-bit to 10-bit then you get a 10-bit period of 4x4=16 therefore the 10-bit duty can go from 0 to 16 when the 8-bit period is set to 4.

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

Hello Ben

Im not too sure what you mean by using the setduty10bit to increase the resolution of the period.

I have attached an example of a normal 8 bit duty cycle, would you be able to show be example how your theory of using the setduty10bit macro can be used to increase the resolution of the Period?

My input in the example is 12bit, 8 readings averaged in attempt to get a more stable value when run on hardware.

Thanks
Attachments
2.8 High Res Output.fcf
(79.82 KiB) Downloaded 299 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by medelec35 »

Hi Lord Grezington
When Ben was talking about period, I believe he was referring to the Period Register.
Normally when using PWM, period should remain the same & the duty is altered.
But you will need to alter the period register to fine tune to the desired PWM frequency.

For example:
If Period Register = 240
For 8bit:
min duty = 0
Max duty = Period Register value
So for 50% PWM
Duty = 240/100*50
120

For 10 bit PWM:
min duty = 0
Max duty = Period Register value x 4
so again for 50% duty
Duty = 240/100*50*4
=480

So if in your case period Register value is 255.
If you use SetDutyCycle = 8 bit
then for 50% duty the value has to be 255/100*50 = 127

If you use SetDutyCycle10bit = 10bit
then for 50% duty the value has to be 255/100*50*4 = 510

The resolution can be looked at the number of steps from 0 to max duty.

The 8bit version with period register set at 255 has 255 steps
The 10bit version with period register set at 255 has 255*4 steps = 1020 steps
So the resolution is for 10bit is 4 times greater than 8 bit, which is what Ben was referring to.

You can use the ChangePeriod component which will change the PWM to a new frequency, but if you do the PWM duty values will have to be altered if you still require the same duty.


Hope this helps.

Martin
Martin

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

Hi Medlec

I need to vary the frequency to from around 0 up to about 42Khz in a reasonably good resolution, the duty should remain @ 50% if possible.

I need to go to work now and not had a good chance to fully take in your response, but I will take another read of it this evening.

Thanks

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

Hello Martin/Ben

Is it possible to vary the frequency between 0-42Khz in increments of around 41Hz? (10 bits)

Will I need to use a different chip that can do this or perhaps try and somehow bit bang it?
If I bit bang it, how do I gain access to the clock the PWM uses? The hardware currently has a 10Mhz Xtal and running the PLL to push it up to 40Mhz. I am using an interupt on CLK0 on TMR0.

Thanks
Graham

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Benj »

Hello,

Not sure what the best method would be. My advise would be to have a go using hardware PWM and see how far you get. Then maybe try again using a software based PWM approach and see if this is better or worse. Once you hit a wall, post what you have so far and we will see if there is anything that could be improved or done slightly differently.

If you need help with software PWM interfaces then this article covers them fairly well.
http://www.matrixmultimedia.com/article.php?a=52

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by medelec35 »

If it was me I would use the hardware PWM to.
I have noticed that hardware PWM don't tie down resources at the extreme of it's range unlike software PWM.

As a starting point I created a sweep generator here that was for a pipe descaler and that changed the frequency while keeping duty at 50%.

Not experimented on the highest obtainable frequency, but with osc at 40MHz, 42KHz should be doable, but i'm not sure 41Hz increments is possible?

I will have a look when I get a chance
Martin

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

I have run some test on the PWM macro and the settings, I have attached the program...

Basically the code is meant to test the max and min values of the frequency (checked on a pico scope). I have noticed that changing the period register in the ext properties makes no difference to the period.

I ran the program firstly with the register set to 255, I got a min frequency of 2.36Khz and a max of 204.5Khz. I then change the register to 0 and I get the same 2.36Khz and 204.5Khz frequency variation. These settings change in the software but not when run on hardware.

I have had a look at doing it in hardware, I cant seem to find a cheap system that will work from 3.3V and provide more than 8 bit resolution. I have looked at using a 555 and varying
either the caps or resistance digitally but I cant seem to get the range with required resolution.

Medlec - I will try some of your ideas, but change them slightly. I will try and break the frequency up into 3 or 4 bands and offset each one and try and match the max and mins of each band. However this will only work if the register works which does not seem to be the case at the moment.

Thanks

"Added Code"
Attachments
Freq test.fcf
(21.5 KiB) Downloaded 295 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by medelec35 »

Hi Graham,
I believe its your flowchart that is causing the confusion.
What you can try is either set the PWM for a period. delay for 5 seconds then set for a different period, delay fir 5 seconds etc.

Or use a switch or a pot for setting different periods.

Simulator can show 5ms changes, but your eyes can't.
Only way of seeing the change is a when the waveform is being logged.

I'm wondering if you can use look-up tables to generate correct period and prescaller for the frequency steps required.
don't believe a formula will work to well since the lower the period, the greater step in frequency.
So its a non linear relationship.
Martin

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

Hi Martin

I had the time set to longer to start with, however it was tedious making changes. Since I was using a scope to check the change in Period I could increase the speed change to 5ms.

A look up table may work, but I still need to get the Period Register sorted out.

I get the below compiler message when I run the attached flowchart. The thing causing the failure is the Period Register. When changed to the same value it compiles ok.


Generated by: Flowcode v5.5.2.1
Date: Wednesday, April 16, 2014 07:50:44
http://www.matrixmultimedia.com
Launching the compiler...
C:\Program Files\Flowcode\v5\Tools\boostc\boostc_pic18_flowcode.exe -v -t PIC18F4423 "Freq test.c"
BoostC Optimizing C Compiler Version 7.04 (for PIC18 architecture)
http://www.sourceboost.com
Copyright(C) 2004-2011 Pavel Baranov
Copyright(C) 2004-2011 David Hobday

Licensed to FlowCode User under Single user Pro License for 1 node(s)
Limitations: PIC18 max code size:Unlimited, max RAM banks:Unlimited


Freq test.c
Starting preprocessor: C:\PROGRA~1\Flowcode\v5\Tools\boostc\pp.exe "Freq test.c" -i C:\PROGRA~1\Flowcode\v5\Tools\boostc\include -d _PIC18F4423 -la -c2 -o "Freq test.pp" -v -d _BOOSTC -d _PIC18 -d _CHAR_INDEX

Freq test.c(475): PWM period value must be the same in all PWM modules

1 errors detected
Error: preprocessing error

failure
Return code = 1
Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.


FINISHED
Attachments
Freq test.fcf
(18.39 KiB) Downloaded 237 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by medelec35 »

Hi Graham,
Properties for both PWM channels must be set to the same Period register and Prescaler divide otherwise you will get and error when compiling.

Bother PWM channels uses the same interrupt for generating the PWM freqency.
Therefore you can't have CCP1 set at 2.4KHz and CCP2 set at 312KHz at the same time,

Martin
Martin

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: PWM - Duty Cycle and Period Overflow Values

Post by Lord Grezington »

Hi Martin

I getting back onto this project again, surely if CCP1 is disabled before CCP2 in enabled it does not matter of the same interrupt is used?

Post Reply