PWM question on LPC12xx

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

PWM question on LPC12xx

457 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gclaudiu on Fri Sep 20 10:30:16 MST 2013
Hi there,

I am working on a small project and I need to control 2 motors using PWM. My question is if I can use 4 PWM outputs on the same timer (CT32B1)?

I am using PIO0_6 (MAT0 )and PIO0_7 (MAT1) for 1 motor and PIO0_8 (MAT2) and PIO0_9 (MAT3) for another motor.  My initialisation code looks like:

        // Power on 32-bit timers
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);

        LPC_IOCON->PIO0_6            &= ~0x07;
LPC_IOCON->PIO0_6            |=  0x04;            //C32B1_MAT0

LPC_IOCON->PIO0_7            &= ~0x07;
LPC_IOCON->PIO0_7            |=  0x04;            //C32B1_MAT1

LPC_IOCON->PIO0_8            &= ~0x07;
LPC_IOCON->PIO0_8            |=  0x04;            //C32B1_MAT2

LPC_IOCON->PIO0_9            &= ~0x07;
LPC_IOCON->PIO0_9            |=  0x04;            //C32B1_MAT3

/// Setup the external match register
LPC_CT32B0->EMR = (1<<EMC0) | MATCH0 | (1<<EMC1) | MATCH1 | (1<<EMC2) | MATCH2 | (1<<EMC3) | MATCH3;


/// Set prescaler
LPC_CT32B0->PR  = ((SystemCoreClock/PWM_FREQUENCY)/1024)-1;

/* Enable the selected PWMs */
LPC_CT32B0->PWMC = MATCH3 | MATCH0 | MATCH1 | MATCH2;

/// Set PWM period on MATCH3
LPC_CT32B0->MR3 = MAXSPEED;

LPC_CT32B0->MR0 = 0x00;

/// Set match control register
LPC_CT32B0->MCR = 1<<10;

/// Reset pwm
LPC_CT32B0->TCR = 0x02;

/// Enable pwm
LPC_CT32B0->TCR = 0x01;


Code to control 1 motor is working fine:

if(speed < 0){
LPC_CT32B0->MR0 = -speed;

LPC_CT32B0->MR1 = 0;              // can be either "1" or "0"
} else {
LPC_CT32B0->MR1 = speed;

LPC_CT32B0->MR0 = 0;              // can be either "1" or "0"
}


I can't use the same code on the second motor because I do not know how to control the MAT3 output. I thought of using MCR and and use MR2 to reset TC and free up the MAT3 when changing the direction as MAT2 can be either "1" or "0" when I have PWM on MAT3.

May be a silly question, but I did try to look for a solution and I am stuck. Unfortunately I can not change the board to use other timers.

Any suggestion will really be appreciated!

Thankks,
Claudiu
Labels (1)
0 Kudos
1 Reply

388 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gclaudiu on Sat Sep 21 19:10:27 MST 2013
Ok, the solution that works is pretty simple:

if(speed < 0){
LPC_CT32B0->MCR = 1<<10;                     // use MR3 to reset TC

LPC_CT32B0->MR2 = MAXSPEED + speed;

LPC_CT32B0->MR3 = MAXSPEED;
} else {
LPC_CT32B0->MCR = 1<<7;                      // use MR2 to reset TC

LPC_CT32B0->MR3 = MAXSPEED - speed;

LPC_CT32B0->MR2 = MAXSPEED;
}


Is there any other option like using all 4 pins for PWM? Any suggestion?
0 Kudos