View previous topic :: View next topic |
Author |
Message |
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
Wrong output freq timer2, Why? |
Posted: Thu Oct 04, 2007 6:47 am |
|
|
Strange result for this example
This give a freq of 19.5Khz (scope)
but this should be 39Khz
Do i something wrong???
Code: | #include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT
// The cycle time will be (1/clock)*4*t2div*(period+1)
// In this program clock=20000000 and period=127
// (1/20000000)*4*1*128=25.6us or 39.0khz
#int_TIMER2
void TIMER2_isr() {
output_toggle(PIN_C1);
}
void main() {
setup_timer_2(T2_DIV_BY_1,127,1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE) {
}
} |
|
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Thu Oct 04, 2007 7:15 am |
|
|
The square-wave frequency (on the scope) is one-half the isr frequency.
Ken |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Oct 04, 2007 8:22 am |
|
|
Your ISR is being entered at a frequency of 39KHZ. You are simply toggling the output which, in effect, is acting as a divide-by-two. If you want to get 39KHZ out of the output then your ISR will need to toggle the output both UP and DOWN to get a 1:1 ratio out.
Ronald
_______________________
Only open up a can of worms if you're going fishing. |
|
|
|