[PIC] How to properly set PWM duty cycle

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[PIC] How to properly set PWM duty cycle

James Burkart-2
Hey, everyone!

I am trying to figure out how to properly set the duty cycle for a PWM on a
PIC16F1503. I'm sure I am missing something obvious.

System clock is the internal oscillator at 16MHz.
TMR2 prescaler is 1:64, period is set to 2.496 ms.
This gives the PWM a 9-bit resolution.

The PWM frequency is 400 Hz. My goal is to be able to adjust the pulse
width from 1 ms to 2 ms (controlling an ESC).

I'm using the MCC generated function:

void PWM2_LoadDutyValue(uint16_t dutyValue)

I was assuming that making dutyValue 512 would give me a 100% duty cycle,
instead I get a duty cycle of around 82%.
A dutyValue of 624 gets me 100%, 312 gets me 50%.

What am I not getting?

--
Sincerely,

James Burkart
*925.667.7175*
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to properly set PWM duty cycle

Nicola Perotto
Hi James,
find attached a small spreadsheet to do calculations.
It confirm (part of) your calculations.
I don't know the library you are using.
Some info are missing:
- code snippet, including PWM initialization
- PR2 value (the period is calculated from this, not the inverse), my value is 155
- "What am I not getting?" is actually useless... describe with details!
Regards
        Nicola


On 29/07/2020 06:07, James Burkart wrote:

> Hey, everyone!
>
> I am trying to figure out how to properly set the duty cycle for a PWM on a
> PIC16F1503. I'm sure I am missing something obvious.
>
> System clock is the internal oscillator at 16MHz.
> TMR2 prescaler is 1:64, period is set to 2.496 ms.
> This gives the PWM a 9-bit resolution.
>
> The PWM frequency is 400 Hz. My goal is to be able to adjust the pulse
> width from 1 ms to 2 ms (controlling an ESC).
>
> I'm using the MCC generated function:
>
> void PWM2_LoadDutyValue(uint16_t dutyValue)
>
> I was assuming that making dutyValue 512 would give me a 100% duty cycle,
> instead I get a duty cycle of around 82%.
> A dutyValue of 624 gets me 100%, 312 gets me 50%.
>
> What am I not getting?
>
> --
> Sincerely,
>
> James Burkart
> *925.667.7175*
--

Nicola Perotto - NP Engineering
Sistemi Informatici ed Elettronici
skype: nicolap


--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

pic_pwm.xls (18K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to properly set PWM duty cycle

James Burkart-2
"What am I not getting?" Is very useful. It tells you that I don't
understand, possibly not even enough to know what information you need to
help me.

What library? I did say I'm using MCC generated functions. Do you mean you
would like to know what version of MCC?

PR2 is calculated by MCC. I'll dig through the generated code and see what
it is set to. There is something wrong with MCC: it's not showing the
proper PWM frequency. My last question to the PIC list was about that
problem.

Thanks, Nicola, for pointing me in the right direction. You're a wonderful
human bean!!

-- James



On Wed, Jul 29, 2020, 1:12 AM Nicola Perotto <[hidden email]>
wrote:

> Hi James,
> find attached a small spreadsheet to do calculations.
> It confirm (part of) your calculations.
> I don't know the library you are using.
> Some info are missing:
> - code snippet, including PWM initialization
> - PR2 value (the period is calculated from this, not the inverse), my
> value is 155
> - "What am I not getting?" is actually useless... describe with details!
> Regards
>         Nicola
>
>
> On 29/07/2020 06:07, James Burkart wrote:
> > Hey, everyone!
> >
> > I am trying to figure out how to properly set the duty cycle for a PWM
> on a
> > PIC16F1503. I'm sure I am missing something obvious.
> >
> > System clock is the internal oscillator at 16MHz.
> > TMR2 prescaler is 1:64, period is set to 2.496 ms.
> > This gives the PWM a 9-bit resolution.
> >
> > The PWM frequency is 400 Hz. My goal is to be able to adjust the pulse
> > width from 1 ms to 2 ms (controlling an ESC).
> >
> > I'm using the MCC generated function:
> >
> > void PWM2_LoadDutyValue(uint16_t dutyValue)
> >
> > I was assuming that making dutyValue 512 would give me a 100% duty cycle,
> > instead I get a duty cycle of around 82%.
> > A dutyValue of 624 gets me 100%, 312 gets me 50%.
> >
> > What am I not getting?
> >
> > --
> > Sincerely,
> >
> > James Burkart
> > *925.667.7175*
>
> --
>
> Nicola Perotto - NP Engineering
> Sistemi Informatici ed Elettronici
> skype: nicolap
>
> --
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to properly set PWM duty cycle

Nicola Perotto
Hi James,

On 29/07/2020 14:35, James Burkart wrote:
> "What am I not getting?" Is very useful. It tells you that I don't
> understand, possibly not even enough to know what information you need to
> help me.
My bad, I misunderstood the sentence, I speak better Asm than English ;-)

