Skip to content
LDmicroGitHub edited this page Dec 14, 2016 · 9 revisions

There are some cases that require attention when using PWM.
PIC and AVR notes:
1.1) In this case PWM duty 15% sets every PLC cycle time 3 ms.
image
As you can see in the picture below, sometimes calculation and setting PWM duty overrides the expected PWM output impulse.
image
Solution: When the PWM duty is constant you can set it only once (OSR used).
image

1.2) See datasheet for determine the amount of PWM modules of MCU.
If Timer0 has the PWM mode you can simply use it.
Else, to have more then one the hardware PWM output, you should use the Timer0 as the PLC cycle source. Then you can use Timer1 and Timer2 in PWM mode and can get two or more PWM outputs.
image

1.3) You can use RESET element to disable PWM output and set it to low level.
Also you can set another PWM base frequency after RESET PWM.
See RESET PWM

PIC notes:
2.1) Fragment from PIC datasheet:
...
When two CCP modules are in a PWM mode, Timer2 is the time-base for both PWM1(based on Timer1) and
PWM2(based on Timer2) outputs.
This means that they will have the same PWM frequency, as determined by the Timer2 prescaler and frequency of the device.
...
So PWM frequency must be identical, duty cycle may be different.
image

2.2) PIC duty 0% has no narrow spike, see 3.1)

AVR notes:
3.1) Fragment from AVR datasheet:
...
The extreme values for the OCR1x Register represents special cases when generating
a PWM waveform output in the fast PWM mode.
If the OCR1x is set equal to BOTTOM (0x0000 = 0% duty) the output will be a narrow spike
for each timer clock cycle.
Setting the OCR1x equal to TOP ( = 100% duty) will result in a constant high.
...
Thus, SET PWM = 0% does not disable pulses on the PWM output.

Use RESET element to disable PWM output and set it to low level.

3.2) Real PWM base frequency may be different from the specified in "Set PWM" element.
PWM frequency error does not exceed 5%.
image
image
Available several fixed values of PWM base frequency , depending on the frequency of the processor Fclk and selected prescaler.
image
LDmicro gets your target frequency, calculates the prescaler, then calculate the fact PWM frequency, and if target and fact differ more 5% send Message.
image
So with 16MHz oscillator you can select
65.2k, 7.8k, 976, 244 or 61 Hz PWM frequency only.

Any additional, special cases please report to LDmicro.GitHub@gmail.com

Clone this wiki locally