CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Measuring a Pulse with 1us resolution

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Acetoel
Guest







Measuring a Pulse with 1us resolution
PostPosted: Sat Mar 22, 2003 2:46 pm     Reply with quote

Hello,
I new to measure a very short pulse (from 80uSec to 1000uSec). The measurement has to start with a High-To-Low trasnsition, here the program must measure the Low state and stops to measure when a Low-To-High Transition occurs.
Is this possible with this resolution? I can implenet something similar with Pic*Basic*Pro, but would like to do it with C.
Thanks
Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12961
Shane Rowell
Guest







Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 1:31 am     Reply with quote

I am not sure what device you are using, but here is what is possible.

If you have a device with CCP1 and CCP2 you can tie the two pins together and set one for falling edge and the other for rising edge. Knowing the polarity of your signal you can then determine the pulse width. You should be able to achieve .2uS resolution.

Shane

:=Hello,
:= I new to measure a very short pulse (from 80uSec to 1000uSec). The measurement has to start with a High-To-Low trasnsition, here the program must measure the Low state and stops to measure when a Low-To-High Transition occurs.
:= Is this possible with this resolution? I can implenet something similar with Pic*Basic*Pro, but would like to do it with C.
:=Thanks
:=Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12967
Acetoel
Guest







Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 5:21 am     Reply with quote

Hi,
I'm using a 16F76 at 16MHz. Do you have a piece of code?
Thanks
Eze

:=I am not sure what device you are using, but here is what is possible.
:=
:=If you have a device with CCP1 and CCP2 you can tie the two pins together and set one for falling edge and the other for rising edge. Knowing the polarity of your signal you can then determine the pulse width. You should be able to achieve .2uS resolution.
:=
:=Shane
:=
:=:=Hello,
:=:= I new to measure a very short pulse (from 80uSec to 1000uSec). The measurement has to start with a High-To-Low trasnsition, here the program must measure the Low state and stops to measure when a Low-To-High Transition occurs.
:=:= Is this possible with this resolution? I can implenet something similar with Pic*Basic*Pro, but would like to do it with C.
:=:=Thanks
:=:=Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12970
Humberto



Joined: 08 Sep 2003
Posts: 1183
Location: Buenos Aires la Reina del Plata

View user's profile Send private message

Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 2:52 pm     Reply with quote

:=Hi,
:= I'm using a 16F76 at 16MHz. Do you have a piece of code?
:=Thanks


Hi:
Try starting with this

#include <16F876.H>
#device *=16

#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
#zero_ram

long int counter;
int end_counter_ready, enable_counter;


void main()
{
printf("\r\nRunning...");
enable_interrupts(INT_EXT);
ext_int_edge(H_to_L);
enable_interrupts(GLOBAL);

while(TRUE)
{
if(end_counter_ready)
{
printf("\r\nCounter \%lu ", counter);
end_counter_ready = FALSE;
delay_ms(200);
enable_interrupts(GLOBAL);
}

while(enable_counter)
{
counter++;
}

enable_counter = FALSE;
enable_interrupts(INT_EXT);
}
}


#int_EXT
edge_detect_isr()
{
if(!enable_counter)
{
enable_counter = TRUE;
ext_int_edge(L_to_H);
counter = 0;
}
else
{
enable_counter = FALSE;
end_counter_ready = TRUE;
ext_int_edge(H_to_L);
disable_interrupts(INT_EXT);
}
}

Hope this help

Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 12979
_________________
Humberto
Acetoel
Guest







Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 6:57 pm     Reply with quote

Hi Humberto,
The High to Low Transition, in which pins has to occur. I think in the CCP1 pin, but I cannot see a reference to CCP1 in your code.
Thanks
Ezequiel

