Forums

PWM Duty Cycle

Started by Ravi May 5, 2008
Hi all,
I'm working on LPC2148 for PWM generation.
Below is part of code used for setting of PWM module.

PWMPCR = 0x00001000; //Enable single edge controlled PWM4
PWMMCR = 0x00000003; //Reset timer on MR0 match,generate interrupt
PWMPR = 0x00000004; //prescaler value
PWMMR0 = 0x00000064; //30Khz cycle rate (crystal 12Mhz)
PWMMR4 = 0x00000032; //Dutycycle = 50%
PWMTCR = 0x00000002; //Reset timer counter and prescaler counter on next Pclk cycle
PWMTCR= 0x00000009;//enable PWM mode

I'm getting correct cycle rate(Freq) butwith above code (PWMMR4=0x32) I'm getting 54.3% duty cyle,with PWMMR4=0x19 I'm getting 63% duty cyle which I feel is not correct, it should come 25%.I tried different PWMMR4 values, I'm not getting lenear relation between PWMMR4 value and dutycycle.
Where I'm wrong?
Please help.
Thanx in advance
Waiting...........
Ravi

An Engineer's Guide to the LPC2100 Series

Ravi Wrote
>I'm working on LPC2148 for PWM generation.
>Below is part of code used for setting of PWM module.
>
>PWMPCR = 0x00001000; //Enable single edge controlled PWM4
>PWMMCR = 0x00000003; //Reset timer on MR0 match,generate interrupt
>PWMPR = 0x00000004; //prescaler value
>PWMMR0 = 0x00000064; //30Khz cycle rate (crystal 12Mhz)
>PWMMR4 = 0x00000032; //Dutycycle = 50%
>PWMTCR = 0x00000002; //Reset timer counter and prescaler counter on next
Pclk cycle
>PWMTCR= 0x00000009;//enable PWM mode
>
>I'm getting correct cycle rate(Freq) butwith above code (PWMMR4=0x32) I'm
getting 54.3% >duty cyle,

Or 45.7%

>with PWMMR4=0x19 I'm getting 63% duty cyle

or 37%

>which I feel is not correct, it should come 25%.I tried different PWMMR4
values, I'm not >getting lenear relation between PWMMR4 value and dutycycle.
>Where I'm wrong?

How are you measuring duty cycle? And where in your circuit?

Robert

--------------------------------
mail2web LIVE Free email based on Microsoft Exchange technology -
http://link.mail2web.com/LIVE

Thanx for ur quick reply
I'm measuring it with multimeter directly on port pin.
I'm getting very accurate frequency readings.
regards
Ravi
On Mon, 05 May 2008 s...@aeolusdevelopment.com wrote :
>Ravi Wrote
> >I'm working on LPC2148 for PWM generation.
> >Below is part of code used for setting of PWM module.
> >
> >PWMPCR = 0x00001000; //Enable single edge controlled PWM4
> >PWMMCR = 0x00000003; //Reset timer on MR0 match,generate interrupt
> >PWMPR = 0x00000004; //prescaler value
> >PWMMR0 = 0x00000064; //30Khz cycle rate (crystal 12Mhz)
> >PWMMR4 = 0x00000032; //Dutycycle = 50%
> >PWMTCR = 0x00000002; //Reset timer counter and prescaler counter on next
>Pclk cycle
> >PWMTCR= 0x00000009;//enable PWM mode
> >
> >I'm getting correct cycle rate(Freq) butwith above code (PWMMR4=0x32) I'm
>getting 54.3% >duty cyle,
>
>Or 45.7%
>
> >with PWMMR4=0x19 I'm getting 63% duty cyle
>
>or 37%
>
> >which I feel is not correct, it should come 25%.I tried different PWMMR4
>values, I'm not >getting lenear relation between PWMMR4 value and dutycycle.
> >Where I'm wrong?
>
>How are you measuring duty cycle? And where in your circuit?
>
>Robert
>
>--------------------------------
>mail2web LIVE Free email based on Microsoft Exchange technology -
>http://link.mail2web.com/LIVE

Ravi Wrote
>Thanx for ur quick reply
>I'm measuring it with multimeter directly on port pin.

By measuring voltage? If so that may explain the problem. You really need
an oscilloscope for this sort of measurement.

Robert
--------------------------------
mail2web - Check your email from the web at
http://link.mail2web.com/mail2web

