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

Timer1 not working !

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



Joined: 04 Mar 2010
Posts: 27
Location: Caribbean

View user's profile Send private message

Timer1 not working !
PostPosted: Wed May 12, 2010 6:16 am     Reply with quote

Please help me use Timer1. I'm using a PIC16F876A with a 4MHz crystal.
I calculated the interrupts per second and I got ~15. I wanted a 2 second timer so i set counter to 30. Am I using the timer1 in the right way ?



Code:

#define ints_per_sec 30

#INT_TIMER1
void Tmr1_1s_isr() {
counter--;
if(counter==0){         
puts("T1_isr has been triggered "); // Not printing to screen
counter = ints_per_sec;
}
}
void main(){
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
counter = ints_per_sec;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

if(getch()=='a') // if 'a' recieved start timer1
set_timer1(0);

while(1);
}
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed May 12, 2010 9:06 am     Reply with quote

You're trying to use the timer incorrectly. First, never place a printf() or puts() statement in an ISR. Printf()'s takes way too much time. You want to be in and out as quickly as possible, and delays are not good either.

set_timer() takes several instruction cycles to execute and your timer will be many clocks down the road before you're ready to do whatever it is you want to accomplish.

You're not calculating things properly either. Your main clock is 4MHZ. This clock is divided by 4 before it goes into the timer circuitry. You now have a 1MHZ clock coming in. That is being devided by 8 by the prescaler. Your clock is now 125KHZ. The timer is a 16-bit timer which will count up to 65535 before rolling over to zero, at which time the ISR is entered. So, your calculation should be 4MHZ/4/8/65536 = ~1.9. You're having a little over 1.9 interrupts per second.

Now, I would suggest you use timer2 instead. It is possible to configure this to give you just what you want. Configure it with setup_timer_2(T2_DIV_BY_16,249,10);. Timer2 is an 8-bit counter with a preset number that will fire the interrupt when that number is counted to. Your prescaler will be 16, postscaler will be 10 and your ISR will enter when the timer rolls over from 249. So, your calculation will be:
4MHZ/4/16/250/10 = 25. Your ISR will happen 25 times each second. Next, have your counter variable count to 50. When it reaches 50, set a flag (1 bit variable) that can be evaluated in your main() section.

Clear as mud?

Ronald
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