:=:=Hi,
:=:= I'm using a 16F76 at 16MHz. Do you have a piece of code?
:=:=Thanks
:=
:=
:=Hi:
:=Try starting with this
:=
:=#include <16F876.H>
:=#device *=16
:=
:=#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
:=#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
:=#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
:=#zero_ram
:=
:=long int counter;
:=int end_counter_ready, enable_counter;
:=
:=
:=void main()
:={
:= printf("\r\nRunning...");
:= enable_interrupts(INT_EXT);
:= ext_int_edge(H_to_L);
:= enable_interrupts(GLOBAL);
:=
:= while(TRUE)
:= {
:= if(end_counter_ready)
:= {
:= printf("\r\nCounter \%lu ", counter);
:= end_counter_ready = FALSE;
:= delay_ms(200);
:= enable_interrupts(GLOBAL);
:= }
:=
:= while(enable_counter)
:= {
:= counter++;
:= }
:=
:= enable_counter = FALSE;
:= enable_interrupts(INT_EXT);
:= }
:=}
:=
:=
:=#int_EXT
:=edge_detect_isr()
:={
:= if(!enable_counter)
:= {
:= enable_counter = TRUE;
:= ext_int_edge(L_to_H);
:= counter = 0;
:= }
:= else
:= {
:= enable_counter = FALSE;
:= end_counter_ready = TRUE;
:= ext_int_edge(H_to_L);
:= disable_interrupts(INT_EXT);
:= }
:=}
:=
:=Hope this help
:=
:=Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 12987
Humberto



Joined: 08 Sep 2003
Posts: 1183
Location: Buenos Aires la Reina del Plata

View user's profile Send private message

Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 7:36 pm     Reply with quote

:=Hi Humberto,
:= The High to Low Transition, in which pins has to occur. I think in the CCP1 pin, but I cannot see a reference to CCP1 in your code.
:=Thanks
:=Ezequiel

Ezequiel:

Nop, I donīt use CCP1, instead I use external -edge selected- interrupt. I assume that to implement that you must use PIN_B0
to activate the interrupt hardware in 16F87x.This works fine.

Humberto


:=:=:=Hi,
:=:=:= I'm using a 16F76 at 16MHz. Do you have a piece of code?
:=:=:=Thanks
:=:=
:=:=
:=:=Hi:
:=:=Try starting with this
:=:=
:=:=#include <16F876.H>
:=:=#device *=16
:=:=
:=:=#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
:=:=#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
:=:=#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
:=:=#zero_ram
:=:=
:=:=long int counter;
:=:=int end_counter_ready, enable_counter;
:=:=
:=:=
:=:=void main()
:=:={
:=:= printf("\r\nRunning...");
:=:= enable_interrupts(INT_EXT);
:=:= ext_int_edge(H_to_L);
:=:= enable_interrupts(GLOBAL);
:=:=
:=:= while(TRUE)
:=:= {
:=:= if(end_counter_ready)
:=:= {
:=:= printf("\r\nCounter \%lu ", counter);
:=:= end_counter_ready = FALSE;
:=:= delay_ms(200);
:=:= enable_interrupts(GLOBAL);
:=:= }
:=:=
:=:= while(enable_counter)
:=:= {
:=:= counter++;
:=:= }
:=:=
:=:= enable_counter = FALSE;
:=:= enable_interrupts(INT_EXT);
:=:= }
:=:=}
:=:=
:=:=
:=:=#int_EXT
:=:=edge_detect_isr()
:=:={
:=:= if(!enable_counter)
:=:= {
:=:= enable_counter = TRUE;
:=:= ext_int_edge(L_to_H);
:=:= counter = 0;
:=:= }
:=:= else
:=:= {
:=:= enable_counter = FALSE;
:=:= end_counter_ready = TRUE;
:=:= ext_int_edge(H_to_L);
:=:= disable_interrupts(INT_EXT);
:=:= }
:=:=}
:=:=
:=:=Hope this help
:=:=
:=:=Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 12988
_________________
Humberto
Humberto



Joined: 08 Sep 2003
Posts: 1183
Location: Buenos Aires la Reina del Plata

View user's profile Send private message

Re: Measuring a Pulse with 1us resolution
PostPosted: Sun Mar 23, 2003 8:14 pm     Reply with quote

:=:=Hi Humberto,
:=:= The High to Low Transition, in which pins has to occur. I think in the CCP1 pin, but I cannot see a reference to CCP1 in your code.
:=:=Thanks
:=:=Ezequiel
:=
:=Ezequiel:


Nop, I donīt use CCP1, instead I use external -edge selected- interrupt. I assume that to implement that you must use PIN_B0
to activate the interrupt hardware in 16F87x.This works fine.


