View previous topic :: View next topic |
Author |
Message |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
How to set interrupts? |
Posted: Sat Apr 06, 2013 7:06 am |
|
|
how can I set interrupts at any port in the PIC ?
I need to set also a counter that counts up when the interrupt goes from h_to_low and then goes from low to high.
I started to call some interrupts functions. But, it's still not complete .
in the beginning of the program I wrote the following :
Code: |
#INT_EXT
void ext_isr() {
static int1 Encoder_1 =FALSE;
if(!Encoder_1)
{
Encoder_1 =TRUE;
ext_int_edge(L_TO_H); // change so interrupts on release
}
delay_ms(100); // debounce button
}
|
Encoder_1 ====> PIN_A3
In the part in which I want the interrupt to be activated in wrote the following.
Code: |
ext_int_edge(H_TO_L); // init interrupt triggering
enable_interrupts(INT_EXT);// turn on interrupts
enable_interrupts(GLOBAL);
|
my programmer is still not working well. and I got confused.
Help please [/code] |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Sat Apr 06, 2013 7:51 am |
|
|
First thing to do is read the PIC datasheet and processor.h file that you're using.Within those documents you'll read which pins have interrupts.
2nd thing is to read the various examples that CCS supplies in the 'examples' folder.Several show how to setup and use interrupts.
We don't know which PIC you're using, but, historically the INT_EXT ISR has been associated with pin B_0 not A_3.
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Sat Apr 06, 2013 8:00 am |
|
|
Thank you for letting me notice about the pin
and Yes
I have checked the datasheet.
B0 refers to External Interrupt 0
B1 refers to External Interrupt 1
B2 refers to External Interrupt 2 |
|
![](templates/subSilver/images/spacer.gif) |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Sat Apr 06, 2013 12:04 pm |
|
|
Ok,
As I have been reading about the interrupts. I found out the following.
First I have to write the function in which the interrupts should do.
In my situation I should write the following program :
Code: |
#int_Encoder
void Encoder_isr() {
counter++;
}
in the main I have to activate
enable_interrupts(global);
enable_interrupts(int_encoder);
ext_int_edge( L_TO_H );// this will set the interrupt when
// the interrupted signal goes from L to h
// and then
|
I wish I could get it right |
|
![](templates/subSilver/images/spacer.gif) |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Sat Apr 06, 2013 12:33 pm |
|
|
No
Code: |
ISR declaration should be
#int_EXT
void Encoder_isr()
{
counter++;
}
void main() {
enable_interrupts(INT_EXT)
ext_int_edge( L_TO_H );// this will set the interrupt when
// the interrupted signal goes from L to h
// and then
your code
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Sat Apr 06, 2013 1:53 pm |
|
|
Not quite.
Code: |
#int_EXT
void Encoder_isr()
{
counter++;
}
void main()
{
enable_interrupts(INT_EXT)
ext_int_edge( L_TO_H );// this will set the interrupt when
// the interrupted signal goes from L to h
// and then
clear_interrupt(INT_EXT); //if you read the data sheet, you will
//find that selecting the edge, can result in the interrupt becoming
//set, so you should clear it after setting the edge, and before
enable_interrupts(GLOBAL);
//Now the code will call the interrupt in the rising edge.
while (TRUE)
{
//your code
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Sat Apr 06, 2013 10:27 pm |
|
|
I have written this program :
Code: |
#include <18F2455.H>
#device PASS_STRINGS=IN_RAM
#FUSES NOWDT,HS,NOBROWNOUT,NOLVP
#USE delay(clock=16M)
int counter = 0 ;
#int_EXT
void Encoder_isr() {
counter++;
}
#use rs232(baud = 9600 , xmit = PIN_A4, stream = COM_LCD)
#include<lcd.h>
void main()
{
delay_ms(2000);
fprintf(COM_LCD,"Hello");
enable_interrupts(INT_EXT);
ext_int_edge( L_TO_H );
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);
while (TRUE)
{
lcd_cursor_move(1,6);
fprintf(COM_LCD,"%d",counter);
}
}
|
when I displayed it in the LCD. The counter doesn't stop counting even if nothing has occurred it counts all the time. Isn't the counter supposed to count if and only if it goes from low to high ?
Where is the mistake ? |
|
![](templates/subSilver/images/spacer.gif) |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Sat Apr 06, 2013 11:01 pm |
|
|
Thank you guys for your help.
I believe there was no mistake in my program.
I could find out that the mistake was Hardware mistake.
Thank you for your replies |
|
![](templates/subSilver/images/spacer.gif) |
|