View previous topic :: View next topic |
Author |
Message |
SKROTNISSE Guest
|
Find time between two max values. PS. HELP!!!! |
Posted: Tue May 20, 2003 8:52 pm |
|
|
Hello
I need to find the time between two max values. The signal I'm getting is much like a sinus wave. So how can I start the timer/ counter on the first max value and stop the timer on the next max value. So I can read the time? I'm maessuring in approx. 1,5 to 2 seconds.
Maby I'm stupid but I just cant get this to work. I'm using a 11MHz crystal on a PIC16F877
What is best to use timer1 or timer2, and how do I set this up?
Need desperate help!
Skrotnisse
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514610 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Find time between two max values. PS. HELP!!!! |
Posted: Tue May 20, 2003 9:29 pm |
|
|
At 11MHz your timers tick very fast. So what you have to do is to choose either Timer1 or Timer2, set it to interrupt on the overflow, and have a counter in the ISR function to count the number of the overflows. So that's what you need to do:
1.Setup the timer to overflow in for example 1ms
2.Find the first max value
3.Enable the timer interrupt
4.Find the second max value
5.Disable the interrupt
6.Check the counter. Your counter will be 2000 for a 2 second time difference between the maximums.
Hope this helps,
Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514612 |
|
|
SKROTNISSE Guest
|
Thank you so much! Can I use something like this? |
Posted: Tue May 20, 2003 10:05 pm |
|
|
Thank you so much. But how do I set up the timer2 to overflow every 1ms, and what is the IRS function?
Is this something that I can use? ;
#int_timer2
void tick_handler(void) {
static int tick=11;
if (--tick==0) {
++time;
tick=11;
}
}
main{
setup_timer_2(T2_DIV_BY_1,49,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
}
Thank you so much for any answer!!!
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514613 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Thank you so much! Can I use something like this? |
Posted: Wed May 21, 2003 12:29 am |
|
|
:=Thank you so much. But how do I set up the timer2 to overflow every 1ms, and what is the IRS function?
:=
:=Is this something that I can use? ;
:=
:=#int_timer2
:=void tick_handler(void) {
:=static int tick=11;
:= if (--tick==0) {
:= ++time;
:= tick=11;
:= }
:=}
:=
:=main{
:= setup_timer_2(T2_DIV_BY_1,49,5);
:= enable_interrupts(INT_TIMER2);
:= enable_interrupts(GLOBAL);
:=}
:=
:=
:=Thank you so much for any answer!!!
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514617 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Thank you so much! Can I use something like this? |
Posted: Wed May 21, 2003 12:33 am |
|
|
The line
setup_timer_2(T2_DIV_BY_1,219,13);
Gives a 1ms interrupt on Timer 2. So your code can look like this:
int32 Tick;
#int_timer2
void tick_handler(void) {
Tick++;
}
main{
Tick=0;
setup_timer_2(T2_DIV_BY_1,219,13);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
}
So the variable Tick is incremented every 1ms. You can reset it on the first aximum and check it on the second maximum (without the need to disable the interrupt anymore).
Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514618 |
|
|
SKROTNISSE Guest
|
Why is there a difference between timer and actual time |
Posted: Thu May 22, 2003 12:57 am |
|
|
Thank you once again! But now this works, but the timer isn't accurate enough. When I'm meassure 2000ms the timer shows 1966ms
Why is this happening? Do you have any solution to this?
Once again, thank you for your answer !
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514661 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Why is there a difference between timer and actual time |
Posted: Thu May 22, 2003 8:22 am |
|
|
:=Thank you once again! But now this works, but the timer isn't accurate enough. When I'm meassure 2000ms the timer shows 1966ms
:=
:=Why is this happening? Do you have any solution to this?
:=
:=Once again, thank you for your answer !
Search the forum for a thread on real time clocks. You will find some methods for measuring time. If your timer has a period of 0.983mS and you wish to measure the time between events start a counter with zero and add 983uS per interupt. What you have is a rounding error.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514673 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Why is there a difference between timer and actual time |
Posted: Thu May 22, 2003 5:24 pm |
|
|
The problem is probably that your timer doesn't overflow in exactly 1ms. Let's assume that your crystal IS 11MHz accurate. Try to find a pair of numbers for the Timer2 prescaler/postscaler that makes it oveflow in a round number (e.g. 1ms, 2ms,...).
One other thing you can do is to connect a 32.768KHz crystal to the T1OSI(RC1) and T1OSO(RC0) pins and setup Timer1 as a counter. Clear the Timer1 on the first maximum and read it after the second maximum. The value of Timer1 will show you the time difference (e.g. 16384 will mean 500ms). This is probably much more accurate.
__Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514686 |
|
|
SKROTNISSE Guest
|
That will probably solve it! But how .... |
Posted: Thu May 22, 2003 7:42 pm |
|
|
Thanks again!
But how do I set up the timer1 as a counter?
I'm very grateful for all your help!
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514687 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: That will probably solve it! But how .... |
Posted: Sat May 24, 2003 3:21 am |
|
|
You can use this line to setup Timer1 to use an external oscillator:
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
You can have it in two modes, sync and async. in the sync mode the PIC chip syncs the external clock pulses with the main oscillator. I guess it won't make difference in you application.
Remember you also need to connect the T1OSI and T1OSO pins to ground wiht two 33pF capacitors (that value is for a 32.768KHz crystal).
You can read all about setting up timers and all that in the CCS help.
__Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514718 |
|
|
|