MCPWM in LPC17xx

MCPWM in LPC17xx Motor Control PWM can be used to control AC and DC motors. It can also be used for many other purposes which require timing, counting, comparing or capturing. The MCPWM is used for generating audio signals in the 2d game console as the general PWM is used for color generation. And I will be discussing as to how it works and how to initialize it.

There are 3 MCPWM channels in LPC1768. Each channel contains its own 32-bit timer, 32-bit Limit register, 32-bit Match register, 10-bit deadtime register and 32-bit capture register. And there will be two output pins for each channel named A and B. The A and B outputs will be complementary to each other. Here the Limit register sets the TOP value of the timer. Hence the timer counts from 0 to the Limit value. So one can set the PWM frequency by choosing a correct prescaler (clock frequency divider) and the limit value.

There are two modes, one is edge aligned and the other is center aligned mode. In edge aligned mode, the timer will count upwards from 0 to limit and goes back to 0 and the output toggles when the timer value matches with the match register or the limit register. In center aligned mode, the timer will count upwards from 0 to limit and then counts downward from limit to 0 and the output toggles only when the timer value matches the match register value. If you are looking for a phase correct mode, then you should use center aligned mode.

You can even set the passive polarity of the output. If you select passive polarity state as LOW, then the MCPWM output signal will be LOW until the timer value matches the match register value and will be HIGH from the time when match occurs till the timer value matches the limit register value. If the polarity state is selected as HIGH, then the reverse will happen.

/media/uploads/mbed2f/img1.png

Figure 1: Waveform indicating the output of MCPWM channel 0 with polarity as 0 in edge aligned mode. (Source: NXP user manual for LPC1768)

/media/uploads/mbed2f/img2.png

Figure 2: Waveform indicating the output of MCPWM channel 0 with polarity as 0 in center aligned mode. (Source: NXP user manual for LPC1768)

For 2-channel audio generation purpose, MCPWM channels 1 and 2 were used. Follow the code below to initialize and use the MCPWM.

          // Power up MCPWM
    LPC_SC->PCONP |= 1<<17;


    // PCLK = CCLK (100MHz in our case)
    LPC_SC->PCLKSEL1 |= 1<<30;


          // Configuring P1.25 and P1.28 as MC1A and MC2A
    LPC_PINCON->PINSEL3 |= (1<<18) | (1<<24); 

          // Set the Limit register (MCLIM0-2 is named as MCPER0-2 in CMSIS source file)
    LPC_MCPWM->MCPER1 = 255;
    LPC_MCPWM->MCPER2 = 255;

          //  Start MCPWM channels 1 and 2 and POLA = 1
    LPC_MCPWM->MCCON_CLR = 0xffffffff;
    LPC_MCPWM->MCCON_SET = (1<<10) | (1<<18) | (1<<16) | (1<<8); 

Here the reason I am using POLA = 1 is that I want average value of the output signal to be my audio signal. I am using 8-bit audio and the audio sample value 0 should give less amplitude than the value 255. So the PWM signal should stay HIGH only till it matches with the match value (audio sample) and then should become LOW.

Now that the configuration is over, the only thing that is remaining is to get audio out of MC1A and MC2A. So we need to update the MCMAT1 and MCMAT2 at the PCM rate of the audio signal that needs to be played. For example we want to play a 16kHz 8-bit sample width audio (WAV file). So we need to update MCMAT1,2 once in (1/16kHz) = 62.5uS. Lets setup a timer to give an interrupt once in 62.5uS and update the match values in that interrupt.

          // Power up Timer1
    LPC_SC->PCONP |= 1<<2;

          // PCLK = CCLK (In my case it is 100MHz)  
    LPC_SC->PCLKSEL0 |= 1<<4;

          // Select 16kHz as timer frequency
          // (100MHz / 16kHz) = 6250. Therefore match value should be 6249 as the timer starts counting from 0.
    LPC_TIM1->MR0 = 6249;

          // Reset and interrupt on Match 0
    LPC_TIM1->MCR |= (1<<0) | (1<<1);
    
    // Enable Timer1 interrupt  
    NVIC_EnableIRQ(TIMER1_IRQn);

          // Reset and Start the timer
    LPC_TIM1->TCR = 2;
    LPC_TIM1->TCR = 1;

Now the interrupt routine looks like this

void TIMER1_IRQHandler()
{
    // Update the Match registers of MCPWM 
    // MCMAT0-2 is named as MCPW0-2 in CMSIS source file
    LPC_MCPWM->MCPW1 = <sample of audio channel 1>;
    LPC_MCPWM->MCPW2 = <sample of audio channel 2>; 
}

After doing this you can enjoy listening to music from LPC. You can very easily make an audio player with an SD card. See this http://gvworks.blogspot.com/2011/05/wav-player-with-lpc1768-and-sd-card.html for more information as to how to use an SD card with LPC1768 with PWM output. If anyone needs more help with MCPWM then post your queries in comments. I will try my best to find you the answer.

thankyou Thejasvi http://msys-mv.blogspot.com/


2 comments on MCPWM in LPC17xx:

18 Apr 2013

Hello,

I would like to use the MCPWM as a normal PWM output, because the mbed pwm is already in use. Can you help me what I should configure to control p1.19.

I tried already this, but on the scope I get a DC value 0f 2.36V: Power up MCPWM LPC_SC->PCONP |= 1<<17;

PCLK = CCLK (100MHz in our case) LPC_SC->PCLKSEL0 |= 1<<30;

Configuring P1.19 = 33 as MC0A LPC_PINCON->PINSEL3 |= (1<<6);

Set the Limit register (MCLIM0-2 is named as MCPER0-2 in CMSIS source file) LPC_MCPWM->MCPER0 = 5555;

Start MCPWM channels 0 LPC_MCPWM->MCCON_CLR = 0xffffffff; LPC_MCPWM->MCCON_SET = (1<<0);

LPC_MCPWM->MCPW0 = 2777;

My intension is to get a frequency of 180 Hz and a duty cycle of 50%.

Thanks in advance.

05 Feb 2017

And dead_time insertion ??

Please log in to post comments.