#include <16F873.H>
#device *=16

#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
#zero_ram

long int counter;
int end_counter_ready, enable_counter;


void main()
{
printf("\r\nRunning...");
enable_interrupts(INT_EXT);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
ext_int_edge(H_to_L);
enable_interrupts(GLOBAL);

while(TRUE)
{
if(end_counter_ready)
{
end_counter_ready = FALSE;
printf("\r\nCounter \%lu ", counter);
delay_ms(500); // choose your update time interval
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
}

do
{

}while(enable_counter);
}
}


#int_EXT
edge_detect_isr()
{
if(!enable_counter) // first falling edge
{
enable_counter = TRUE;
enable_interrupts(INT_TIMER1);
set_timer1(0);
counter = 0;
}
else
{ // second falling edge
disable_interrupts(INT_TIMER1);
counter = get_timer1();
disable_interrupts(INT_EXT);
enable_counter = FALSE;
end_counter_ready = TRUE;
}
}


Hope this help

Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 12989
_________________
Humberto
Acetoel
Guest







Re: Measuring a Pulse with 1us resolution
PostPosted: Mon Mar 24, 2003 3:50 am     Reply with quote

Hi Humberto,
Thanks very much for your help. My code starts with no interrupt enable, I need only to measure 1 (ONE) High to Low Transition. I have PB.0 connected to PC.7 (HUART RX)and a pull up resistor. So I need to measure 1 start bit pulse, and with that measurement I can determine the baud rate. Can I use PC.7 (In I/O Mode) directly?

Thanks
Ezequiel

:=:=:=Hi Humberto,
:=:=:= The High to Low Transition, in which pins has to occur. I think in the CCP1 pin, but I cannot see a reference to CCP1 in your code.
:=:=:=Thanks
:=:=:=Ezequiel
:=:=
:=:=Ezequiel:
:=
:=
:=Nop, I donīt use CCP1, instead I use external -edge selected- interrupt. I assume that to implement that you must use PIN_B0
:=to activate the interrupt hardware in 16F87x.This works fine.
:=
:=
:=#include <16F873.H>
:=#device *=16
:=
:=#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
:=#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
:=#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
:=#zero_ram
:=
:=long int counter;
:=int end_counter_ready, enable_counter;
:=
:=
:=void main()
:={
:= printf("\r\nRunning...");
:= enable_interrupts(INT_EXT);
:= setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
:= ext_int_edge(H_to_L);
:= enable_interrupts(GLOBAL);
:=
:= while(TRUE)
:= {
:= if(end_counter_ready)
:= {
:= end_counter_ready = FALSE;
:= printf("\r\nCounter \%lu ", counter);
:= delay_ms(500); // choose your update time interval
:= enable_interrupts(INT_EXT);
:= enable_interrupts(GLOBAL);
:= }
:=
:= do
:= {
:=
:= }while(enable_counter);
:= }
:=}
:=
:=
:=#int_EXT
:=edge_detect_isr()
:={
:= if(!enable_counter) // first falling edge
:= {
:= enable_counter = TRUE;
:= enable_interrupts(INT_TIMER1);
:= set_timer1(0);
:= counter = 0;
:= }
:= else
:= { // second falling edge
:= disable_interrupts(INT_TIMER1);
:= counter = get_timer1();
:= disable_interrupts(INT_EXT);
:= enable_counter = FALSE;
:= end_counter_ready = TRUE;
:= }
:=}
:=
:=
:=Hope this help
:=
:=Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 12994
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Measuring a Pulse with 1us resolution
PostPosted: Mon Mar 24, 2003 9:21 am     Reply with quote

<font face="Courier New" size=-1>:=Hello,
:= I new to measure a very short pulse (from 80uSec to 1000uSec). The measurement has to start with a High-To-Low trasnsition, here the program must measure the Low state and stops to measure when a Low-To-High Transition occurs.
:= Is this possible with this resolution? I can implenet something similar with Pic*Basic*Pro, but would like to do it with C.
:=Thanks
:=Ezequiel

This is almost the best accuracy you may achieve. Connect an extra pin to the input signal.

While (extra_pin);
set_timer0(0);
While (!extra_pin);
get_timer0();


