View previous topic :: View next topic |
Author |
Message |
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
Timer2 question |
Posted: Mon Nov 08, 2004 5:57 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
|
Posted: Mon Nov 08, 2004 6:03 pm |
|
|
Also, what would happen if I changed the '1' to a '2'?
thanks |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
From the help file..... |
Posted: Mon Nov 08, 2004 6:15 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Nov 08, 2004 7:16 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
thanks |
Posted: Tue Nov 09, 2004 7:30 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|