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

Timer2 question

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



Joined: 04 Nov 2004
Posts: 67

View user's profile Send private message Send e-mail

Timer2 question
PostPosted: Mon Nov 08, 2004 5:57 pm     Reply with quote

Hello All,

i'm using a PIC16F876 and I have a question about using Timer2 as an interrupt. I'm trying to understand how to figure out when the interrupt occurs and when it resets and what is the difference between the two? Currently I have timer2 settings as:


setup_timer_2(T2_DIV_BY_16, 156,1)
and its running at 20MHz

I believe that the T2_DIV_BY_16 is a prescalar to take it from 20MHz to (20/16)MHz, but what does the 156 mean and what does the 1 mean? ANy help would be so unbelieveably greatly appreciated.

Jon
jseidmann



Joined: 04 Nov 2004
Posts: 67

View user's profile Send private message Send e-mail

PostPosted: Mon Nov 08, 2004 6:03 pm     Reply with quote

Also, what would happen if I changed the '1' to a '2'?

thanks
dyeatman



Joined: 06 Sep 2003
Posts: 1937
Location: Norman, OK

View user's profile Send private message

From the help file.....
PostPosted: Mon Nov 08, 2004 6:15 pm     Reply with quote

Syntax:
setup_timer_2 (mode, period, postscale)

Parameters:
mode may be one of:

T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16

period is a int 0-255 that determines when the clock value is reset,

postscale is a number 1-16 that determines how many timer resets before an interrupt: (1 means one reset, 2 means 2, and so on).

Function:
Initializes timer 2. The mode specifies the clock divisor (from the oscillator clock). The timer value may be read and written to using GET_TIMER2() and SET_TIMER2(). Timer 2 is a 8 bit counter/timer.

Requires
Constants are defined in the devices .h file.

Examples:
setup_timer_2 ( T2_DIV_BY_4, 0xc0, 2);

// At 20mhz, the timer will include every 800ns,

// will overflow every 153.6us,

// and will interrupt every 460.3us.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Nov 08, 2004 7:16 pm     Reply with quote

Sometimes the manual/'help file' is a bit hard to understand for somebody that's not experienced the PIC very much.

Quote:

setup_timer_2(T2_DIV_BY_16, 156,1)
and its running at 20MHz


If your oscillator is running at 20MHZ it will be divided by 4 before it hits the timer circuit which will give you a 5MHZ clock into the timer. The 'T2_DIV_BY_16' will then divide that 5MHZ signal by 16 which will leave you with 312500HZ. Now, the timer will begin to count at this rate. Once the timer register has counted up to the value of 156 (your period) it will then reset back to zero on the next clock pulse coming in. This will then be filtered by the postscaler which is set to a one(1) by the setup_timer() statement. Since it is set to a one(1) an interrupt will occur each time the register is reset. If you had set the postscaler to 16 then you would get an interrupt every 16th time the register was reset. The compiler will take care of clearing the interrupt flag for you. Just remember to create a interrupt service routine for timer2:

Code:

#int_TIMER2
TIMER2_isr()
{
}


and then enable that interrupt.

Clear as mud?

Look at the block diagram in the spec. sheet and then try experimenting with it a bit.

Ronald
jseidmann



Joined: 04 Nov 2004
Posts: 67

View user's profile Send private message Send e-mail

thanks
PostPosted: Tue Nov 09, 2004 7:30 am     Reply with quote

thank you, that is very clear. I had already looked at the manual and it didnt mention the timer clock being 1/4th the oscillator or really flesh out the details. I appreciate all your help.

jon
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