View previous topic :: View next topic |
Author |
Message |
bells_electronics
Joined: 05 Dec 2009 Posts: 40
|
timer2 Basic Question |
Posted: Wed Jan 26, 2011 12:29 pm |
|
|
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);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 26, 2011 2:04 pm |
|
|
Try this setting:
Code: | setup_timer_2(T2_DIV_BY_1, 109, 1); |
|
|
![](templates/subSilver/images/spacer.gif) |
bells_electronics
Joined: 05 Dec 2009 Posts: 40
|
|
Posted: Wed Jan 26, 2011 8:55 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
bells_electronics
Joined: 05 Dec 2009 Posts: 40
|
|
Posted: Thu Jan 27, 2011 7:09 am |
|
|
any one plzzz
i need formula for timer 2 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Thu Jan 27, 2011 10:02 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|