Ravi wrote:
> Thanx for ur quick reply
> I'm measuring it with multimeter directly on port pin.
> I'm getting very accurate frequency readings.
> regards
> Ravi
> On Mon, 05 May 2008 s...@aeolusdevelopment.com wrote :
>> Ravi Wrote
>>> I'm working on LPC2148 for PWM generation.
>>> Below is part of code used for setting of PWM module.
>>>
>>>
>>> I'm getting correct cycle rate(Freq) butwith above code (PWMMR4=0x32) I'm
>> getting 54.3% >duty cyle,
>>
>> Or 45.7%
>>
>>> with PWMMR4=0x19 I'm getting 63% duty cyle
>> or 37%
>>
>>> which I feel is not correct, it should come 25%.I tried different PWMMR4
>> values, I'm not >getting lenear relation between PWMMR4 value and dutycycle.
>>> Where I'm wrong?
>> How are you measuring duty cycle? And where in your circuit?
>>
>> Robert
>>
>> --------------------------------
Hi Ravi,

I use the PWM on the 2148 and I run it up to 50KHz cycles. Robert is correct that you need a scope. If you are using a frequency counter on a DVM they have a sample rate as fast as 1ms or slower. That would account for the differences.

regards,
Charles

--- In l..., "subscriptions@..."
wrote:
>
> Ravi Wrote
> >Thanx for ur quick reply
> >I'm measuring it with multimeter directly on port pin.
>
> By measuring voltage? If so that may explain the problem. You
really need
> an oscilloscope for this sort of measurement.
>
> Robert
> -------------------------------
-
> mail2web - Check your email from the web at
> http://link.mail2web.com/mail2web
>
hi ravi and robert,
I 'm using PWM to control a serrvo motor....
to control a motor...u nead 6 PWM's (in each pair each PWM has to
be the compliment of the other within that pair....)

I would like to know if we will be able to generate a compliment for
the same....
If so plz type down the particular part of code wer the compliment
is made..thanx in advance
regards
Rajeev
Hi Rajeev,
for LPC2000 I've not seen any such option for complement output,
I feel use of inverter(NOT gate) at the output port is better solution.
Ravi

On Wed, 07 May 2008 urjust2oocool wrote :
>--- In l..., "subscriptions@..."
> wrote:
> >
> > Ravi Wrote
> > >Thanx for ur quick reply
> > >I'm measuring it with multimeter directly on port pin.
> >
> > By measuring voltage? If so that may explain the problem. You
>really need
> > an oscilloscope for this sort of measurement.
> >
> > Robert
> >
> >
> > -------------------------------
>-
> > mail2web - Check your email from the web at
> > http://link.mail2web.com/mail2web
> >
>hi ravi and robert,
>I 'm using PWM to control a serrvo motor....
>to control a motor...u nead 6 PWM's (in each pair each PWM has to
>be the compliment of the other within that pair....)
>
>I would like to know if we will be able to generate a compliment for
>the same....
>If so plz type down the particular part of code wer the compliment
>is made..thanx in advance
>regards
>Rajeev
>

you can compliment by doing XOR with 1.
a^1 = ~a
for bits that you need complimented, you build a bit mask, such as if
bit 3 needs to be complimented, it would read 00001000 then you do XOR
with the data and it will flip that bit.
Rand3289

--- Ravi wrote:

> Hi Rajeev,
> for LPC2000 I've not seen any such option for complement output,
> I feel use of inverter(NOT gate) at the output port is better
> solution.
> Ravi
> On Wed, 07 May 2008 urjust2oocool wrote :
> >--- In l..., "subscriptions@..."
> > wrote:
> > >
> > > Ravi Wrote
> > > >Thanx for ur quick reply
> > > >I'm measuring it with multimeter directly on port pin.
> > >
> > > By measuring voltage? If so that may explain the problem. You
> >really need
> > > an oscilloscope for this sort of measurement.
> > >
> > > Robert
> > >
> > >
> > >
> -------------------------------
> >-
> > > mail2web - Check your email from the web at
> > > http://link.mail2web.com/mail2web
> > >
> >hi ravi and robert,
> >I 'm using PWM to control a serrvo motor....
> >to control a motor...u nead 6 PWM's (in each pair each PWM has to
> >be the compliment of the other within that pair....)
> >
> >I would like to know if we will be able to generate a compliment for
> >the same....
> >If so plz type down the particular part of code wer the compliment
> >is made..thanx in advance
> >regards
> >Rajeev
> >
>

