View previous topic :: View next topic |
Author |
Message |
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
how external interrupt ?? |
Posted: Fri Oct 12, 2012 3:03 pm |
|
|
I want to take -my first- external interrupt from ir transistor but this code not work.
Code: | void EXT_isr(void)
{
delay_ms (50); //debounce
if( input(PIN_B0))
{
OUTPUT_D(0xf0);
}
else
{
OUTPUT_D(0x0f);
}
}
void main()
{
SET_TRIS_D( 0x00 );
setup_comparator(NC_NC_NC_NC);
OUTPUT_D(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Oct 12, 2012 3:47 pm |
|
|
Read the CCS forum guidelines.
Post complete compilable code, so that we can copy and test.
Tell us about hardware and what code does/doesn't do.
Mike |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Fri Oct 12, 2012 4:03 pm |
|
|
Code: | #include <16F887.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=8000000)
|
i connect the ir transistor to RB0 as an external interrupt ... i just want when the ir transisror switch RBO >>>OUTPUT_D(0xf0); |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Oct 12, 2012 4:21 pm |
|
|
hmm..
Does the IR transistor need a pullup resistor ?
Does the IR unit need 3.3V and the PIC 5V ?
Does your proram work right if you substitute a switch in place of the IR unit ?
Assuming you have LEDS connected to port D, does a simple 'turn LEDs on' program work correctly? |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Fri Oct 12, 2012 4:28 pm |
|
|
I already checked the leds and replaced the ir transistor with a simple switch ...........but nothing.
????????????? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Fri Oct 12, 2012 7:21 pm |
|
|
The code you have shown is incomplete. You don't show, for example the #INT_EXT directive anywhere in your code, though that would be required for the interrupt to function. |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Fri Oct 12, 2012 8:02 pm |
|
|
thnx for your interest
this is the full code:
Code: | #include <povint.h>
#int_EXT
void EXT_isr(void)
{
delay_ms (50); //debounce
if( input(PIN_B0))
{
OUTPUT_D(0xf0);
}
else
{
OUTPUT_D(0x0f);
}
}
void main()
{
SET_TRIS_D( 0x00 );
setup_comparator(NC_NC_NC_NC);
OUTPUT_D(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
}
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Oct 13, 2012 3:39 am |
|
|
Have you got a pull up resistor with your switch?
Does a simple LED flasher work?
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Oct 13, 2012 4:28 am |
|
|
Several things:
By default INT_EXT will trigger on the rising edge on pin B0. So the interrupt code will always see B0 as 'high', and will output 0xF0.
The delay in the interrupt doesn't really do what you want. The point about 'debounce', is that you need to sample initially to see what edge you have, and then sample _again_ to verify it is still the same. Generally, delays inside interrupts are considered 'poor practice', since it rather ruins the 'point' of interrupts, preventing other code from running....
You do realise the chip will have gone to sleep (run off the end of the code), so will take a relatively long time to wake up and respond.
On the 887, the chip will wake up with RB0 setup for analog. Won't work for digital input like this.
Realistically, start by proving your chip is running with a simple 'flash LED' code. Then move on one step at a time.
Best Wishes |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Sat Oct 13, 2012 10:32 am |
|
|
Quote: | Have you got a pull up resistor with your switch?
Does a simple LED flasher work?
Mike | yes, all is ok.
Quote: |
By default INT_EXT will trigger on the rising edge on pin B0. So the interrupt code will always see B0 as 'high', and will output 0xF0.
|
When pin B0 see the falling edge ? There is another method for sense ???
Quote: | You do realise the chip will have gone to sleep (run off the end of the code), so will take a relatively long time to wake up and respond. |
Can I put Code: | enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL); | in a loop ???
thanx |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Oct 13, 2012 2:10 pm |
|
|
Ttelmah means you should do this: Add the line shown in bold below.
Quote: |
void main()
{
SET_TRIS_D( 0x00 );
setup_comparator(NC_NC_NC_NC);
OUTPUT_D(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
while(1); // Prevent PIC from going to sleep
}
|
Then the PIC will never execute the hidden Sleep instruction that the
compiler puts at the end of main(). The PIC will continue to run OK. |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Sat Oct 13, 2012 2:32 pm |
|
|
thnx PCM programmer
it's run now
but in a random run ( very very slow and bad response) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Oct 13, 2012 2:37 pm |
|
|
What's the purpose of your project ? What is the external device that
produces the IR that you want to detect ? Post a link to a schematic of
your project. Use a free image-hosting website for this. (Find one with Google). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Oct 13, 2012 2:39 pm |
|
|
Sounds like you've compiled using the 'debug' option and not the 'release' option in the build configuration pulldown tabs.
The newer version of MPLAB allows you to save 'release' as the default( my normal option).
hth
jay |
|
|
gamal eldeen
Joined: 29 May 2012 Posts: 29 Location: Alexandria - Egypt
|
|
Posted: Sat Oct 13, 2012 2:47 pm |
|
|
Quote: | What's the purpose of your project ? What is the external device that
produces the IR that you want to detect ?
|
I want to make a propeller clock and I want to use the IR LED to trigger the PHOTOTRANSISTOR to use it as a home trigger, but now for simulation I replace all of that by a simple switch to RB0. |
|
|
|