|
|
View previous topic :: View next topic |
Author |
Message |
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
Program hangs on Timer2 Enable |
Posted: Sat Jan 28, 2012 4:58 pm |
|
|
I have a strange situation where my program appears to be hanging up as soon as I enable Timer2. I've tried other timers and had no problem. Using the latest 4.129 compiler, but have also tried a previous version (4.127) with the same results.
Code: |
#case
#include <18F66K22.h>
#device *=16
#device ADC=12
#fuses NOWDT,PROTECT,BROWNOUT,PUT,NODEBUG,NOCPD,CPB,NOMCLR
#use delay(clock=8000000)
#include "lcd416.c"
void main()
{
setup_oscillator(OSC_8MHZ);
lcd_init();
lcd_putc("\f");
lcd_gotoxy(1,1);
lcd_putc("Made it here 1..");
setup_timer_2(T2_DIV_BY_1, 100, 1);
lcd_gotoxy(1,1);
lcd_putc("Made it here 2..");
enable_interrupts(INT_TIMER2); //hangs here
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);
lcd_gotoxy(1,1);
lcd_putc("Made it here 3..");
for(;;);
} //end main
|
Code: |
30: enable_interrupts(INT_TIMER2); //hangs here
0276 829D BSF 0xf9d, 0x1, ACCESS
31: enable_interrupts(INT_TIMER3);
0278 82A0 BSF 0xfa0, 0x1, ACCESS
32: enable_interrupts(GLOBAL);
027A 0EC0 MOVLW 0xc0
027C 12F2 IORWF 0xff2, F, ACCESS
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 28, 2012 7:07 pm |
|
|
You enabled the Timer and the interrupts, but you didn't put in a interrupt
service routine. So when the PIC interrupts it jumps to address 0x0008,
but there's no isr, so the program crashes.
Example of a program with the Timer2 isr:
http://www.ccsinfo.com/forum/viewtopic.php?t=41473&start=3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Jan 29, 2012 2:57 am |
|
|
Also, seriously, 'think again'. You have the master clock, feeding this timer, and interrupting effectively every 101 instructions. Now it takes typically something like 60 instructions just to get into and out of an ISR. Do anything in the ISR, and the chip will have no time left to do anything else, especially getting into yet another ISR as well.....
Best Wishes |
|
|
MotoDan
Joined: 30 Dec 2011 Posts: 55
|
|
Posted: Sun Jan 29, 2012 11:52 am |
|
|
Thanks for the replies. Sorry for not including the complete code listing. My intent was to use Timer2 to drive a PWM output - not an ISR. This is where I made the mistake of assuming I needed to enable TIMER2 interrupts in order for the PWM to wok. As you suggested PCM, the program no longer hangs when I put in an ISR for TIMER2.
Ttelmah - Timer2 is fed by Fosc/4, not directly of the 8 MHz clock. By using a PR2 value of 100, Timer2 interrupts at 20 kHz which is my desired PWM frequency.
Thanks again for your help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Jan 29, 2012 3:12 pm |
|
|
Fosc/4, _is_ the instruction rate of the processor (it takes four clock cycles to perform one instruction), so what I said is exactly right. However (fortunately), doesn't matter if you don't use the interrupt...
However your programmed rate is slightly wrong. The clock rate will only be 19801Hz. For 20KHz, you need the PR2 value to be 99. The count for the PWM/interrupt, is this value _plus one_. Hence my talking about 101 instructions.
Best Wishes |
|
|
|
|
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
|