>
> What library? I did say I'm using MCC generated functions. Do you mean you
> would like to know what version of MCC?
I never used MCC so for me is not significant but for other people can be:
version of tools and libraries is often important.


>
> PR2 is calculated by MCC. I'll dig through the generated code and see what
> it is set to. There is something wrong with MCC: it's not showing the
> proper PWM frequency.
This was another missing information item :-)
With PWM you have to check:
- there is "something" in the pin;
- the frequency is correct or if it's wrong is an integer (sub-)multiple of
required frequency;
- the duty is correct.
If something is wrong the first step is to obtain a pwm, with a fixed and known
duct cycle.
Than I work with frequency, usually with a very minimal program to exclude other
causes.

>   My last question to the PIC list was about that
> problem.
>
> Thanks, Nicola, for pointing me in the right direction. You're a wonderful
> human bean!!
Bean, beam... maybe but human?!?!?! Are you sure? ;-)
Happy to help
       Nicola



>
> -- James
>
>
>
> On Wed, Jul 29, 2020, 1:12 AM Nicola Perotto <[hidden email]>
> wrote:
>
>> Hi James,
>> find attached a small spreadsheet to do calculations.
>> It confirm (part of) your calculations.
>> I don't know the library you are using.
>> Some info are missing:
>> - code snippet, including PWM initialization
>> - PR2 value (the period is calculated from this, not the inverse), my
>> value is 155
>> - "What am I not getting?" is actually useless... describe with details!
>> Regards
>>          Nicola
>>
>>
>> On 29/07/2020 06:07, James Burkart wrote:
>>> Hey, everyone!
>>>
>>> I am trying to figure out how to properly set the duty cycle for a PWM
>> on a
>>> PIC16F1503. I'm sure I am missing something obvious.
>>>
>>> System clock is the internal oscillator at 16MHz.
>>> TMR2 prescaler is 1:64, period is set to 2.496 ms.
>>> This gives the PWM a 9-bit resolution.
>>>
>>> The PWM frequency is 400 Hz. My goal is to be able to adjust the pulse
>>> width from 1 ms to 2 ms (controlling an ESC).
>>>
>>> I'm using the MCC generated function:
>>>
>>> void PWM2_LoadDutyValue(uint16_t dutyValue)
>>>
>>> I was assuming that making dutyValue 512 would give me a 100% duty cycle,
>>> instead I get a duty cycle of around 82%.
>>> A dutyValue of 624 gets me 100%, 312 gets me 50%.
>>>
>>> What am I not getting?
>>>
>>> --
>>> Sincerely,
>>>
>>> James Burkart
>>> *925.667.7175*
>> --
>>
>> Nicola Perotto - NP Engineering
>> Sistemi Informatici ed Elettronici
>> skype: nicolap
>>
>> --
>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>> View/change your membership options at
>> http://mailman.mit.edu/mailman/listinfo/piclist
>>

--

