View previous topic :: View next topic |
Author |
Message |
btklister
Joined: 16 Jul 2009 Posts: 14
|
Program don't run with timer2 interrupt |
Posted: Sat Apr 03, 2010 3:19 pm |
|
|
Hi
I have a simple program that don't run if the timer2 isr is activated.
If I delete it, the program will run normally. Why this happen?
Code: |
#include <16F88.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PUT //Power Up Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#use delay(clock=8000000)
#int_TIMER2
void TIMER2_isr(void)
{
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,19,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_8MHZ|OSC_TIMER1);
// TODO: USER CODE!!
while(1)
{
output_b(0x00);
delay_ms(1000);
output_b(0xff);
delay_ms(1000);
}
}
|
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 03, 2010 10:09 pm |
|
|
Quote: | setup_oscillator(OSC_8MHZ|OSC_TIMER1);
|
What happens if you delete this line ? |
|
|
btklister
Joined: 16 Jul 2009 Posts: 14
|
|
Posted: Sun Apr 04, 2010 11:39 am |
|
|
nothing hapens |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 04, 2010 12:39 pm |
|
|
What's your compiler version ? |
|
|
btklister
Joined: 16 Jul 2009 Posts: 14
|
|
Posted: Sun Apr 04, 2010 12:51 pm |
|
|
4.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 04, 2010 1:32 pm |
|
|
The problem is because your Timer2 interrupt rate is too fast.
CCS has an interrupt handler routine that "saves the machine state"
before it executes your #int_timer2 routine, and then restores it upon
exit from that routine. This takes about 60 instructions. With an 8 MHz
oscillator, the PIC does 2 instructions per microsecond. So it takes
about 30 us just for the interrupt handler overhead. Then, your
#int_timer2 routine also takes some time. It's an empty routine, but
it takes another 2-3 us to execute.
You have set the PR2 value at 19 in the setup_timer2() function.
This means that Timer2 counts from 0 to 19, which is 20 clocks.
The Timer2 clock is 2 MHz, or 0.5 us per clock. So 20 clocks x 0.5 us
gives a 10 us rollover period (or interrupt period). However, I have
already shown that the interrupt handler takes 32 us, at least.
What does this mean ? The interrupt handler takes much longer than
your interrupt period. A new interrupt will be generated, and be pending,
while the first interrupt is still being handled. As soon as your program
exits the interrupt handler, it will just jump right back into it. There
won't be any time left to run your program. The program will always
be executing your interrupts.
One solution is to reduce the interrupt rate. Change the T2 divisor to
div by 16, and you'll notice that the program will immediately start
working. Example:
Quote: |
setup_timer_2(T2_DIV_BY_16, 19, 1);
|
See this thread for more explanation of the CCS interrupt handler code:
http://www.ccsinfo.com/forum/viewtopic.php?t=29173 |
|
|
btklister
Joined: 16 Jul 2009 Posts: 14
|
|
Posted: Sun Apr 04, 2010 1:41 pm |
|
|
great! I understand ;)
Many thanks! |
|
|
|