View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
interrupt on certain pins H2W |
Posted: Wed Dec 18, 2013 3:25 am |
|
|
Hello! I made a program that suppose to use two interrupt pins H2W
Everything work, but the interrupt function responds with a certain delay. I mean when I press the button I need to hold it on about 1 sec to obtain isr reaction.
the code: Code: |
#include <16f1508.h>
#fuses INTRC_IO,NOMCLR,NOWDT
#use delay(clock=16M)
#define inc_butt pin_a5
#define dec_butt pin_a4
#define confirm_butt pin_c5
#define led_r pin_c3
#define led_w pin_c4
#define relay_SB pin_c6
#define T1 pin_b7
#define T2 pin_b7
unsigned int16 ms=2000;
#INT_RA
void isr()
{
delay_ms(10);//to prevent debounce of button
if(!input(inc_butt))
output_toggle(relay_sb);
if(!input(dec_butt))
output_toggle(relay_sb);
clear_interrupt(INT_RA5_H2L|INT_RA4_H2L);
}
void main()
{
ENABLE_INTERRUPTS(INT_RA5_H2L|INT_RA4_H2L);
ENABLE_INTERRUPTS(GLOBAL);
clear_interrupt(INT_RA5_H2L|INT_RA4_H2L);
while (true)
{
output_low(led_r);
delay_ms(MS);
output_high(led_r);
delay_ms(MS);
}
} |
Does anyone see something wrong? _________________ A person who never made a mistake never tried anything new. |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Dec 18, 2013 3:39 am |
|
|
Yes, your delay in the interrupt. When you compile you will have a warning "interrupts disabled to prevent re-entrancy" or similar, meaning while in delay routine interrupts will not work.
Regards |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Dec 18, 2013 7:30 am |
|
|
Ok if I have delay inside interrupt function, this apparently is a problem (because of the latency). In other hand, if I remove that delay I got button debounce, that cause re-entrance interrupt. Is there solution on my problem anyway? _________________ A person who never made a mistake never tried anything new. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Dec 18, 2013 8:20 am |
|
|
You could try using one of the hardware timers to deal with the debounce.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Dec 18, 2013 8:36 am |
|
|
Or you can do the entire operation using a 'tick' interrupt instead of the edge interrupt.
As you have found, buttons/keys don't give nice edges, so using the edge interrupt is often pointless. Instead read the inputs in a tick at (say) 100Hz. If they are 'made' for two successive ticks, then accept as a input.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Wed Dec 18, 2013 8:48 am |
|
|
Here's an 'inline' debounce code segment
...
#define LT PIN_B1 ;pin with switch
while (!input(LT) ) ; //wait if low
delay_ms(20); //debounce delay
while (input(LT) ) ;//wait if hi
delay_ms(20); //debounce delay
...
may be 'rude and crude' but it does work.
reversing the order of the whiles,change if looking for h2l or l2h .
hth
jay |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Thu Dec 19, 2013 1:34 pm |
|
|
Thank you guys! What did good job for me was the modifying of the hardware. I put capacitor parallel to the button, then I removed the delay inside the interrupt function and everything got better and steady as well. So, software delay might work well either, but I don't want to have any disturbance into the switch cycle. It would violate the timing (may be insignificantly) of the cycle.
Thank you again! _________________ A person who never made a mistake never tried anything new. |
|
|
|