View previous topic :: View next topic |
Author |
Message |
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
How to Calculate time between 2 times press a button |
Posted: Tue Nov 25, 2014 8:48 am |
|
|
My code:
Code: |
#include <16f716.h>
#fuses NOWDT,NOPROTECT
#use delay(Clock=4000000)
#Define IN1 PIN_B0
#Define IN2 PIN_B1
#Define OUT1 PIN_A0
#Define OUT2 PIN_A1
VOID main ()
{
WHILE (true)
{
IF (!input (IN1))
{
output_high (OUT1);
output_low (OUT2);
}
IF (!input (IN2))
{
output_high (OUT2);
output_low (OUT1);
}
}
}
|
Now I want calculate time between two events press button (IN1) and (IN2)
Please help me.
Thanks. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Re: How to Calculate time between 2 times press a button |
Posted: Tue Nov 25, 2014 9:14 am |
|
|
Depends on desired resolution and accuracy.
One way.
1) Create background "tick" using one of the built in timers to drive an ISR.
2) In the ISR increment a counter on each tick
3) Capture the counter value as you press each button.
4) Finish with some maths.
5) Job done.
As your code stands you've got no switch debounce so could have problems.
Mike |
|
|
kx9
Joined: 05 Nov 2013 Posts: 3 Location: portland OR
|
|
Posted: Tue Nov 25, 2014 9:50 am |
|
|
My first couple of questions are:
1. How fast do you expect the button presses? Min max
2. What kind of resolution do you need? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 25, 2014 12:35 pm |
|
|
These CCS example files may give you some ideas:
Quote: |
c:\program files\picc\examples\ex_pulse.c
c:\program files\picc\examples\ex_react.c
c:\program files\picc\examples\ex_speed.c |
|
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
Re: How to Calculate time between 2 times press a button |
Posted: Wed Nov 26, 2014 1:03 am |
|
|
Mike Walne wrote: | Depends on desired resolution and accuracy.
One way.
1) Create background "tick" using one of the built in timers to drive an ISR.
2) In the ISR increment a counter on each tick
3) Capture the counter value as you press each button.
4) Finish with some maths.
5) Job done.
As your code stands you've got no switch debounce so could have problems.
Mike |
Thank.
I need specific code.
I'm newbie.
Help me if You can.
Thanks. |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Wed Nov 26, 2014 1:06 am |
|
|
kx9 wrote: | My first couple of questions are:
1. How fast do you expect the button presses? Min max
2. What kind of resolution do you need? |
1. About 0.2s to 0.5s
2. millisecond.
Help me. |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Wed Nov 26, 2014 1:15 am |
|
|
PCM programmer wrote: | These CCS example files may give you some ideas:
Quote: |
c:\program files\picc\examples\ex_pulse.c
c:\program files\picc\examples\ex_react.c
c:\program files\picc\examples\ex_speed.c |
|
I have seen those files.
They too difficult to understand for me. I tried to find out but failed.
I am a beginner, it is necessary to have specific examples.
Thank you for your thoughtful.
Sorry, my english is not good. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Nov 26, 2014 3:17 am |
|
|
I think you need to understand, that this is a help forum for _problems with writing your own code_, not a 'write things for you' forum.
We often do generate code for people, but only once they have started doing it themselves.
Now the CCS examples for things like pulse timing, are as simple as it gets!. The only way to learn, is to start a little at a time. The point is that the PIC has timers, which are just 'counters', which run off a clock you can specify. Once started, they run on their own, whatever your code is doing. So you can time something, by reading the value of one of these, when the first 'event' happens, and then reading it again when the second happens. This is what the pulse example does.
Now step back. the first big problems is the nature of your pulse. If this is a switch signal, then you need to understand the problems of 'bounce'. Except for some very special high speed switches, all switches exhibit 'bounce'. Have a look at:
<http://www.labbookpages.co.uk/electronics/debounce.html>
Do your switches have a debounce circuit?.
Then try something simple. Just have a counter incrementing as your code goes round the loop. Clear this when the first button is seen, and read it when the second occurs. This is going to be a direct measure of the time (in 'loops' rather than mSec). Work out the scale factor from loops to mSec, and you have a working answer. If you want to get a result that won't change as your loop does other things, then try using a timer. The timer will give a figure just like the 'loop counter', but this will be a direct factor of your master oscillator (dependant on the prescaler selected), rather than depending on the code in your loop. MicroChip does a very detailed application note about how the prescalers work, and the timers themselves.
The point is solve one problem at a time. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Nov 26, 2014 6:25 am |
|
|
You've already got a loop.
Follow Ttelmah's advice
Include the following lines in your while(true) loop Code: | delay_ms(1);
loop_count++; | Add code in the other if's to capture the loop_count.
This will get you off the ground.
You SHOULD get ms timings within 10%.
Then refine to improve.
Solve other issues as they arise.
You have to make the effort.
Mike |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Wed Nov 26, 2014 10:00 am |
|
|
Mike Walne wrote: | You've already got a loop.
Follow Ttelmah's advice
Include the following lines in your while(true) loop Code: | delay_ms(1);
loop_count++; | Add code in the other if's to capture the loop_count.
This will get you off the ground.
You SHOULD get ms timings within 10%.
Then refine to improve.
Solve other issues as they arise.
You have to make the effort.
Mike |
Thank you.
This way I know how to do it. However, this loop has a few other things. |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Wed Nov 26, 2014 10:10 am |
|
|
Ttelmah wrote: | I think you need to understand, that this is a help forum for _problems with writing your own code_, not a 'write things for you' forum..... |
Thanks for your help carefully, I enjoyed it.
I will actively research to understand it.
You as a dedicated teacher. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Nov 26, 2014 10:27 am |
|
|
My last paragraph, and Mike's expansion on it, give you a simple place to start. |
|
|
|