View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
want to set interrupt on falling edge |
Posted: Wed Jun 16, 2010 1:29 am |
|
|
Hi,
I am using PIC18f252 and CCS compiler 4.093.
I wanna use external interrupt 1 (PIN_RB1/INT1).
What I want to do is that interrupt is enable at the falling edge.
I write a code but interrupt is generating on both rising and falling edge.
Following is my code.
Code: |
#int_EXT1
void EXT1_isr(void)
{
clear_interrupt(INT_EXT1);
enable_interrupts(INT_EXT1);
ext_int_edge(H_TO_L);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
}
void main()
{
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);
// TODO: USER CODE!!
while(1)
{
output_low(PIN_C0);
}
}
|
At hardware side what I have done is that pullup the RB1 pin using 10k resistor. A two pin connector is also connected, one pin of the connector is attached to the RB1 pin and second pin is connected to the ground.
My theme is that when I short the two pins an interrupt will be generate, the RB1 pin goes low and the falling edge will be detected by microcontroller.
Hope you guys understand |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Jun 16, 2010 2:00 am |
|
|
Code: |
#int_EXT1
void EXT1_isr(void)
{
//clear_interrupt(INT_EXT1);
//enable_interrupts(INT_EXT1);
//ext_int_edge(H_TO_L); Get rid of all these. In general, don't
//fiddle with interrupts inside interrupts!...
//General comment, what follows, would be better done using a timer
//interrupt - as it stands the chip is effectively 'locked up' from
//handling anything else for two seconds.....
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
}
void main()
{
//ext_int_edge(H_TO_L); This is the 'problem' line. ext_int_edge
//defaults to controlling the edge on INT0
ext_int_edge(1,H_TO_L); //Note the '1'.....
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);
// TODO: USER CODE!!
while(1)
{
output_low(PIN_C0);
}
}
|
Comments in the code.
Best Wishes |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Jun 16, 2010 2:25 am |
|
|
Thanks Ttelmah for your quick response.
I have correct the code but still the problem exists.
Code: |
#int_EXT1
void EXT1_isr(void)
{
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
}
void main()
{
ext_int_edge(1,H_TO_L);
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);
// TODO: USER CODE!!
while(1)
{
output_low(PIN_C0);
}
}
|
Actually i have to monitor the position of the switch (ON/OFF) at the interrupt pin so i think no need of timers. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Jun 16, 2010 4:03 am |
|
|
Your problem now, is _bounce_.
You are assuming that a switch just nice and cleanly 'makes'. Most don't. Your switch is almost certainly making and breaking a few times when you operate it. As such the interrupt will see a falling edge from this bounce...
Two possibilities:
1) Software debouce. Do something like:
Code: |
#int_EXT1
void EXT1_isr(void)
{
delay_ms(10); //wait 10mSec
if (input(PIN_B1)==1) break; //False trigger.
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
delay_ms(500);
}
|
The problem with the delay approach, is twofold. Whether this 'matters' is down to what else you want to do.
If you have any delays in the external 'main' code, interrupts will be disabled in these, potentially dleaying the switch response - this is because the PIC doesn't support re-entranty code (code called inside itself), and with delays in the external code, delays in the interrupts would result in this happening. Search here for solutions to this.
The second, is that while you are in this interrupt, other things will be missed. Serial I/O (for example), will miss characters while you are delaying in the interrupt. If you want to do anything like this, then consider another approach....
Best Wishes |
|
|
|