Page 1 of 1

PIC32 PWM Component

Posted: Sun Aug 14, 2016 5:34 pm
by QMESAR
Hi Team

It seems that I can not use the full range of the PWM on PIC32 as I can only adjust the Period and duty similar to a PIC8
I would expect to be able to adjust between 0 -65535 and not only 255
My apologies if my understanding is incorrect

Thank you for looking into this :D

Re: PIC32 PWM Component

Posted: Mon Aug 15, 2016 5:04 pm
by Benj
Hello Peter,

Yes the PWM on the PIC32 should be 16-bit.

I believe I have found and fixed the problem now.

The Period should default to 255 but can now be edited up to 65535.

Use the SetDuty10Bit macro to assign a duty over the range 0-255.
cal_pwm.fcpx
(8.07 KiB) Downloaded 298 times
Let me know how you get on.

Re: PIC32 PWM Component

Posted: Mon Aug 15, 2016 5:18 pm
by QMESAR
Hello Ben

Thank you very much for the help, I will let you know how I get along :D

Re: PIC32 PWM Component

Posted: Mon Aug 15, 2016 7:36 pm
by QMESAR
Hi Ben,

The PWM pin now is active however we still have some issues or misunderstanding from my side ,

As you will see in the Flowchart I enable the PWM pin and I set the following in the properties
(1) the period to 20 000 which shows a time of 500 uSec as expected( I want 2Khz)
(2) set the Timer to Timer2
(3) do a 8bit avarage on a adc pin and load this 0-255 value to the DC regsiter ( I also loaded fix value of 127 and 255 )

The results as you will see in the scope prints
(1) the Frequency or period is double the expected so my 2Khz i2 4 Khz
(2) The duty cycle never goes bigger than 2.9 uSec about 2% max of the period

Note: I am sure I get the 255 in the adc8Bit value as when I display this variable in PORTD I can light up all 8 Leds and any numer down to 0

Sorry if I am doing something wrong :D

Re: PIC32 PWM Component

Posted: Mon Aug 15, 2016 9:09 pm
by Benj
Thanks Peter,

Sounds like another bug to me, should be easy to solve so I'll get on it.

Re: PIC32 PWM Component

Posted: Mon Aug 15, 2016 9:17 pm
by QMESAR
Thanks Ben ;
have good one :D

Re: PIC32 PWM Component

Posted: Tue Aug 16, 2016 10:35 am
by Benj
Hi Peter,

Right here is another component fix which hopefully better reflects what the PIC32 PWM hardware is doing.
cal_pwm.fcpx
(8.09 KiB) Downloaded 283 times
Let me know how you get on.

Re: PIC32 PWM Component

Posted: Tue Aug 16, 2016 6:08 pm
by QMESAR
Hi Ben;

Thank you very much this file is working fine the following observations

(1) The perioad fit exactly to the setting in PWM properties
(2) The Duty cycle does not work with values between 0 -and 255 as you mentioned in a previous post

In my understanding from the PIC32 the Duty cycle value must match the Period value to achieve 100% Duty cycle
there for when I apply a value between 0 and the 40000 which is the period cycle or value I can scale the Duty cycle exactly between 0 and 100%

I achievde this by doing a 10 bit ADC read(0 -1023) and mulitiply by 40 which is for a fast test 0 -40000 :D (also load 40000 directly to the setDutycycle Function )
Benj wrote: Use the SetDuty10Bit macro to assign a duty over the range 0-255.
I am only concerned if the setDutycycle() function expects 0-255 then something is still wrong however my asuption above is based on my experience using the PWM
in C code and there it works as I mentioned above and now in FC also like it is now with the last file you posted

Many Thanks once again.

Have good one