Continue to Site

# Help to measure pulse width

Status
Not open for further replies.

#### pete

##### Member level 2
pic measure pulse width

Hi! Can anybody help how to measure a pulse width in CCSC? Signal normally high and I want to meaure the duration of a low signal before it goes high again(i.e. a negative pulse in the millisecond range)
Please show two methods: 1. Using interrupt 2. Without using an interrupt.

Thanks

measure pulse width

Hi,
OK first method :

withut int
while(1)
{
while(input(PIN_B0));
set_timer1(0);
while(!input(PIN_B0));
puls=get_timer1();
printf("%lu",puls);
}

you must setup timer_1 depend of your pulse width. This is very simple method, can use with all timer, if we talk specially for timer 1 you can use start/stap bit to enable disable timer, and then only read and clear after reading.

whit intterupt

ext interrupt subroutine
#int_ext
ext_IRS()
{
if(!start_mes)
{
start_mes=true;
set_timer1(0);
ext_int_edge( L_TO_H );
}
else
{
puls=get_timer1();
start_mes=false;
mes_compl=true;
ext_int_edge( H_TO_L );
}
}

main routine
ext_int_edge( H_TO_L );
while(1)
{
if(mes_compl)
{
mes_compl=false;
printf("%lu",puls);
}
}

this is one of the possible way, using ext int pin, you must enable this interrupt. When start the soft, your interrupt will be High to Low, when you receive the interrupt, soft clear the timer 1 and after this configurate ext int pin to generate interrupt on Low to Hig. when impulse going again to 1, you read the timer and set measurement complete bit to 1.
Yu can use port B change interrupt, then no neet to preconfigurate the interrupt edge becouse port B4-B7 generate enterrupt on both edge.

This is the idea, you can use CCP module for this measurement too.

ext_int_edge(l_to_h)

just look this examples in ccs:

EX_CCPMP.C
Uses the PIC CCP module to measure a pulse width

EX_PULSE.C
Measures a pulse width using timer0

The first, interrupt method, second polling method

Status
Not open for further replies.