LPCXpresso (LPC1114 ) Input capture mode for measuring pulse width

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

LPCXpresso (LPC1114 ) Input capture mode for measuring pulse width

697 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by asokfair on Tue Dec 02 03:44:04 MST 2014
Hi Everyone,

I was trying measure the pulse width using lpc1114 in capture mode but it doesn't works.

I am using 16bit Timer0 :

//init 16bit timer0
     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);//enable clock CT16B0
    //init io CT16B0_CAP0
     LPC_IOCON->PIO0_2 = (1<<1)|(2<<3);     //CT16B0_CAP0 & pullupCAP0FE
     LPC_TMR16B0->CTCR = 0;                //use timer mode
     LPC_TMR16B0->CCR  = 5;                //rising
     LPC_TMR16B0->PR  = 47;                //prescaler 48
     NVIC_EnableIRQ(TIMER_16_0_IRQn);         //enable interrupt
     LPC_TMR16B0->TCR  = 1;                  //and run timer

Interrupt routine :

void TIMER16_0_IRQHandler(void)
{

if(LPC_TMR16B0->IR & (1<<4))            //capture interrupt?
{
  LPC_TMR16B0->TC = 0;                    //reset timer
  period_time = LPC_TMR16B0->CR0;        //read time
  LPC_TMR16B0->IR |= (1<<4);            //reset capture interrupt
}

return;
//end capture interrupt
}


I am varying the pulse with (1-100us) in P0_2 pin but the period time not changing. but i need only the pulse width value
please help me to make it work

Note:-(reading pulse from ultra sonic sensor module)

Thanks,
Ashok r
0 Kudos
6 Replies

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Dec 02 23:29:40 MST 2014

Quote: asokfair
ok , if i enable both edges , how to differentiate between falling and raising...



Just read GPIOnDATA...
0 Kudos

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by asokfair on Tue Dec 02 22:21:56 MST 2014
ok , if i enable both edges , how to differentiate between falling and raising interrupt ? should i need to read the pin status?

Thanks,
Ashok r
0 Kudos

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Dec 02 05:42:01 MST 2014
I would recommend to avoid changing CCR in ISR and enabling both edges instead...

Reading Input PIN in ISR is a by far better solution  :)
0 Kudos

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by asokfair on Tue Dec 02 05:31:00 MST 2014
Yes R2D2,

Now i changed that code and made it work for measuring pulse.

Thank you!


Ashok r
0 Kudos

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Dec 02 04:14:55 MST 2014
You've copied Zero's sample:

http://www.lpcware.com/content/forum/lpc1114-my-isr-using-timer16-on-lpcxpresso

which is measuring the frequency by reading the rising edge of the signal  :)


Quote:

LPC_TMR16B0->CCR  = 5;                //rising, interrupt



To measure pulse width you have to set the interrupt for falling edge, too...
0 Kudos

480 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by asokfair on Tue Dec 02 04:09:40 MST 2014
I think i resolved this issue!


void TIMER16_0_IRQHandler(void)
{

if(LPC_TMR16B0->IR & (1<<4))            //capture interrupt?
{
if(LPC_TMR16B0->CCR ==5){
  LPC_TMR16B0->TC = 0;                    //reset timer
  LPC_TMR16B0->CCR =6;
}
else{
  period_time = LPC_TMR16B0->CR0;        //read time
  LPC_TMR16B0->CCR =5;
}
  LPC_TMR16B0->IR |= (1<<4);            //reset capture interrupt
}

return;
//end capture interrupt
}


Thanks,
Ashok r
0 Kudos