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 support@ccsinfo.com

Counting Time Without Interrupts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rhaguiuda



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

Counting Time Without Interrupts
PostPosted: Mon Feb 09, 2009 6:09 am     Reply with quote

How is the best way to count time, without using timers or delays?

For example, if I need a 1ms delay?

Thanks.
_________________
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime.
Ttelmah
Guest







PostPosted: Mon Feb 09, 2009 8:22 am     Reply with quote

At the end of the day, there are really three ways of counting time:

1) Count machine cycles. This is what the CCS 'delay' uses.
2) Count with an internal hardware timer. You can either use interrupts with this, or just poll it.
3) Count externally. For this, you need hardware other than the PIC (most RTC chips support 'alarms' at programmed intervals or times).

Now, if you have actually ruled out '1' and '2', you are left with adding hardware. This can range from using (for example), the charge time on a capacitor, through using a digital counter of some sort.

There are some 'odd' solutions that might apply, but at heart they are all based on one of the above. For instance, if you program the UART, to 100bps, and then send a character, the transmitter buffer will go empty after 1mSec. However this is just a variant of '2', but using a counter that is not normally targetted at 'timing'.

Best Wishes
rhaguiuda



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 8:38 am     Reply with quote

Ttelmah, thanks for repply.

I liked the second suggestion.
So I can use the timer1 counter, for example, to count the time I need. All I have to do is make sure it won't generate interrupts (by not allowing it to overflow).

Thanks again!
_________________
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime.
Ttelmah
Guest







PostPosted: Mon Feb 09, 2009 9:31 am     Reply with quote

You can let it overflow.

The timers when they overflow, set _interrupt flags_. An 'interrupt' (actual call to an interrupt handler), _requires_ three things to be true, before it'll occur:
1) A device interrupt flag being set.
2) It's interrupt enable being set.
3) The global interrupt enable being set.

Now, the timer hardware sets '1', but it is up to you to set '2',and '3'. Assuming you are using interrupts on other devices, '3' will be set, but provided you ensure '2' is not set, no actual interrupt will occur. Indeed you can use the interrupt flag 'bit', as if this was an extra bit of the hardware timer counter, giving you potentially 17bit of counting, (for a 16bit timer).

Also, if you want, you can use the interrupt flag, as a hardware 'marker' to say that a particular time has been reached. Soo (for instance):
Code:

//On PIC18 you will need to change the addresses for a PIC16
#byte PIR2=0xFA1
#bit tmr3IF=PIR2.1

setup_timer3(T3_DIV_BY_8|T3_INTERNAL);

//timer3, now counts in CLOCK/(8*4) steps
//So assuming clock = 20MHz, and you want 1mSec, you would want
//625 steps

disable_interrupts(INT_TIMER3); //ensure the interrupt enable is clear
set_timer3(64911); //65536-625
tmr3IF=0; //clear interrupt flag

while (tmr3IF==0) {
    //Do whatever you  want here - the interrupt flag will set when 1mSec
    //has passed.
}

//Arrive here when the flag sets.



You can use _timers_, without using interrupts.

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 9:47 am     Reply with quote

Quite correct, but note that if you use an interrupt, the flag that sends you to the interrupt (tmr3IF, or whatever) gets cleared automatically, so you won't e-enter the interrupt. If you just monitor the flag in software, it's your code that has to clear the flag! Failing to do this means that the program will keep responding to the flag.
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