Nicola Perotto - NP Engineering
Sistemi Informatici ed Elettronici
skype: nicolap

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to properly set PWM duty cycle

James Burkart-2
Thanks, Nicola.

Human bean, it's a compliment. It is someone who, though might be a little
jaded, is willing to help someone for the greater good.

On Wed, Jul 29, 2020, 8:04 AM Nicola Perotto <[hidden email]>
wrote:

> Hi James,
>
> On 29/07/2020 14:35, James Burkart wrote:
> > "What am I not getting?" Is very useful. It tells you that I don't
> > understand, possibly not even enough to know what information you need to
> > help me.
> My bad, I misunderstood the sentence, I speak better Asm than English ;-)
>
> >
> > What library? I did say I'm using MCC generated functions. Do you mean
> you
> > would like to know what version of MCC?
> I never used MCC so for me is not significant but for other people can be:
> version of tools and libraries is often important.
>
>
> >
> > PR2 is calculated by MCC. I'll dig through the generated code and see
> what
> > it is set to. There is something wrong with MCC: it's not showing the
> > proper PWM frequency.
> This was another missing information item :-)
> With PWM you have to check:
> - there is "something" in the pin;
> - the frequency is correct or if it's wrong is an integer (sub-)multiple
> of
> required frequency;
> - the duty is correct.
> If something is wrong the first step is to obtain a pwm, with a fixed and
> known
> duct cycle.
> Than I work with frequency, usually with a very minimal program to exclude
> other
> causes.
>
> >   My last question to the PIC list was about that
> > problem.
> >
> > Thanks, Nicola, for pointing me in the right direction. You're a
> wonderful
> > human bean!!
> Bean, beam... maybe but human?!?!?! Are you sure? ;-)
> Happy to help
>        Nicola
>
>
>
> >
> > -- James
> >
> >
> >
> > On Wed, Jul 29, 2020, 1:12 AM Nicola Perotto <[hidden email]>
> > wrote:
> >
> >> Hi James,
> >> find attached a small spreadsheet to do calculations.
> >> It confirm (part of) your calculations.
> >> I don't know the library you are using.
> >> Some info are missing:
> >> - code snippet, including PWM initialization
> >> - PR2 value (the period is calculated from this, not the inverse), my
> >> value is 155
> >> - "What am I not getting?" is actually useless... describe with details!
> >> Regards
> >>          Nicola
> >>
> >>
> >> On 29/07/2020 06:07, James Burkart wrote:
> >>> Hey, everyone!
> >>>
> >>> I am trying to figure out how to properly set the duty cycle for a PWM
> >> on a
> >>> PIC16F1503. I'm sure I am missing something obvious.
> >>>
> >>> System clock is the internal oscillator at 16MHz.
> >>> TMR2 prescaler is 1:64, period is set to 2.496 ms.
> >>> This gives the PWM a 9-bit resolution.
> >>>
> >>> The PWM frequency is 400 Hz. My goal is to be able to adjust the pulse
> >>> width from 1 ms to 2 ms (controlling an ESC).
> >>>
> >>> I'm using the MCC generated function:
> >>>
> >>> void PWM2_LoadDutyValue(uint16_t dutyValue)
> >>>
> >>> I was assuming that making dutyValue 512 would give me a 100% duty
> cycle,
> >>> instead I get a duty cycle of around 82%.
> >>> A dutyValue of 624 gets me 100%, 312 gets me 50%.
> >>>
> >>> What am I not getting?
> >>>
> >>> --
> >>> Sincerely,
> >>>
> >>> James Burkart
> >>> *925.667.7175*
> >> --
> >>
> >> Nicola Perotto - NP Engineering
> >> Sistemi Informatici ed Elettronici
> >> skype: nicolap
> >>
> >> --
> >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> >> View/change your membership options at
> >> http://mailman.mit.edu/mailman/listinfo/piclist
> >>
>
> --
>
> Nicola Perotto - NP Engineering
> Sistemi Informatici ed Elettronici
> skype: nicolap
>
> --
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist