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 support@ccsinfo.com

Enter to timer2 interrupt with a button

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



Joined: 18 Sep 2018
Posts: 2

View user's profile Send private message

Enter to timer2 interrupt with a button
PostPosted: Fri Sep 21, 2018 1:46 pm     Reply with quote

Hello. I just start to do programming with ccs c. I try to enter to timer 2 interrupt with a button but I don't achieve this goal. Plus, I want to read button's situation to enter the function.
There is the code that I wrote:

Code:
#int_timer2
void timer2_isr()
{
   if(leddurum==0 && ledtam==0)
   {
     dutyTime++;
   if(dutyTime > 250)
   {
      dutyTime=0;
      x++;
      if(x > 100)
      {
         x=0;
         dc=0;
         dutyTime=0;
         leddurum=1;
         ledtam=1;
      }
   }
   dc++;
   if(dc > 100)
   {
      dc=0;
   }
   if(dc <= x && ledtam==0)
   {
      output_high(pin_b1);
   }
   if(dc > x && ledtam==0)
   {
      output_low(pin_b1);
   }
   if(ledtam==1)
   {
   output_high(pin_b1);
   ledtam=0;
   }
   }

void main()
{
   
   set_tris_b(0b11111110);
   set_tris_a(0b11111111);
   output_b(0x00);
   
   setup_timer_2(t2_div_by_16, 0, 1);
   
 
   if(input(pin_a2)==0)
   {
   delay_ms(50);
   situation++;
   while(!input(pin_a2));
   enable_interrupts(int_timer2);
   enable_interrupts(GLOBAL);
   }
   
}


Note: I want to do that: If I press the button, I enter to interrupt and the led which is connected to pin_b3 should be opened slowly. If the LED reach to maximum brightness, it should be keep up its appearance.
Thank you for answering.
gaugeguy



Joined: 05 Apr 2011
Posts: 288

View user's profile Send private message

PostPosted: Fri Sep 21, 2018 2:25 pm     Reply with quote

When you get to the end of main() the processor goes to sleep and does not execute any more instructions.
You may want to add an infinite loop in the appropriate place to keep your program running.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Sep 21, 2018 2:25 pm     Reply with quote

You need to tell us 'what chip'. However for most 'not going to happen'.

On most standard PIC's, Timer2, always runs off the internal oscillator. It doesn't offer an external clock input, so can't be triggered by an external signal (the other timers usually can). Also even if it was fed off an external signal, you are not going to get very far with DIV_16, since it would require 16 clocks to count once...

Use an interrupt input pin. That's what they are for....

The CCP modules can also be set to capture on an edge, and would again do what you want.
catalyst



Joined: 18 Sep 2018
Posts: 2

View user's profile Send private message

PostPosted: Fri Sep 21, 2018 3:11 pm     Reply with quote

Hi. Chip is PIC16F716. I want to use software tmr. I just start to program microchip.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 21, 2018 8:44 pm     Reply with quote

I think he means he wants to enable Timer0 interrupts when a button
is pushed.

First he needs to make a test program to verify that his switch circuit
and code work correctly. Just turn an LED on/off whenever a button
is pushed. Add the Timer0 interrupt code later, after the button is proven
to work correctly.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Sep 24, 2018 1:14 am     Reply with quote

OK. That sort of makes sense.
He is not going to 'enter Timer2 interrupt with a button', but enable the timer, which then calls it's interrupt.
As PCM_programmer says the key is to do one thing at a time. Write code to 'see' a button being pressed, and get this working. Then add to this the routine to enable Timer2, and it's interrupt.
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