PIC32MZ2048EFH064 PWM not working properly

Any bugs you encounter with Flowcode should be discussed here.
SpeedPIC32
Posts: 278
http://meble-kuchenne.info.pl
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

the Pic runs with 200 MHz change
Period 65535 Prescaler 32 Duty 10Bit 32678 results in 1,525KHz Duty 50%
Period 32768 Prescaler 32 Duty 10Bit 32678 results in 3,052KHz Duty ~5% from estimate :cry:
Period 32768 Prescaler 64 Duty 10Bit 32678 results in 47Hz Duty ~5% from estimated :cry:
Period 65535 Prescaler 256 Duty 10Bit 32678 results in 5Hz Duty 50%.
The prescaler can be set under PWM1 properties.
Period Frequency is calculated and displayed.
Prescaler Frequency
1 3.051KHz
2 3.051KHz :x
4 762.939Hz
8 762.939Hz :x
16 190.734Hz
32 95.367Hz
64 47.683Hz
256 11.92Hz

where is 128
then the jumps
Duty cannot be set correctly

Please change Thank you

BenR
Matrix Staff
Posts: 1952
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 510 times
Been thanked: 697 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by BenR »

Hello,

This is interesting, can you post your project so we can see the way your calling the PWM functions just to make sure that is not a contributing factor.

Hopefully it's somethign fairly easy for us to iron out.

SpeedPIC32
Posts: 278
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

Hello,

I actually only wanted to have a frequency of around 500 Hz without a lot of programming and also a Duti of 50%.

I proceeded as described.
I set and compiled every line in the PIC and looked at the result on the oscilloscope. I rubbed my eyes a few times and then called for help.

Something is wrong

Thanks already
and have fun :mrgreen:
Attachments
pwm dsp mz2048efh064.fcfx
(12.74 KiB) Downloaded 510 times

BenR
Matrix Staff
Posts: 1952
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 510 times
Been thanked: 697 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by BenR »

Hello,

Many thanks for letting us know. I've had a look through the CAL code and hopefully now solved a problem where some of the prescaler options would default to 1:1. Specifically 1:2, 1:4, 1:16 and 1:32 options were all missing from the code.

Please let me know how you get on with this fix.

SpeedPIC32
Posts: 278
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

Hello Benn,
it's going a little better.
What about the divider 128, it's still doing stupid things.
Then one more question:
how much is 10 bit???
For me it is 1023
to enter 10 bits in SetDutyCycle in FC9 should be 65535? :shock: (16Bit?) :oops:
The next error
Period 32768,256 Duty 16384 = 50%
Period 65535,256 Duty 16384 = 25%
Period 65535,256 Duty 32768 = 50%
Period 32768,256 Duty 32768 = 100%
Period 32768.1 Duty 32765 = 99%

Here's to a new one
have fun searching and :lol:
please test :D

SpeedPIC32
Posts: 278
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

Hello Benn,
can it be that changePeriod and SetDutyCycle are interchanged?

SpeedPIC32
Posts: 278
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

Hello Benn,
The smaller the value in ChangePeriod, the larger the DutyCycle, i.e. the time for the signal.
If I make SetDutyCycle smaller, the one time will be also becomes smaller.
For 50% DutyCycle you have to enter half the value of ChangePeriod and not the usual one-time 50% and no matter what value is in Peiod, Duty remains at 50%.

Thanks already

BenR
Matrix Staff
Posts: 1952
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 510 times
Been thanked: 697 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by BenR »

Hello,

I have spent most of the morning looking into this and I'm finding out quite a lot regarding the PIC32 MZ. They have multiple peripheral clocks that control the dividers of the frequency for things like the UART, SPI, I2C and the PWM. Annoyingly these dividers seem to default to different values and not 1:1. I've now managed to tame these so that things run at the right frequency.

Next we have the PWM, you mentioned a problem with the prescaler of 128, there is no 128 setting available on the 32MZxxxEF devices it's either 64 or 256 so hopefully that is the problem there. If you enter 128 then you will get the default prescaler of 1.

I've checked that the duty and the period are doing what they should be doing and this all looks to be correct, at least for me.

A period of 65535 and a duty of 32768 gives 50% duty.
A period of 65535 and a duty of 16384 gives 25% duty.
A period of 32768 and a duty of 16384 gives 50% duty.

The naming of the functions is legacy and you're right it is misleading. I might try and change this to make it less confusing.

SetDuty8Bit allows for a duty of 0-255 to be loaded.
SetDuty10Bit allows for a 16-bit duty of 0-65535 to be loaded.

The 10-bit comes from when we only supported 8-bit PIC and AVR devices and 10-bit was the max PWM duty but in reality this allows up to 16-bit duty control.

I'm going to do some more testing and then I'll roll out the changes, at least to the MZ EF family.

Edit. This fix has now been pushed to the v8 and v9 update systems. Let me know how you get on.

SpeedPIC32
Posts: 278
Joined: Thu Dec 10, 2020 2:35 pm
Location: Germany
Has thanked: 37 times
Been thanked: 17 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by SpeedPIC32 »

Hallo,

A period of 32768 and a duty of 16384 gives 50% duty.

I don't agree with that.
If I set a duty of 50% = 50% of 65535, this is a value of 65535 / 2, i.e. half. :shock:
The frequency does not play a role here.
Since when do I have to recalculate both values if I only want to change the frequency?
Even worse, I want to change the transmitted power in a system, then I set a different value for Duty, that's it. It cannot and must not be that the frequency changes. Try this with a motor controller, you will quickly get electronic waste...
In any case, this is not right. Please have a look at your own PWM demo (homework not done). :mrgreen:


Thanks until here
now the rest :lol:

thanks Benn

BenR
Matrix Staff
Posts: 1952
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 510 times
Been thanked: 697 times

Re: PIC32MZ2048EFH064 PWM not working properly

Post by BenR »

Hello,

Please can you send me an example of your code, what you are expecting and what you are getting.
Since when do I have to recalculate both values if I only want to change the frequency?
The period and prescaler set the output frequency.

The duty sets the percentage on time.

If you want a variable frequency with a locked percentage on time then you have to edit both the period and the duty values together to ensure the duty remains a specific percentage of the period.

On the ESP32 devices you can specify the duty as a percentage but on the PIC devices the duty is simply a count value and has to be compared to the period to provide the percentage.

Post Reply