PWM question on LPC12xx

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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_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 */

/// Set PWM period on MATCH3

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!