____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Hi Rand3289,
How can U do 'a^1 = ~a' with PWM output ?

Ravi

On Wed, 07 May 2008 rand3289 wrote :
>you can compliment by doing XOR with 1.
>a^1 = ~a
>for bits that you need complimented, you build a bit mask, such as if
>bit 3 needs to be complimented, it would read 00001000 then you do XOR
>with the data and it will flip that bit.
>Rand3289
>
>--- Ravi wrote:
>
> > Hi Rajeev,
> > for LPC2000 I've not seen any such option for complement output,
> > I feel use of inverter(NOT gate) at the output port is better
> > solution.
> >
> >
> > Ravi
> >
> >
> >
> >
> > On Wed, 07 May 2008 urjust2oocool wrote :
> > >--- In l..., "subscriptions@..."
> > > wrote:
> > > >
> > > > Ravi Wrote
> > > > >Thanx for ur quick reply
> > > > >I'm measuring it with multimeter directly on port pin.
> > > >
> > > > By measuring voltage? If so that may explain the problem. You
> > >really need
> > > > an oscilloscope for this sort of measurement.
> > > >
> > > > Robert
> > > >
> > > >
> > > >
> > -------------------------------
> > >-
> > > > mail2web - Check your email from the web at
> > > > http://link.mail2web.com/mail2web
> > > >
> > >hi ravi and robert,
> > >I 'm using PWM to control a serrvo motor....
> > >to control a motor...u nead 6 PWM's (in each pair each PWM has to
> > >be the compliment of the other within that pair....)
> > >
> > >I would like to know if we will be able to generate a compliment for
> > >the same....
> > >If so plz type down the particular part of code wer the compliment
> > >is made..thanx in advance
> > >regards
> > >Rajeev
> > >
> >
> >
> >
> >
> > ____________________________________________________________________________________
>Be a better friend, newshound, and
>know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

My bad, I did not realize PWM was generated in hardware.
In that case, if there is an "interrupt on pin change" you could link
PWM output to an input pin, each change would trigger an interrupt and
in software one would output a negative of the input on another pin.
If the PWM frequency is not too high, this might work.

Otherwise, you are right, a NOT gate is the best solution. I used it on
my H-bridges to drive 2 pairs of mosfets, however NOT gate is so fast,
it created a shoot-through current and one of the mosfets will heat up.

rand3289

--- Ravi wrote:

> Hi Rand3289,
> How can U do 'a^1 = ~a' with PWM output ?
>
> Ravi
>
> On Wed, 07 May 2008 rand3289 wrote :
> >you can compliment by doing XOR with 1.
> >a^1 = ~a
> >for bits that you need complimented, you build a bit mask, such as
> if
> >bit 3 needs to be complimented, it would read 00001000 then you do
> XOR
> >with the data and it will flip that bit.
> >Rand3289
> >
> >--- Ravi wrote:
> >
> > > Hi Rajeev,
> > > for LPC2000 I've not seen any such option for complement output,
> > > I feel use of inverter(NOT gate) at the output port is better
> > > solution.
> > >
> > >
> > > Ravi
> > >
> > >
> > >
> > >
> > > On Wed, 07 May 2008 urjust2oocool wrote :
> > > >--- In l..., "subscriptions@..."
> > > > wrote:
> > > > >
> > > > > Ravi Wrote
> > > > > >Thanx for ur quick reply
> > > > > >I'm measuring it with multimeter directly on port pin.
> > > > >
> > > > > By measuring voltage? If so that may explain the problem.
> You
> > > >really need
> > > > > an oscilloscope for this sort of measurement.
> > > > >
> > > > > Robert
> > > > >
> > > > >
> > > > >
> > >
> -------------------------------
> > > >-
> > > > > mail2web - Check your email from the web at
> > > > > http://link.mail2web.com/mail2web
> > > > >
> > > >hi ravi and robert,
> > > >I 'm using PWM to control a serrvo motor....
> > > >to control a motor...u nead 6 PWM's (in each pair each PWM has
> to
> > > >be the compliment of the other within that pair....)
> > > >
> > > >I would like to know if we will be able to generate a compliment
> for
> > > >the same....
> > > >If so plz type down the particular part of code wer the
> compliment
> > > >is made..thanx in advance
> > > >regards
> > > >Rajeev
> > > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
>
____________________________________________________________________________________
> >Be a better friend, newshound, and
> >know-it-all with Yahoo! Mobile. Try it now.
> http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>

____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