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

Wrong output freq timer2, Why?

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



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

Wrong output freq timer2, Why?
PostPosted: Thu Oct 04, 2007 6:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 04, 2007 7:15 am     Reply with quote

The square-wave frequency (on the scope) is one-half the isr frequency.

Ken
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Oct 04, 2007 8:22 am     Reply with quote

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.
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