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

How to enable timer2 interrupt for pic16f1933

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



Joined: 17 May 2013
Posts: 2

View user's profile Send private message

How to enable timer2 interrupt for pic16f1933
PostPosted: Sun May 19, 2013 9:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 19, 2013 10:31 pm     Reply with quote

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: 19499

View user's profile Send private message

PostPosted: Mon May 20, 2013 12:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 21, 2013 2:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 21, 2013 10:30 am     Reply with quote

Don't #include 16F1933.h in every file that you add to the project.
Just #include it once, in the main source file.
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