If the timer overflows increment another variable. You can count all the time you wish to. It only cost you an extra pin.</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13000
Humberto



Joined: 08 Sep 2003
Posts: 1183
Location: Buenos Aires la Reina del Plata

View user's profile Send private message

Re: Measuring a Pulse with 1us resolution
PostPosted: Mon Mar 24, 2003 8:55 pm     Reply with quote

:=Hi Humberto,
:= Thanks very much for your help. My code starts with no interrupt enable, I need only to measure 1 (ONE) High to Low Transition. I have PB.0 connected to PC.7 (HUART RX)and a pull up resistor. So I need to measure 1 start bit pulse, and with that measurement I can determine the baud rate. Can I use PC.7 (In I/O Mode) directly?

Yes, you can use it BUT you need to disable the INT_RDA and enable the INT_EXT interrupt until you get the count pulses you expect to discriminate what the baud rate is, then you need to disable INT_EXT and enable the INT_RDA to enable the hardware UART and get the next char, I think you will lost the first character, you need to work around. The only pin that can trigger the INT_EXT is PB0.

regards,

Humberto



:=
:=Thanks
:=Ezequiel
:=
:=:=:=:=Hi Humberto,
:=:=:=:= The High to Low Transition, in which pins has to occur. I think in the CCP1 pin, but I cannot see a reference to CCP1 in your code.
:=:=:=:=Thanks
:=:=:=:=Ezequiel
:=:=:=
:=:=:=Ezequiel:
:=:=
:=:=
:=:=Nop, I donīt use CCP1, instead I use external -edge selected- interrupt. I assume that to implement that you must use PIN_B0
:=:=to activate the interrupt hardware in 16F87x.This works fine.
:=:=
:=:=
:=:=#include <16F873.H>
:=:=#device *=16
:=:=
:=:=#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
:=:=#use delay(clock=4000000) //using 4Mhz crystal you'll get 1 us increment
:=:=#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ENABLE=PIN_C5, ERRORS)
:=:=#zero_ram
:=:=
:=:=long int counter;
:=:=int end_counter_ready, enable_counter;
:=:=
:=:=
:=:=void main()
:=:={
:=:= printf("\r\nRunning...");
:=:= enable_interrupts(INT_EXT);
:=:= setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
:=:= ext_int_edge(H_to_L);
:=:= enable_interrupts(GLOBAL);
:=:=
:=:= while(TRUE)
:=:= {
:=:= if(end_counter_ready)
:=:= {
:=:= end_counter_ready = FALSE;
:=:= printf("\r\nCounter \%lu ", counter);
:=:= delay_ms(500); // choose your update time interval
:=:= enable_interrupts(INT_EXT);
:=:= enable_interrupts(GLOBAL);
:=:= }
:=:=
:=:= do
:=:= {
:=:=
:=:= }while(enable_counter);
:=:= }
:=:=}
:=:=
:=:=
:=:=#int_EXT
:=:=edge_detect_isr()
:=:={
:=:= if(!enable_counter) // first falling edge
:=:= {
:=:= enable_counter = TRUE;
:=:= enable_interrupts(INT_TIMER1);
:=:= set_timer1(0);
:=:= counter = 0;
:=:= }
:=:= else
:=:= { // second falling edge
:=:= disable_interrupts(INT_TIMER1);
:=:= counter = get_timer1();
:=:= disable_interrupts(INT_EXT);
:=:= enable_counter = FALSE;
:=:= end_counter_ready = TRUE;
:=:= }
:=:=}
:=:=
:=:=
:=:=Hope this help
:=:=
:=:=Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 13036
_________________
Humberto
Barry Gershenfeld
Guest







Re: Measuring a Pulse with 1us resolution
PostPosted: Wed Mar 26, 2003 2:18 pm     Reply with quote

: So I need to measure 1 start bit pulse, and with that measurement I can determine the baud rate.

Be aware that the start bit does not return high if the data bit that follows is also a zero. So you will need to watch those start bit readings for a few characters before deciding what is a usable reading. Or else instruct the host or user to always begin with certain characters.

Barry
___________________________
This message was ported from CCS's old forum
Original Post ID: 13106
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group