EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

PWM Explanation

Started by todd...@hotmail.com January 24, 2011
I keep seeing this snippet of code that is supposed to explain how to generate PWM signals. However, there is no explanation for each of the keywords (e.g. PWM1TCT). I've read the user manual, but am having a difficult time figuring out valid values. Can anyone provide a detailed explanation for how to generate the associated values (e.g. (0x2 << 8)). I'm new to the nxp uC, and am looking for a source that walks you through the programming - rather than assumes you understand the nuances.

#include /* include for LPC23xx */

static void PWMInit(){
/* considering LPC2387 */
/* Select PWM1.2 output on P1.20 */
PINSEL3 |= (0x2 << 8);

/* reset PWM counter */
PWM1TCR = 0x02;

/* setup match control to reset on MR0 */
PWM1MCR = 0x02;

/* select single edge for PWM1.2 and enable PWM1.2 o/p */
PWM1PCR = 1 << 10;

/* setup PWM period */
PWM1MR0 = 100;

/* setup PWM duty cycle (50%) */
PWM1MR2 = 50;

/* start counter and enable PWM */
PWM1TCR = 0x81;

/* enable LER */
PWM1LER = 0x04;
}

int main(){
PWMInit();
while(1);
}

An Engineer's Guide to the LPC2100 Series

Hi,

