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 CCS Technical Support

Re: Questions about TIMER1 and interrupts

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







Questions about TIMER1 and interrupts
PostPosted: Wed Nov 07, 2001 2:32 am     Reply with quote

All,
I am still a bit confused as to the correct operation
of TIMER1 and the overflow interrupt.

Given a 16f876 with a 20mhz clock.

TIMER1 is setup as follows:

setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_TIMER1);

Am I correct to assume the following:

1. TIMER1 will increment its count
every (1 / (20000000 / 4)) = .2uSec.
2. TIMER1 will interrupt on overflow
every (.0000002 x 65536) = 0.0131072 seconds.
3. get_timer1() will retrieve the current
TIMER1 accumulated tick count as a 16 bit number.
4. this tick count can range 0-65536 and represents
an actual time of 0-0.0131072 seconds.

If I want to accumulate a long running total of
elapsed time between 2 events can I:
1. Keep track of the number of times TIMER1 overflows.
2. Multiply this overflow count x 0.0131072(time required
for TIMER1 to overflow).
3. Read the current tick count with get_timer1().
4. Multiply this tick count x 0.0000002(time between
increments of TIMER1).
5. Add result from #2 to result from #4 to get the total accumulated elapsed time.

for example:

#int_timer1
timer1_isr()
{
ulNumOverFlows++;

if(ulNumOverFlows == 1000)
{
boolOutOfRange = true;
}
}

main()
{
setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_TIMER1);

// wait for first event
while(!event1)
;

// reset TIMER1 internal count
set_timer1(0);

// wait for second event
while(!event2)
;

// calculate elapsed time between event1 and event2
// read current tick count from TIMER1
// each tick count will represent .2 uSecs
ulEvent2WaitCount = get_timer1();


// add overflow time to current tick count
// to get true elapsed time
float fElapsedTime = (float)(ulNumOverFlows) *
(float) 0.0131072 +
(float)(ulEvent2WaitCount) *
(float) 0.0000002;

}

Am I somewhere in the ballpark ???
Any comments or alternate suggestions would be greatly appreciated.

Thanks,
John
___________________________
This message was ported from CCS's old forum
Original Post ID: 996
Tomi
Guest







Re: Questions about TIMER1 and interrupts
PostPosted: Sat Nov 10, 2001 2:06 am     Reply with quote

Your basic concept is OK, but you have choose a difficult solution. Let's simplify your conditions.
If you have a 32-bit variable (I assume you use V3 compiler) to hold the elapsed clock periods then we can say that the content of the var. is the (elapsed time in microseconds)*5 Don't use a lot of float calculations: it is a RAM-and ROM-expensive method.

Try this:

int32 elapsedTime; // var. for elapsed time in 0.2 usec steps

#int_timer1
void __Timer1()
{
int16 upperhalf;
elapsedTime += 0x00010000; // don't worry: it is a simple 16-bit addition to increment the upper 16 bits of the 32-bit var
upperhalf = (int16)(elapsedTime >> 16); // check for timeout
if (upperhalf == 1000) boolOutOfRange = true;
}

In your main():
float fElapsed;
setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL); // you MUST enable global, too
while (!Event1) ;
Set_Timer1(0);
elapsedTime = 0;
while (!Event2) ;
disable_interrupts(INT_TIMER1); // to fix the upper 16 bit of elapsedTime
elapsedTime |= Get_Timer1(); // get the lower half of the 32-bit var
fElapsed = elapsedTime;
fElapsed /= 5000000.0; // to calc. seconds as float
elapsedTime /= 5; // to get time as microseconds


:=All,
:=I am still a bit confused as to the correct operation
:=of TIMER1 and the overflow interrupt.
:=
:=Given a 16f876 with a 20mhz clock.
:=
:=TIMER1 is setup as follows:
:=
:=setup_timer_1(T1_INTERNAL);
:=enable_interrupts(INT_TIMER1);
:=
:=Am I correct to assume the following:
:=
:=1. TIMER1 will increment its count
:=every (1 / (20000000 / 4)) = .2uSec.
:=2. TIMER1 will interrupt on overflow
:=every (.0000002 x 65536) = 0.0131072 seconds.
:=3. get_timer1() will retrieve the current
:=TIMER1 accumulated tick count as a 16 bit number.
:=4. this tick count can range 0-65536 and represents
:=an actual time of 0-0.0131072 seconds.
:=
:=If I want to accumulate a long running total of
:=elapsed time between 2 events can I:
:=1. Keep track of the number of times TIMER1 overflows.
:=2. Multiply this overflow count x 0.0131072(time required
:=for TIMER1 to overflow).
:=3. Read the current tick count with get_timer1().
:=4. Multiply this tick count x 0.0000002(time between
:=increments of TIMER1).
:=5. Add result from #2 to result from #4 to get the total accumulated elapsed time.
:=
:=for example:
:=
:=#int_timer1
:=timer1_isr()
:={
:= ulNumOverFlows++;
:=
:= if(ulNumOverFlows == 1000)
:= {
:= boolOutOfRange = true;
:= }
:=}
:=
:=main()
:={
:= setup_timer_1(T1_INTERNAL);
:= enable_interrupts(INT_TIMER1);
:=
:= // wait for first event
:= while(!event1)
:= ;
:=
:= // reset TIMER1 internal count
:= set_timer1(0);
:=
:= // wait for second event
:= while(!event2)
:= ;
:=
:= // calculate elapsed time between event1 and event2
:= // read current tick count from TIMER1
:= // each tick count will represent .2 uSecs
:= ulEvent2WaitCount = get_timer1();
:=
:=
:= // add overflow time to current tick count
:= // to get true elapsed time
:= float fElapsedTime = (float)(ulNumOverFlows) *
:= (float) 0.0131072 +
:= (float)(ulEvent2WaitCount) *
:= (float) 0.0000002;
:=
:=}
:=
:=Am I somewhere in the ballpark ???
:=Any comments or alternate suggestions would be greatly appreciated.
:=
:=Thanks,
:=John
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 1046
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