Measuring 1usec pulse width with interrupts

10 May 2012

I am measuring a square wave signal (0-5V) with a minimum pulse width of 1usec and minimum duty cycle of 10% low. I would like to measure the sum of the low pulse widths over a fixed time period, such as 1 sec, and output that to serial. I tried to use the method described in this forum post: http://mbed.org/forum/mbed/topic/2326/ but it's not working for me. I'm new to embedded programming so just learning about interrupts and ISRs.

Thanks, Anna

/media/uploads/miriya52/mbed_code.pdf

10 May 2012

This link doesn't achieve what you require, but may help you solve the issue. http://mbed.org/users/d_worrall/programs/pulseWidth/lu75i2

10 May 2012

Thanks for the reference. That uses interruptIn, which I have already determined to be too slow.

I've made some updates to the code but still not able to measure the 1us pulse width accurately.

/media/uploads/miriya52/mbed_code.pdf

Anna

11 May 2012

You could use the capture feature of the timers. I have used this to measure time delays of a few nanoseconds of two separate signals at a 1 MHz repetition rate using two inputs thus:

void TIMER0_IRQHandler (void) { clear interrupt flag for capture channel 0 event. if ( LPC_TIM0->IR & (0x1<<4) ) { LPC_TIM0->IR = 0x1<<4; /* clear interrupt flag */ LPC_GPIO0->FIOSET = (1<<CH0); } clear interrupt flag for capture channel 1 event. if ( LPC_TIM0->IR & (0x1<<5) ) { LPC_TIM0->IR = 0x1<<5; /* clear interrupt flag */ LPC_GPIO0->FIOCLR = (1<<CH0); time0 = LPC_TIM0->CR0; time1 = LPC_TIM0->CR1; diff_time = time1 - time0; ...............................

}

In your case there is only one signal, so you could still direct it to two capture inputs, one of which is set to fire on the rising edge, the other on the falling.

JohnR

11 May 2012

According to the handbook Timer only has 1us resolution. This may not be the best way to measure your signal. You may write your own running counter with a faster timebase. Just a thought...

Information

Warning

Note that timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1...

http://mbed.org/handbook/Timer