View previous topic :: View next topic |
Author |
Message |
twizz0
Joined: 26 Jun 2006 Posts: 3
|
Timing until Pin HIGH |
Posted: Mon Jun 26, 2006 7:51 am |
|
|
Hello. I am very new to PIC programing and am looking for a very simple answer to what is possibly a very simple question. I have searched this forum and am unable to find and answer to my question (well one i understand anyway). All I need is a way to time the gap between me setting a pin High and other pin going high thats it. Please can somone help me. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 8:04 am |
|
|
Several ways of doing it. Picking which way depends on whether or not anything else needs to happen in the mean time. It could be as simple as setting the pin high. Start a timer running. Poll for the other pin to become high. Read the timer value. If you need the processor to do stuff in between. Then use an interrupt. Either and external edge triggered one or one of the CCP pins (if the pic has the ccp module). For this, you would set you pin high. Start a timer. And then wait for the interrupt. There is some latency associated with the interrupt. For the external interrupt, you would have to adjust for this. If using the CCP module, it captures the timer value when the event occurs so you only need to read the CCP value. |
|
|
twizz0
Joined: 26 Jun 2006 Posts: 3
|
|
Posted: Mon Jun 26, 2006 8:08 am |
|
|
No the pic doesnt need to do anything else at the same time so how do i setup a timer an use it (sorry really new to this) |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 8:11 am |
|
|
Read the manual (help file). There are examples under the sections. There are also example files and there is a search option for the forum. Look at setup_timer# in the manual. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Jun 26, 2006 8:11 am |
|
|
EX_CCPMP.C
Uses the PIC CCP module to measure a pulse width |
|
|
twizz0
Joined: 26 Jun 2006 Posts: 3
|
|
Posted: Mon Jun 26, 2006 8:24 am |
|
|
Yeah unfortunetly i dont understand any of them thats why I am asking for help, sorry |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Jun 26, 2006 9:33 am |
|
|
I can't post the code. BUT.. there are 9 lines of code
line
1)print a message
2)setup ccp1 with a rising edge. This count how often tmr1 overflows
3)setup ccp2 with a falling edge. This count how often tmr1 overflows
4)setup tmr1 as a time base. The count values then can be scaled to a time.
5)enable CCP2 interupt remember we are using the falling edge
6)trun on GLOBAL interupts// must turn on both CCP2 and global
7)while loop
8)a delay
9)print result.
We use two counters,
we store the count values at Riseing edge and falling edge.
subract the two counts and the difference is a scaled time of the pulse width. |
|
|
|