View previous topic :: View next topic |
Author |
Message |
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
detecting rising edge |
Posted: Sun Sep 13, 2009 12:44 am |
|
|
Hi,
I'm trying to do the follows. When the input of a certain pin (say PIN X) goes high, I would like to start the PIC timer till the input of another pin (say PIN Y) goes high.
Because the time is critical in my application, I would like to start the timer when the PIC encounters the first rising edge of the signal of PIN X and stop when it detects the first rising edge of PIN Y.
I know how to use the input(pinX) command, but is there a way I can detect the rising edge? I need the timing to be as accurate as possible because I am trying to measure the time difference of arrival between a RF signal and a ultrasonic one. From what I have read, the best way would be to create an interrupt and start the timer then.
What commands should I be looking into? Also, I what is the most accurate way I can set the timer so it detects the TDOA of two very fast signals?
Any help would be great...thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 13, 2009 2:29 am |
|
|
See these example programs:
Quote: |
c:\program files\picc\examples\ex_pulse.c
c:\program files\picc\examples\ex_ccpmp.c
|
|
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 13, 2009 2:32 am |
|
|
CCP.
This is the best way to do what you want.
This is a hardware module in most PICs (you will need to choose one that has it), with a whole range of abilities to do with timing, and pulse generation. One ability, is to capture the reading on an internal counter (normally Timer1, but some of the PIC18's also allow Timer3 to be used), when an 'event' occurs. The events can be programmed to be the rising edge, or falling edge, on the CCP pins.
Accuracy will far exceed any software solution.
Look in the examples, at EX_CCPMP.C
In this, the two CCP modules are used. One to record when a signal rises, and the other when it falls. Each time a CCP records a value, there is an interrupt, and the code uses the second interrupt, on the falling edge, then subtracts the rising edge count, from the falling edge count, to give the pulse width. The same signal is fed to both CCP pins.
In your case, you simply connect your two signals to the two pins, and program the second CCP, to also use the rising edges (in the example, it is programmed to use the falling edge).
Best Wishes |
|
|
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Sun Sep 13, 2009 4:45 am |
|
|
Ttelmah wrote: | CCP.
This is the best way to do what you want.
This is a hardware module in most PICs (you will need to choose one that has it), with a whole range of abilities to do with timing, and pulse generation. One ability, is to capture the reading on an internal counter (normally Timer1, but some of the PIC18's also allow Timer3 to be used), when an 'event' occurs. The events can be programmed to be the rising edge, or falling edge, on the CCP pins.
Accuracy will far exceed any software solution.
Look in the examples, at EX_CCPMP.C
In this, the two CCP modules are used. One to record when a signal rises, and the other when it falls. Each time a CCP records a value, there is an interrupt, and the code uses the second interrupt, on the falling edge, then subtracts the rising edge count, from the falling edge count, to give the pulse width. The same signal is fed to both CCP pins.
In your case, you simply connect your two signals to the two pins, and program the second CCP, to also use the rising edges (in the example, it is programmed to use the falling edge).
Best Wishes |
Hey, thanks!
Ill have a crack at it and keep you posted. |
|
|
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Sun Sep 13, 2009 5:16 am |
|
|
hey,
I read the program, and yes, it makes sense. Thanks for the heads up. Just a quick question. My pic, 16f690, has only one ccp pin. What then?
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Sun Sep 13, 2009 10:51 am |
|
|
hey pcm, thanks for the link. Are you suggesting i feed both signals into the ccp pin and work out the pulse width? I didnt think of that. Will the ccp timer be fast enough to capture the tdoa of a rf and us signal? What if the two signals comes right after one another such that that there is no time for the signal to go low and high again? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 13, 2009 2:53 pm |
|
|
Post the time interval that you expect to be measuring.
Also, post the source of the signal(s). What device is producing them ? |
|
|
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Sun Sep 13, 2009 9:56 pm |
|
|
PCM programmer wrote: | Post the time interval that you expect to be measuring.
Also, post the source of the signal(s). What device is producing them ? |
There is going to be 2 devices producing each signal. So one is the HT12D chip, which gives a HIGH when it receives a valid transmittion (VT) and the other is my ultrasonic receiver, which goes through a comparator which in turn goes HIGH when a certain threshold is passed. So one HIGH for arrival of RF and another HIGH for arrival of US.
In terms of interval, I would say extremely small. My intention is to send a RF signal and a US signal at the same time from across the room and for the receiver to pick up the TDOA of the 2 signals. So as you can imagine, probably microseconds, maybe even smaller. This is just an estimate. Hope this answers your questions.
Thanks heaps. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 13, 2009 10:12 pm |
|
|
If you want to capture a small interval, such as less than a microsecond,
then you should use a PIC with two CCPs with a 20 MHz crystal. |
|
|
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Mon Sep 14, 2009 8:05 am |
|
|
PCM programmer wrote: | If you want to capture a small interval, such as less than a microsecond,
then you should use a PIC with two CCPs with a 20 MHz crystal. |
Hey PCM, thanks for that. Unfortunately, I have no choice but to use the 16F690 for the time being.
I have also done some research and it seems that the intervals that I should be expecting would be in the milliseconds. Given that the pic16f690 has an internal clock of 20Mhz, do you think if I can connect one input to the ccp and the other to any other input, it should capture microsecond level intervals pretty easy? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 14, 2009 12:57 pm |
|
|
Quote: |
There is going to be 2 devices producing each signal. So one is the
HT12D chip, which gives a HIGH when it receives a valid transmittion
(VT) and the other is my ultrasonic receiver, which goes through a
comparator which in turn goes HIGH when a certain threshold is passed.
So one HIGH for arrival of RF and another HIGH for arrival of US.
|
You have two separate devices, each producing a signal. You only have
one CCP input pin on the 16F690. How do you propose to connect these
two separate signals to the one pin ? You haven't explained that. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Sep 14, 2009 2:20 pm |
|
|
Are you trying to determine range by comparing time of flight of a radio signal and an ultrasonic signal? If so can you consider the radio signal as instantaneous and just measure the time of the ultrasonic with one counter? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 14, 2009 2:31 pm |
|
|
One obvious suggestion. Use a set/reset latch.
Connect the first signal to the 'set' pin, and the second the the 'reset' pin. Then feed the output to the single CCP. The pulse width is what you want.
Best Wishes |
|
|
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Tue Sep 15, 2009 9:56 am |
|
|
SherpaDoug wrote: | Are you trying to determine range by comparing time of flight of a radio signal and an ultrasonic signal? If so can you consider the radio signal as instantaneous and just measure the time of the ultrasonic with one counter? |
Yes I am trying to find range...
But I need the arrival of the RF signal to determine when to start the timer. My RX and TX are two separate units, so if I just send a US pulse out from one side, I cant tell how long it took to get to the other side without the RF as some kind of reference. |
|
|
|