Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
12 years, 4 months ago.
problem on pwm interrupt
I am using LPC 1768 board....I wrote a program for creating interrupt when counter matches with match 0 register.....but its not entering into interrupt program....led3 is not toggling...please help...attached c prog /media/uploads/amalsebastian1992/prog3.txt
2 Answers
12 years, 4 months ago.
(You can still put it between <<code>> and <</code>> here, thats easier for everyone).
Anyway, looked at it, and three things:
First of all, I assume the wait(0.2) in your IRQ handler is just for debugging purposes, otherwise it is really bad ;).
Next, one of those small irritating errors (I actually copied your code to blink a led in the main loop, and it didn't do anything even after I deleted everything else in the program, only then I realised it):
chB != chB; //This only checks if chB is unequal to chB, which is false, and then it doesn't do anything else. chB = !chB; //This one makes chB the inverse of its current value, and is the one you want
Finally the main problem, I expected since you used 'PWM1_IRQHandler', that that is the default PWM1_IRQHandler location, apparently it isn't. The default way I use the hardware IRQs fixes it though, before you enable the IRQ, add the following line:
NVIC_SetVector(PWM1_IRQn, (uint32_t)&PWM1_IRQHandler);
With that you can redirect the IRQ vector to any function you want.
Then the code works for me, just to be sure, here it is:
#include "mbed.h"
void PWMInit();
PwmOut chA(p23);
DigitalOut chB(LED3);
int main (void)
{
PWMInit();
while(1) {
LPC_PWM1->MR3 = 6000;
LPC_PWM1->MR4 = 15000;
LPC_PWM1->LER |= (1<<3) | (1<<4) ;
}
}
void PWM1_IRQHandler (void)
{
if((LPC_PWM1->IR & 0x01) == 0x01) // Check whether it is an interrupt from match 0
{
chB = !chB;
wait(.2);
LPC_PWM1->IR |= 1<<0; // Clear the interrupt flag
}
return;
}
void PWMInit()
{
LPC_SC->PCONP |= 1 << 6; // enable power for PWM1
LPC_SC->PCLKSEL0 |= 1 << 12; // PCLK_PWM1 = CCLK
LPC_PWM1->MR0 = 16000; // PWM freq = CCLK/16000
LPC_PWM1->MCR |= 1 << 0; // interrupt on Match0
LPC_PINCON->PINSEL3 |= (2 << 14 ); // P2.4 works as PWM1 output
LPC_PINCON->PINMODE3 |= 2 << 14; // enable neither pull up nor pull down
LPC_PWM1->PCR |= 1 << 4; // Configure channel 4 as double edge controlled PWM
LPC_PWM1->PCR |= 1 << 12; // Enable PWM channel 4 output
LPC_PWM1->MR0 = 16000; // PWM freq = CCLK/16000
LPC_PWM1->TCR = (1 << 0) | (1 << 3); // enable timer counter and PWM
NVIC_SetVector(PWM1_IRQn, (uint32_t)&PWM1_IRQHandler);
NVIC_EnableIRQ(PWM1_IRQn);
return;
}
Erik, do you have an example on how to do this with the LPC11U24?
posted by Very Compact 16 Feb 2015