Low Frequency PWM

Post here first, or if you can't find a relevant section!
User avatar
Pito
Posts: 994
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Low Frequency PWM

Postby Pito » Fri Apr 22, 2016 4:02 pm

I messed with PWM on the F4 recently (see viewtopic.php?f=39&t=1031 ).
There is a function called setPeriod(pwmPeriod_in_useconds) which returns a max_duty value. This maxduty value is important..
The function calculates internally values for a hw prescaler (1..65536) and also "timer_overflow" values, and it sets up the hw timer such it fits your pwm period values as best as possible.
For example>
maxduty = timer.setPeriod(20000);
sets the PWM period to 20ms (pwm freq=50Hz) and it returns for example maxduty=64515.
Thus you can go with your duty from 0..64515.

F4 Examples only:
pwmWrite(pwm_pin, x); // where 0<=x<=64515 for pwm period=20000us (50Hz)
pwmWrite(pwm_pin, x); // where 0<=x<=56000 for pwm period=1000us (1kHz)
pwmWrite(pwm_pin, x); // where 0<=x<=16800 for pwm period=100us (10kHz)

In F4 there is a bug in the setPeriod() function somewhere (not somewhere frankly - it calculates with a wrong clock_per_usecs value actually), so the pwm period I measured with my LA is twice (half the pwm frequency) of the one set by the setPeriod function..

Mind the maxduty depends on the pwm Period, so it is NOT always "65535" (ie only 16800 for pwm period=100usecs).

Here is the complete PWM source I messed with for the F407disco, it may work with F1 too..
viewtopic.php?f=39&t=1001#p11792
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 5588
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Low Frequency PWM

Postby RogerClark » Fri Apr 22, 2016 10:29 pm

When people have specs that say "very precise", it makes me wonder why they are using the Arduino system.

IMHO its not designed for precision. You can get it to be precise, but I would assume that it is not precise.

For precision, I suspect you would be better off using the STMCube to export code that just does the function you require, and not have all the things going on under the hood that the Arduino system hides from casual users.

Or like several people have suggested, use a separate dedicated PMW generator IC

DrBanana
Posts: 49
Joined: Fri Apr 22, 2016 11:15 am

Re: Low Frequency PWM

Postby DrBanana » Sat Apr 23, 2016 12:31 am

Or like several people have suggested, use a separate dedicated PMW generator IC

Could you please name some of them ? The suggested only have 12 bit resolution as my requirement is in microseconds and milliseconds.
Like

200000uS Period(5Hz)
900uS Pulse Width

250000 Period(4Hz)
900uS Pulse Width

and like this. The range can go from 4Hz - 100Hz and Pulse Width can be 900uS to 100ms. See the difference ratio is way high between pulse width and period. I think 12 bit resolution will not work. Even 16 bit wont.

User avatar
RogerClark
Posts: 5588
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Low Frequency PWM

Postby RogerClark » Sat Apr 23, 2016 1:13 am

What accuracy do you require

e.g. 1uS

User avatar
martinayotte
Posts: 1170
Joined: Mon Apr 27, 2015 1:45 pm

Re: Low Frequency PWM

Postby martinayotte » Sat Apr 23, 2016 1:27 am

DrBanana wrote:Could you please name some of them ? The suggested only have 12 bit resolution as my requirement is in microseconds and milliseconds.

PCA9685 !
See also this recent thread : viewtopic.php?f=3&t=1041&p=12445&hilit=PCA9685#p12445
If Internal Clock doesn't fit your need, simply use External Clock ...

User avatar
Vassilis
Posts: 294
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: Low Frequency PWM

Postby Vassilis » Sat Apr 23, 2016 8:27 am

To achieve the lower frequency (4 Hz) you need to modify the clock configuration that is predefined in Arduino_STM32 core to 72 MHz.
As an example I show you the above picture from my CubeMX clock configuration.
The APB1 Timer clocks needs to be 0.25 MHz (250 kHz) to get the 65535 period. That gives you a 262ms period length that is close to the 250ms you want. A small adjustment to the 65535 value will give you the exact 250ms period you want.

I used Timer 3 to run a few tests
htim3.Init.Period = 62500 gives you 250 ms period length = 4 Hz
htim3.Init.Period = 2500 gives you 10ms period length = 100 Hz

As other people suggested, you either have to use an additional chip to do that or use the CubeMX software to write the source code according to your needs because in my opinion, that case is something that goes out from the limits of arduino core.
Attachments
LowFreqPWM.JPG
LowFreqPWM.JPG (89.33 KiB) Viewed 657 times
Last edited by Vassilis on Sun Apr 24, 2016 11:08 am, edited 1 time in total.

User avatar
Pito
Posts: 994
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Low Frequency PWM

Postby Pito » Sat Apr 23, 2016 9:59 am

Excel is your friend. You may find the setPeriod() function in HardwareTimer.cpp in STM32F1. So you may use it in your code easily.
This is what you get based on calculation the setPeriod() provides for you:
F1 PWM.JPG
F1 PWM.JPG (126.28 KiB) Viewed 641 times

As you may see the 1us resolution with lowest pwm frequencies to achieve is a nogo.
The easiest way would be an FPGA or CPLD. The libraries usually include everything you may need :)
Last edited by Pito on Sat Apr 23, 2016 11:24 am, edited 4 times in total.
Pukao Hats Cleaning Services Ltd.

DrBanana
Posts: 49
Joined: Fri Apr 22, 2016 11:15 am

Re: Low Frequency PWM

Postby DrBanana » Sat Apr 23, 2016 10:26 am

The easiest way would be an FPGA or CPLD. The libraries usually include everything you may need

What they would be? Google seems no help. Could you please explain a bit about those 2.

Thank you.

PS: nvm I think they are some sort of components. Got more stuff on Google.

User avatar
Pito
Posts: 994
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Low Frequency PWM

Postby Pito » Sat Apr 23, 2016 10:29 am

Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 5588
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Low Frequency PWM

Postby RogerClark » Sat Apr 23, 2016 11:24 am

DrBanana wrote:
The easiest way would be an FPGA or CPLD. The libraries usually include everything you may need

What they would be? Google seems no help. Could you please explain a bit about those 2.

Thank you.

PS: nvm I think they are some sort of components. Got more stuff on Google.



https://en.wikipedia.org/wiki/Field-pro ... gate_array

https://en.wikipedia.org/wiki/Complex_p ... gic_device

The are programmable logic chips. They are ideal for some sorts of processing, which would probably include accurate PWM creation.

I suspect that the Cypress PSoC device may also be suitable, as it may fall into the CPLD category.
http://www.cypress.com/products/32-bit- ... tex-m-psoc

https://www.youtube.com/watch?v=pmu-sC_lyhM

(@mrburnette is our resident PSoC expert ;-)


Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 3 guests