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 Basic Question

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



Joined: 05 Dec 2009
Posts: 40

View user's profile Send private message

timer2 Basic Question
PostPosted: Wed Jan 26, 2011 12:29 pm     Reply with quote

hey guys,
I'm using timer2 for PWM & interrupt, I'm bound to use only timer2 for interrupt, I wanna ask that am I right

PWM Frequency = 20 Mhz ??
timer2 interrupt = 22.4 us ??
If yes then why I'm not able to get 22.4 us

Code:

#include <16F876A.h>
#FUSES NOWDT, HS, PUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD                   
#use delay(clock=20MHz , crystal)

#int_timer2                 
void timer2_isr(void)
 {
  clear_interrupt(int_timer2);
  output_toggle(PIN_C0);
 }

void main()
 {
  set_tris_c (0x00); 
  setup_ccp1(CCP_PWM);
  setup_timer_2(T2_DIV_BY_1, 249, 16); 
  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);               

  while(TRUE)
  {     
   set_PWM1_duty(100);
   delay_ms(500);
   set_PWM1_duty(200);
   delay_ms(500);
  }
 }

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 26, 2011 2:04 pm     Reply with quote

Try this setting:
Code:
setup_timer_2(T2_DIV_BY_1, 109, 1); 
bells_electronics



Joined: 05 Dec 2009
Posts: 40

View user's profile Send private message

PostPosted: Wed Jan 26, 2011 8:55 pm     Reply with quote

Sir there is an example in CCS C Manual on page 250.

Example:
Code:

setup_timer_2 ( T2_DIV_BY_4, 0xc0, 2);
// At 20mhz, the timer will increment every 800ns,
// will overflow every 154.4us,
// and will interrupt every 308.8us.

How he has calculated these values??
Are they correct?
bells_electronics



Joined: 05 Dec 2009
Posts: 40

View user's profile Send private message

PostPosted: Thu Jan 27, 2011 7:09 am     Reply with quote

any one plzzz
i need formula for timer 2
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Jan 27, 2011 10:02 am     Reply with quote

It is in the _data sheet_....

Master clock (in your case 20MHz).
/4
/prescaler
/PR2+1
Then /postscaler for the interrupt

In the example:
(20000000)/4 /4 / (192+1) /2

= 3238.34 times/sec = 308.8uSec

For the values from PCM programmer:

(20000000)/4 /1 /(109+1) /1

= 45454.54 times/sec = 22uSec

However _beware_. 22uSec, is pushing it for a PIC interrupt. It is just 110 instruction times at 20Mhz. It typically takes about 60 instruction times to get into, and out of an interrupt handler, so _doing nothing at all_ in the interrupt handler, you would be spending about 60% of your time in the handler....

Comment - you don't have to clear the interrupt in the handler - the compiler does this for you. Wasted instruction.....

Best Wishes
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