View previous topic :: View next topic |
Author |
Message |
catalyst
Joined: 18 Sep 2018 Posts: 2
|
Enter to timer2 interrupt with a button |
Posted: Fri Sep 21, 2018 1:46 pm |
|
|
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: 303
|
|
Posted: Fri Sep 21, 2018 2:25 pm |
|
|
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: 19552
|
|
Posted: Fri Sep 21, 2018 2:25 pm |
|
|
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
|
|
Posted: Fri Sep 21, 2018 3:11 pm |
|
|
Hi. Chip is PIC16F716. I want to use software tmr. I just start to program microchip. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 21, 2018 8:44 pm |
|
|
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: 19552
|
|
Posted: Mon Sep 24, 2018 1:14 am |
|
|
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. |
|
|
|