Have you seen LPC23xx User Manual (http://ics.nxp.com/support/documents/microcontrollers/pdf/user.manual.lpc23xx.pdf) Chapter 24 Section 4 Sample waveform with rules for single and double edge control ?

Regards,
-daniel

--- On Mon, 1/24/11, t...@hotmail.com wrote:

> From: t...@hotmail.com
> Subject: [lpc2000] PWM Explanation
> To: l...
> Date: Monday, January 24, 2011, 4:27 PM
> I keep seeing this snippet of code
> that is supposed to explain how to generate PWM
> signals. However, there is no explanation for each of
> the keywords (e.g. PWM1TCT). I've read the user
> manual, but am having a difficult time figuring out valid
> values. Can anyone provide a detailed explanation for
> how to generate the associated values (e.g. (0x2 <<
> 8)). I'm new to the nxp uC, and am looking for a
> source that walks you through the programming - rather than
> assumes you understand the nuances.
>
> #include /* include for LPC23xx */
>
> static void PWMInit(){
> /* considering LPC2387 */
> /* Select PWM1.2 output on P1.20 */
> PINSEL3 |= (0x2 << 8);
>
> /* reset PWM counter */
> PWM1TCR = 0x02;
>
> /* setup match control to reset on MR0 */
> PWM1MCR = 0x02;
>
> /* select single edge for PWM1.2 and enable
> PWM1.2 o/p */
> PWM1PCR = 1 << 10;
>
> /* setup PWM period */
> PWM1MR0 = 100;
>
> /* setup PWM duty cycle (50%) */
> PWM1MR2 = 50;
>
> /* start counter and enable PWM */
> PWM1TCR = 0x81;
>
> /* enable LER */
> PWM1LER = 0x04;
> }
>
> int main(){
> PWMInit();
> while(1);
> }


--- In l..., toddgilbert@... wrote:
>
> I keep seeing this snippet of code that is supposed to explain how to generate PWM signals. However, there is no explanation for each of the keywords (e.g. PWM1TCT). I've read the user manual, but am having a difficult time figuring out valid values. Can anyone provide a detailed explanation for how to generate the associated values (e.g. (0x2 << 8)). I'm new to the nxp uC, and am looking for a source that walks you through the programming - rather than assumes you understand the nuances.
>
> #include /* include for LPC23xx */
>
> static void PWMInit(){
> /* considering LPC2387 */
> /* Select PWM1.2 output on P1.20 */
> PINSEL3 |= (0x2 << 8);
>
> /* reset PWM counter */
> PWM1TCR = 0x02;
>
> /* setup match control to reset on MR0 */
> PWM1MCR = 0x02;
>
> /* select single edge for PWM1.2 and enable PWM1.2 o/p */
> PWM1PCR = 1 << 10;
>
> /* setup PWM period */
> PWM1MR0 = 100;
>
> /* setup PWM duty cycle (50%) */
> PWM1MR2 = 50;
>
> /* start counter and enable PWM */
> PWM1TCR = 0x81;
>
> /* enable LER */
> PWM1LER = 0x04;
> }
>
> int main(){
> PWMInit();
> while(1);
> }
>

Have you had a look in the header lpc23xx.h file? They will de defined in there !!!

The 'keywords' are actually macros to hardware register addresses. If you have a look in the user manual for the processor (download from the nxp website), you'll be able to search for them an see the address they should be pointing to.

For the LPC2388, PWM1TCR is specified in chapter 24, section 7 'Register Description'. Your header file needs a definition for this such as:

#define PWM1TCR (*(volatile unsigned long *)0xE0018004)

The values i.e. (0x2 << 8) are found in the same section in the manual!

These aren't nuances, these are programming basics !!

I apologize if I offended anyone - not my intent - probably my frustration showing. BTW, I have seen the header file and chapter 24. What I'm confused about is how to assign values to those keywords, and when they are necessary to reference (maybe if I see more PWM examples, things will come together). For example, I've seen "PINSEL3 |= (0x2 << 8)" and "PINSEL3 |= 0x20". Meanwhile, PINSEL3 is defined in pairs (e.g. 11:10). How is PINSEL3 |= (0x2 << 8) related to PINSEL3, pins 9:8 - is the MSB (i.e. 9) the pin that defines PWM1.2?. I'm not quite sure how this all works. Thanks for your help.

To: l...
From: s...@accutest.co.uk
Date: Tue, 25 Jan 2011 09:41:36 +0000
Subject: [lpc2000] Re: PWM Explanation

--- In l..., toddgilbert@... wrote:
>
> I keep seeing this snippet of code that is supposed to explain how to generate PWM signals. However, there is no explanation for each of the keywords (e.g. PWM1TCT). I've read the user manual, but am having a difficult time figuring out valid values. Can anyone provide a detailed explanation for how to generate the associated values (e.g. (0x2 << 8)). I'm new to the nxp uC, and am looking for a source that walks you through the programming - rather than assumes you understand the nuances.
>
> #include /* include for LPC23xx */
>
> static void PWMInit(){
> /* considering LPC2387 */
> /* Select PWM1.2 output on P1.20 */
> PINSEL3 |= (0x2 << 8);
>
> /* reset PWM counter */
> PWM1TCR = 0x02;
>
> /* setup match control to reset on MR0 */
> PWM1MCR = 0x02;
>
> /* select single edge for PWM1.2 and enable PWM1.2 o/p */
> PWM1PCR = 1 << 10;
>
> /* setup PWM period */
> PWM1MR0 = 100;
>
> /* setup PWM duty cycle (50%) */
> PWM1MR2 = 50;
>
> /* start counter and enable PWM */
> PWM1TCR = 0x81;
>
> /* enable LER */
> PWM1LER = 0x04;
> }
>
> int main(){
> PWMInit();
> while(1);
> }
>

Have you had a look in the header lpc23xx.h file? They will de defined in there !!!

The 'keywords' are actually macros to hardware register addresses. If you have a look in the user manual for the processor (download from the nxp website), you'll be able to search for them an see the address they should be pointing to.

For the LPC2388, PWM1TCR is specified in chapter 24, section 7 'Register Description'. Your header file needs a definition for this such as:

#define PWM1TCR (*(volatile unsigned long *)0xE0018004)

The values i.e. (0x2 << 8) are found in the same section in the manual!

These aren't nuances, these are programming basics !!
--- In l..., Todd Gilbert wrote:
> I apologize if I offended anyone - not my intent - probably my
> frustration showing. BTW, I have seen the header file and chapter
> 24. What I'm confused about is how to assign values to those
> keywords, and when they are necessary to reference (maybe if I see
> more PWM examples, things will come together). For example, I've
> seen "PINSEL3 |= (0x2 << 8)" and "PINSEL3 |= 0x20". Meanwhile,
> PINSEL3 is defined in pairs (e.g. 11:10).
> How is PINSEL3 |= (0x2 << 8) related to PINSEL3, pins 9:8 - is the
> MSB (i.e. 9) the pin that defines PWM1.2?. I'm not quite sure how
> this all works. Thanks for your help.

The PINSEL3 settings are shown in Chap 9 Sections 5.4.x

PINSEL3 |= (0x2 << 8) sets *bits* 9:8 to '10' (binary). The table headings show that the 'Function when 10' is PWM1.2

PINSEL3 |= 0x20 sets bits 5:4 to '10' which is PWM1.1

Regards,
Chris Burrows

CFB Software
Astrobe v3.2: LPC2000 Oberon-07 Development System
http://www.astrobe.com

----Original Message----
From: l...
[mailto:l...] On Behalf Of Todd Gilbert
Sent: 25 January 2011 12:16 To: l...
Subject: RE: [lpc2000] Re: PWM Explanation

> I apologize if I offended anyone - not my intent -
> probably my frustration showing. BTW, I have seen the
> header file and chapter 24. What I'm confused about is
> how to assign values to those keywords, and when they are
> necessary to reference (maybe if I see more PWM examples,
> things will come together). For example, I've seen
> "PINSEL3 |= (0x2 << 8)" and "PINSEL3 |= 0x20".
> Meanwhile, PINSEL3 is defined in pairs (e.g. 11:10). How
> is PINSEL3 |= (0x2 << 8) related to PINSEL3, pins 9:8 -
> is the MSB (i.e. 9) the pin that defines PWM1.2?. I'm
> not quite sure how this all works. Thanks for your help.
(0x2)<<8 moves the bit pattern "10" to bit 8

i.e. bit 9 will be set, bit 8 will be clear

(0x03)<<8 would set both bits 9 and 8.

It's written like this to make it easier to understand what the program is doing, you could equally put PINSEL3 |= 0x0100 but then it's harder to see which bits are getting set.

For PINSEL, there are 2 bits for each IO pin which define what is connected to it.
Normally 00 is the standard IO pin then there are 3 other settings which connect a peripheral to the pin.

in the user manual section on Pin Connect Block, it tells you what these settings are.

--
Tim Mitchell

okay, I think I'm catching on. If I look at PWM1.3...

PINSEL3 |= 0x2 << 10; PINSEL3 |= 0x700; and PINSEL3 |= 1792 are all equivalent?

To: l...
From: t...@sabretechnology.co.uk
Date: Tue, 25 Jan 2011 12:40:50 +0000
Subject: RE: [lpc2000] Re: PWM Explanation

----Original Message----
From: l...
[mailto:l...] On Behalf Of Todd Gilbert
Sent: 25 January 2011 12:16 To: l...
Subject: RE: [lpc2000] Re: PWM Explanation

> I apologize if I offended anyone - not my intent -
> probably my frustration showing. BTW, I have seen the
> header file and chapter 24. What I'm confused about is
> how to assign values to those keywords, and when they are
> necessary to reference (maybe if I see more PWM examples,
> things will come together). For example, I've seen
> "PINSEL3 |= (0x2 << 8)" and "PINSEL3 |= 0x20".
> Meanwhile, PINSEL3 is defined in pairs (e.g. 11:10). How
> is PINSEL3 |= (0x2 << 8) related to PINSEL3, pins 9:8 -
> is the MSB (i.e. 9) the pin that defines PWM1.2?. I'm
> not quite sure how this all works. Thanks for your help.

(0x2)<<8 moves the bit pattern "10" to bit 8

i.e. bit 9 will be set, bit 8 will be clear

(0x03)<<8 would set both bits 9 and 8.

It's written like this to make it easier to understand what the program is doing, you could equally put PINSEL3 |= 0x0100 but then it's harder to see which bits are getting set.

For PINSEL, there are 2 bits for each IO pin which define what is connected to it.
Normally 00 is the standard IO pin then there are 3 other settings which connect a peripheral to the pin.

in the user manual section on Pin Connect Block, it tells you what these settings are.

--
Tim Mitchell
Sorry - meant PINSEL3 |= 0x2 << 10; PINSEL3 |= 0x800; and PINSEL3 |= 2048

From: t...@hotmail.com
To: l...
Subject: RE: [lpc2000] Re: PWM Explanation
Date: Tue, 25 Jan 2011 13:29:53 +0000

okay, I think I'm catching on. If I look at PWM1.3...

PINSEL3 |= 0x2 << 10; PINSEL3 |= 0x700; and PINSEL3 |= 1792 are all equivalent?

To: l...
From: t...@sabretechnology.co.uk
Date: Tue, 25 Jan 2011 12:40:50 +0000
Subject: RE: [lpc2000] Re: PWM Explanation

----Original Message----
From: l...
[mailto:l...] On Behalf Of Todd Gilbert
Sent: 25 January 2011 12:16 To: l...
Subject: RE: [lpc2000] Re: PWM Explanation

> I apologize if I offended anyone - not my intent -
> probably my frustration showing. BTW, I have seen the
> header file and chapter 24. What I'm confused about is
> how to assign values to those keywords, and when they are
> necessary to reference (maybe if I see more PWM examples,
> things will come together). For example, I've seen
> "PINSEL3 |= (0x2 << 8)" and "PINSEL3 |= 0x20".
> Meanwhile, PINSEL3 is defined in pairs (e.g. 11:10). How
> is PINSEL3 |= (0x2 << 8) related to PINSEL3, pins 9:8 -
> is the MSB (i.e. 9) the pin that defines PWM1.2?. I'm
> not quite sure how this all works. Thanks for your help.

(0x2)<<8 moves the bit pattern "10" to bit 8

i.e. bit 9 will be set, bit 8 will be clear

(0x03)<<8 would set both bits 9 and 8.

It's written like this to make it easier to understand what the program is doing, you could equally put PINSEL3 |= 0x0100 but then it's harder to see which bits are getting set.

For PINSEL, there are 2 bits for each IO pin which define what is connected to it.
Normally 00 is the standard IO pin then there are 3 other settings which connect a peripheral to the pin.

in the user manual section on Pin Connect Block, it tells you what these settings are.

--
Tim Mitchell
----Original Message----
From: l...
[mailto:l...] On Behalf Of Todd Gilbert
Sent: 25 January 2011 13:35 To: l...
Subject: RE: [lpc2000] Re: PWM Explanation

> Sorry - meant PINSEL3 |= 0x2 << 10; PINSEL3 |= 0x800; and
> PINSEL3 |= 2048

yes!

And I got it wrong too in my previous explanation, (0x02 << 8) sets bits 9 and 8 which is 0x200 not 0x100

--
Tim Mitchell

----Original Message----
From: l...
[mailto:l...] On Behalf Of Todd Gilbert
Sent: 25 January 2011 13:30 To: l...
Subject: RE: [lpc2000] Re: PWM Explanation

> okay, I think I'm catching on. If I look at PWM1.3...
>
> PINSEL3 |= 0x2 << 10; PINSEL3 |= 0x700; and PINSEL3 |> 1792 are all equivalent?

errr...not quite

0x02<<10 is 1000 0000 0000 in binary which I calculate as 0x800
--
Tim Mitchell


The 2024 Embedded Online Conference