Forums

Using PWM on an LPC2101

Started by rfkd07 July 29, 2008
Hey there,

unfortunately I have quite some troubles when trying to use PWM on
pin P0.8 (MAT2.1) of my LPC2101 controller (without the use of
interrupts). The following code is what I have until now:

// Configure P0.8
PINSEL0 &= ~(3<<16);
PINSEL0 |= 2<<16;

T2TCR = 1<<1; // Reset TC and PC, disable timer
T2PR = 5; // Prescaling
T2MCR = 1<<10; // Reset on MR3 match
PWM0CON = 1<<3; // Use single edged PWM for MR3
T2EMR = 1<<1 | 3<<6; // Enable MAT2.1 and set toggle mode

// Setup PWM parameters
unsigned short cycles = 49152;
T2MR3 = cycles; // Period duration
T2MR1 = cycles/4; // Pulse width
T2TCR = 1<<0; // Enable counter

Prescaling and the value of the 'cycles' variable are computed
depending on the frequency, the values above are just examples for
F 0Hz (PCLKX,982,400Hz). My problem is that the pulse remains
constantly high for 5ms, then constantly low for 5ms. Seems like MR1
is getting ignored. Any help available? Thanks in advance.

-rfkd07

An Engineer's Guide to the LPC2100 Series

A couple things to check:
PLL multipler (make sure ur clock speed is what you think)
Peripheral clock divider (R U dividing main clock by 2?)

--- In l..., "rfkd07" wrote:
>
> Hey there,
>
> unfortunately I have quite some troubles when trying to use PWM on
> pin P0.8 (MAT2.1) of my LPC2101 controller (without the use of
> interrupts). The following code is what I have until now:
>
> // Configure P0.8
> PINSEL0 &= ~(3<<16);
> PINSEL0 |= 2<<16;
>
> T2TCR = 1<<1; // Reset TC and PC, disable timer
> T2PR = 5; // Prescaling
> T2MCR = 1<<10; // Reset on MR3 match
> PWM0CON = 1<<3; // Use single edged PWM for MR3
> T2EMR = 1<<1 | 3<<6; // Enable MAT2.1 and set toggle mode
>
> // Setup PWM parameters
> unsigned short cycles = 49152;
> T2MR3 = cycles; // Period duration
> T2MR1 = cycles/4; // Pulse width
> T2TCR = 1<<0; // Enable counter
>
> Prescaling and the value of the 'cycles' variable are computed
> depending on the frequency, the values above are just examples for
> F 0Hz (PCLKX,982,400Hz). My problem is that the pulse remains
> constantly high for 5ms, then constantly low for 5ms. Seems like MR1
> is getting ignored. Any help available? Thanks in advance.
>
> -rfkd07
>

Thanks for your reply. The PLL is fine I think and my PCLK is running
at the same speed as CCLK (~58 MHz). System ticks based software
timers work perfectly so I think the problem must be somewhere else.
Any other ideas?
--- In l..., "wvanhoomissen" wrote:
>
> A couple things to check:
> PLL multipler (make sure ur clock speed is what you think)
> Peripheral clock divider (R U dividing main clock by 2?)
>

Hi ?,
> // Configure P0.8
> PINSEL0 &= ~(3<<16);
> PINSEL0 |= 2<<16;
>
> T2TCR = 1<<1; // Reset TC and PC, disable timer
> T2PR = 5; // Prescaling
> T2MCR = 1<<10; // Reset on MR3 match
> PWM0CON = 1<<3; // Use single edged PWM for MR3
> T2EMR = 1<<1 | 3<<6; // Enable MAT2.1 and set toggle mode
>
> // Setup PWM parameters
> unsigned short cycles = 49152;
> T2MR3 = cycles; // Period duration
> T2MR1 = cycles/4; // Pulse width
> T2TCR = 1<<0; // Enable counter
>

one line, two problems: :-)
> PWM0CON = 1<<3; // Use single edged PWM for MR3

