Using 32bit PWM on an LPC1224

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using 32bit PWM on an LPC1224

2,876 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Wed Mar 13 08:20:48 MST 2013
I am trying to configure PIO0_18 as a PWM output

I have this:

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
LPC_CT32B0->EMR = (1<<EMC3)|(2<<EMC2)|(1<<EMC1)|(1<<EMC0);

LPC_IOCON->PIO0_18 &= ~0x07; LPC_IOCON->PIO0_18 |= 0x04; /* Timer0_32 MAT0 */
LPC_IOCON->PIO0_19 &= ~0x07; LPC_IOCON->PIO0_19 |= 0x04; /* Timer0_32 MAT1 */
        LPC_IOCON->PIO0_20 &= ~0x07; LPC_IOCON->PIO0_20 |= 0x04; /* Timer0_32 MAT2 */
LPC_IOCON->PIO0_21 &= ~0x07; LPC_IOCON->PIO0_21 |= 0x04; /* Timer0_32 MAT3 */

/* Enable the selected PWMs */
LPC_CT32B0->PWMC = 0x0000000F;

LPC_CT32B0->MCR = (1<<10) | (1 << 9);/* Interrupt AND Reset on MR3 */

LPC_CT32B0->MR0 = 50000;
LPC_CT32B0->MR1 = 50000;
LPC_CT32B0->MR2 = 50000;
LPC_CT32B0->MR3 = 50000;

I calculate that 12000000/50000 = 240Hz which is what I need

To set the pwm to say 50% I do this:

LPC_CT32B0->MR0 = 25000;

The problem is that the pwm output is off until it gets to approx 29500 ie 59% thereafter it increases up to 100% when LPC_CT32B0->MR0 = 50000

Does anyone know why low values on MR0 give no pwm output


I changed the pwm range to 500000 with a similar result ie nothing no pwm until MR0 reached 29500, this time of course the value would be about 5.9%. Unfortunately this means that the refresh rate is 24Hz!!
0 Kudos
Reply
14 Replies

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Fri Mar 15 01:58:26 MST 2013
On AVRs the timer match register can be changed at any time with no problems I would expect the ARM to be the same - it is probably something else.

I can get around it as you suggest by only enabling the interrupt when a pwm change is required. I think that 10ms interrupts will be ok as it is not all the time  - just when lights are being ramped up or dimmed down.
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Mar 14 15:59:21 MST 2013
Changing a 240Hz PWM 1000 times per second sounds like no good idea for me :)

Usually it's useful to enable PWM interrupts if changes are necessary and disable it again afterwards, but that's not helping while changing every 1 ms :eek:
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Thu Mar 14 09:46:04 MST 2013
Thats what I thought but it would then generate 250 interrupts per second which doesn't sound a goood idea
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Mar 14 09:37:02 MST 2013

Quote: IanJames
One other point is can the MRn register be written to at any time or is it necessary to wait until the timer matches?



Could be useful to enable MR0 or MR3 interrupt and change MR0 there :)
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Thu Mar 14 09:24:03 MST 2013
I have finally found the source of the problem.

The pwm was working fine it was the output stage prior to the leds that was wrong. The board has 'stubber' capacitors on each output which was preventing the pwm signal from going completely off - so it was hardware not software :)

One other point is can the MRn register be written to at any time or is it necessary to wait until the timer matches?

I ask because I have wriiten some code to decrement MR0 every mSec and what I notice is that sometimes there is 'blip' on the output led - not every time though.

Thanks for your all your help and advice
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Thu Mar 14 02:58:55 MST 2013
I change the width value by altering the LPC_CT32B0->MR0

The diferences  between your sample code and mine are:

1. setting of prescaler - I have not bothered with this as it did not seem important

2. I use PIO0_18

I am now in the office and will be hooking a scope up to it.
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Thu Mar 14 00:02:16 MST 2013
And how do you invoke the routine changing the width value? This is probably the key to your problem.
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Mar 13 14:18:04 MST 2013
There's a clock left at home :confused:

Let's use 32-bit power :eek:

This sample is a working LPC11xx sample, probably no big surprise. It's generating (from 48MHz) a nice little PWM with

-100 s cycle, 25 s off

//PWM32 init
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);    //enable timer0 clock
  LPC_IOCON->PIO1_6    = (2<<0);             //PIO1_6 is MAT0 output
  LPC_TMR32B0->PR    = 48E6 -1;            //prescaler 1 s from 48E6 clock
  LPC_TMR32B0->MR3   = 100-1;            //MR3 cycle
  LPC_TMR32B0->MR0   = 25;                //MR0 low time
  LPC_TMR32B0->MCR   = (1<<10);            //MR3 reset
  LPC_TMR32B0->PWMC  = (1<<0);           //PWM0 output
  LPC_TMR32B0->TCR   = 1;                //run timer
That's scopeable with a clock. And if your fast clock is in the office, use 32-bit range and increase your MR0/MR3 to 3600/86399 and use a sundial
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Wed Mar 13 11:07:51 MST 2013
I have one in the office - I am working from home today
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Mar 13 11:04:21 MST 2013

Quote: IanJames
I don't have a scope...

I have over 20 years of pwm/microcontroller experience...



:confused:

And that's confusing me :eek:

Quote: IanJames
Any help advice would be greatly appreciated.



Buy a PicoScope 220X today ;)

http://www.picotech.com/picoscope2000.html
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Wed Mar 13 11:01:29 MST 2013
Fair point here goes:

void InitPWM(void)
{
init_timer32PWM(50000);
}

void init_timer32PWM(uint32_t period)
{
LPC_CT32B0->TCR=0;

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);

/* Setup the external match register */
LPC_CT32B0->EMR = (1<<EMC3) |(1<<EMC0);

/* Setup the outputs */
LPC_IOCON->PIO0_18 &= ~0x07; LPC_IOCON->PIO0_18 |= 0x04; /* Timer0_32 MAT0 */

/* Enable the selected PWM */
LPC_CT32B0->PWMC = 0x00000001;

LPC_CT32B0->MCR = (1<<10); /* Reset on MR3 */

/* Setup the match registers */
LPC_CT32B0->MR0 = period;
LPC_CT32B0->MR3 = period;

        LPC_CT32B0->TCR=1;
return;
}


void setPWM (uint32_t value)
{
LPC_CT32B0->MR0 = value;
}
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Mar 13 10:48:31 MST 2013
It's hard to tell anything without seeing the real, full code you use to initialize the timer and change its period/pulse width.
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanJames on Wed Mar 13 10:45:14 MST 2013
Hi,

I have a PWM driven LED light attached to the output.

I don't have a scope but I can see the current consumption rise from 40mA to 226mA when fully on (as well as the led light getting brighter)

The current consumption and led only start to increase when the value loaded in to LPC_CT32B0->MR0 is approx 28500.

Up until now I have always used ATMEL processors for our projects (Zigbee controlled Led lighting) but the LPC122x series seems attractive.

I have over 20 years of pwm/microcontroller experience but this one is confusing me!

Any help advice would be greatly appreciated.

Thanks

Ian James
0 Kudos
Reply

2,775 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Mar 13 09:56:07 MST 2013

Quote: IanJames
The problem is that the pwm output is off until it gets to approx 29500...


 
:confused:

#1 How do you get this value in a running PWM?

#2 Is you scope confirming a 59/41 PWM ?
0 Kudos
Reply