View previous topic :: View next topic |
Author |
Message |
PhenixLin
Joined: 17 May 2013 Posts: 2
|
How to enable timer2 interrupt for pic16f1933 |
Posted: Sun May 19, 2013 9:23 pm |
|
|
I want to do some control by timer2 interrupt of pic16f1933.
Here is my code:
Code: |
void main()
{
setup_timer_2(T2_DIV_BY_1, 1, 1);
set_tris_c(0x98);
output_bit(PIN_C0,1);
delay_ms(100);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(1);
}
#int_TIMER2
void timer2_isr(void)
{
output_toggle(PIN_C0);
}
|
But the PIN_C0 always be high which means the timer2_isr is not executed.
Please help me to check why the timer2 interrupt is not available.
Thanks for your help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 19, 2013 10:31 pm |
|
|
Always post a complete test program, with the #include, #fuses, and
#use delay(). Also post your compiler version.
With regard to your program, I assume you have an LED on pin C0 (with
series resistor). Set the Timer2 values so the timer runs and interrupts
at a slow enough rate for you to see the LED blinking. Example:
Code: |
#include <16F1933.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#int_timer2
void t2_isr(void)
{
output_toggle(PIN_C0);
}
//==========================================
void main()
{
setup_timer_2(T2_DIV_BY_64, 255, 16);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(1);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon May 20, 2013 12:46 am |
|
|
Juts giving a slight expansion to PCM's comment.
The point is that unless your clock is unbelievably slow, with your original code, you will never see C0 go down. It'll be toggling at just 1/8th the master clock rate, and an LED attached will appear to be 'on', just slightly dimmer, than fully on.
Human eye will see anything faster than perhaps 10Hz as 'on' (though you can see the 'flicker' out the corner of your eye as you move your head up to perhaps 200Hz). If you have a fast clock, even his code will look to be 'on' (this is why he has forced the CPU clock to 4MHz).
Best Wishes |
|
|
PhenixLin
Joined: 17 May 2013 Posts: 2
|
|
Posted: Tue May 21, 2013 2:12 am |
|
|
Dear all:
Thanks for your kindly help. ^^
The complier version is 4.104.
In fact, I monitor the PIN_C0 by oscilloscope, not LED.
I tried the code provided by PCM programmer and it works.
BUT, when I add another file into the project, the interrupt of timer 2 could not work.
The timer 2 interrupt comes back if I remove the file.
There is only one line in the file I add into the project:
Code: |
#include <16F1933.H>
|
I need your help for the strange issue.
Thanks again. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 21, 2013 10:30 am |
|
|
Don't #include 16F1933.h in every file that you add to the project.
Just #include it once, in the main source file. |
|
|
|