You have to use PWM2CON instead of PWM0CON (which is for Timer0!).
And you want MAT2.1 to be a PWM output, not MAT2.3, so you have to set bit 1 as in:
PWM2CON = 1 << 1; // Use single edged PWM for MR1

There is a typo in the User Manual: The PWM control registers for timers 2 and 3 are named PWM0CON and PWM1CON. Of course they need to be PWM2CON and PWM3CON. The register address is correct.
Probably your header file is ok, so in fact your setting controls Timer0 instead of Timer2.

You don't have to set T2EMR for PWM mode, because it is ignored there. But since you don't enable PWM yet, it DOES have an effect (toggle every full cycle).

This code works for me:
T2TCR = 2;
T2PR = 5;
T2MCR = 1 << 10; // MR3 will reset the timer
PWM2CON = 1 << 1; // MR1 controls MAT2.1 in PWM mode
T2MR3 = cycles-1;
T2MR1 = (cycles/4)-1;
T2TCR = 1;
Regards,
Rolf
----- Ursprgliche Mail ----
Von: rfkd07
An: l...
Gesendet: Dienstag, den 29. Juli 2008, 12:21:04 Uhr
Betreff: [lpc2000] Using PWM on an LPC2101
Hey there,

unfortunately I have quite some troubles when trying to use PWM on
pin P0.8 (MAT2.1) of my LPC2101 controller (without the use of
interrupts). The following code is what I have until now:

// Configure P0.8
PINSEL0 &= ~(3<<16);
PINSEL0 |= 2<<16;

T2TCR = 1<<1; // Reset TC and PC, disable timer
T2PR = 5; // Prescaling
T2MCR = 1<<10; // Reset on MR3 match
PWM0CON = 1<<3; // Use single edged PWM for MR3
T2EMR = 1<<1 | 3<<6; // Enable MAT2.1 and set toggle mode

// Setup PWM parameters
unsigned short cycles = 49152;
T2MR3 = cycles; // Period duration
T2MR1 = cycles/4; // Pulse width
T2TCR = 1<<0; // Enable counter

Prescaling and the value of the 'cycles' variable are computed
depending on the frequency, the values above are just examples for
F=200Hz (PCLK=58,982, 400Hz). My problem is that the pulse remains
constantly high for 5ms, then constantly low for 5ms. Seems like MR1
is getting ignored. Any help available? Thanks in advance.

-rfkd07


__________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com

Remove the line setting up T2EMR and set up the RIGHT bit in the
RIGHT PWMCON register.

Jeff

--- In l..., "rfkd07" wrote:
>
> Hey there,
>
> unfortunately I have quite some troubles when trying to use PWM on
> pin P0.8 (MAT2.1) of my LPC2101 controller (without the use of
> interrupts). The following code is what I have until now:
>
> // Configure P0.8
> PINSEL0 &= ~(3<<16);
> PINSEL0 |= 2<<16;
>
> T2TCR = 1<<1; // Reset TC and PC, disable timer
> T2PR = 5; // Prescaling
> T2MCR = 1<<10; // Reset on MR3 match
> PWM0CON = 1<<3; // Use single edged PWM for MR3
> T2EMR = 1<<1 | 3<<6; // Enable MAT2.1 and set toggle mode
>
> // Setup PWM parameters
> unsigned short cycles = 49152;
> T2MR3 = cycles; // Period duration
> T2MR1 = cycles/4; // Pulse width
> T2TCR = 1<<0; // Enable counter
>
> Prescaling and the value of the 'cycles' variable are computed
> depending on the frequency, the values above are just examples for
> F 0Hz (PCLKX,982,400Hz). My problem is that the pulse remains
> constantly high for 5ms, then constantly low for 5ms. Seems like MR1
> is getting ignored. Any help available? Thanks in advance.
>
> -rfkd07
>

Thanks Jeff, that solved my problem. Stupid me.
--- In l..., "ksdoubleshooter" wrote:
>
> Remove the line setting up T2EMR and set up the RIGHT bit in the
> RIGHT PWMCON register.
>
> Jeff
>