| Author |
Message |
AhmedS
Joined: 11 Aug 2009 Posts: 8 Location: gaza strip
|
18 Aug 2009 16:07 interrupts in c |
|
|
|
|
in pic 16f877 there is 14 source of interrupts
can u tell me if you have two interrupts(low and high) in any program (write in C language)
how i can call interrupts?
in first we write : void interrupt()
but in second??????
((interrupts in C language high level))
|
|
| Back to top |
|
 |
btbass
Joined: 20 Jul 2001 Posts: 1187 Helped: 113 Location: Oberon
|
18 Aug 2009 17:26 interrupts on pic18 c language |
|
|
|
|
The Pic 16 family does not support interrupt priority, all interrupts have the same priority and there is only one interrupt vector.
The normal procedure is to test each flag of the interrupts you have enabled and service the ones that are set.
For Example:
| Code: |
/*--- Interrupt vector ---*/
static void interrupt isr(void)
{
if(T0IF)
{
Timer 0 interrupt;
}
if(TMR1IF)
{
Timer 1 interrupt;
}
if(CCP2IF)
{
Capture interrupt;
}
}
|
|
|
| Back to top |
|
 |
arthur0
Joined: 28 Nov 2003 Posts: 73 Helped: 9 Location: Stockholm, Sweden
|
18 Aug 2009 17:37 interrupt in pic c c language |
|
|
|
|
Hi,
I’ll try write a more general explanation below (I won’t relate to PIC or any specific µC/µP), in the hope that more can read and understand the concepts.
You never "call interrupts" yourself, in a program. An interrupt not function, but an event (usually external to your program, but it can also be a software generated) that interrupts the normal flow of execution, hence the name.
When detecting such an event, the "hardware" is built such that it will stop executing your program and select to jump and execute from a predetermined address (specific to each µC/µP and source).
Your job is to write small routines to serve these interruptions, and locate them at the right addresses.
Take Reset for instance that is an event defined on all the µC/µP I know of. When Reset happens (due to RESET signal kept low and then released, or elapsed watchdog timer, or low voltage detector, or whatever reason), the CPU will start executing from a specific address. When you write your program (in C) and specify main(), the compiler will locate it at the right address for your specific CPU so that it will be executed after every Reset event.
It is more or less the same with any other interrupt source (timer, ADC, UART etc.), but since there are multiple sources, you need to let the compiler know where to place your little Interrupt Service Routines (ISR) and you do that with specific pragmas and/or compiler-specific keywords.
It might be that all the interrupting events will use the same address to execute from. In such a case you’ll need to read specific flags and determine the source and do things accordingly, but the principle is the same.
Interrupt priorities are a totally different matter and it’s just a way the hardware chooses to handle cases when more than one interrupting event is active.
For instance, it might be that a timer has finished counting exactly when the ADC has finished converting, but since (we say that) the timer interrupt event has higher priority, the hardware will choose to jump and execute its ISR first and then the ADC’s.
Now I simplified my explanation a bit, because things are a tad more complicated than this, but for serving an interrupt in C that’s about all there is to know (plus, of course, read the funky manual!).
This is the kind of topic many have a problem with, but if you understand the simple concepts above (plus some other stingy issues that you might never encounter if you keep things simple), there’s really no reason to avoid using interrupts in your designs (on the contrary, there are great advantages).
Maybe someone else can help with the details of PIC, which are foreign to me.
Arthur
|
|
| Back to top |
|
 |
AhmedS
Joined: 11 Aug 2009 Posts: 8 Location: gaza strip
|
18 Aug 2009 20:18 c language code |
|
|
|
|
i mean if you have two interrupts(ISR) in 1 program
how you can call the 2 interrupts in C language???
|
|
| Back to top |
|
 |
btbass
Joined: 20 Jul 2001 Posts: 1187 Helped: 113 Location: Oberon
|
18 Aug 2009 20:45 interrupts in c |
|
|
|
|
You dont normally call interrupts, But
the interrupt flags in the Pic are read/write, so if you want the interrupt to fire, you can just set the interrupt flag. any time you like.
IE:
T0IF = 1;
will fire Timer 0 interrupt.
|
|
| Back to top |
|
 |
millwood
Joined: 02 Jul 2009 Posts: 121 Helped: 11
|
18 Aug 2009 21:00 language c interrupt |
|
|
|
|
| AhmedS wrote: |
i mean if you have two interrupts(ISR) in 1 program
how you can call the 2 interrupts in C language??? |
btbass answered your question directly and clearly.
which part of btbass's answer do you have trouble understanding?
|
|
| Back to top |
|
 |
AhmedS
Joined: 11 Aug 2009 Posts: 8 Location: gaza strip
|
18 Aug 2009 21:04 interrupt in c language |
|
|
|
|
in any program that have 1 interrupt,we write:
void interrupt()
{
----------
}
void main()
{
------
}
but if u have 2 interrupts ,how you can call it in program????
|
|
| Back to top |
|
 |
millwood
Joined: 02 Jul 2009 Posts: 121 Helped: 11
|
18 Aug 2009 21:39 interrupt service routine timer 2 c |
|
|
|
|
| Quote: |
| but if u have 2 interrupts ,how you can call it in program???? |
I think you should talk to your career counselor to see if you can find a living in a profession other than programming, preferably a profession that's much simpler and doesn't require much thinking and logic at all.
|
|
| Back to top |
|
 |
betwixt
Joined: 04 Jul 2009 Posts: 383 Helped: 62 Location: Wales, UK
|
18 Aug 2009 22:29 pic18 multiple interrupt c language |
|
|
|
|
AhmedS, despite the last comment, I will try to explain:
Interrupts are not called. They are triggered by an event happening in hardware.
I think what you are asking is "If I have more than one source of interrupts, how do I deal with them?"
The answer is that all interrupts in the 16F processors make the program call just one address, the interrupt vector. You write code in your interrupt routines that looks at each of the interrupt flags to see which is set. You then have to service whatever interrupt was found and also clear the interrupt flag before returning.
It is possible that more than one interrupt occurs simultaneously or that one occurs while another is being serviced. This does not matter because when you return to your main code the global interrupt will be turned on again (GIE bit) and the the other interrupt will immediately call for attention.
If you want to prioritize the interrupts, simply check the flags in the order needed so the highest priority is found first.
Brian.
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 5152 Helped: 766 Location: Bochum, Germany
|
18 Aug 2009 22:52 interrupt pic c language |
|
|
|
|
| It should be mentioned, that the code between the processor's hardware interrupt vector and the user accessible C interrupt function is strongly implementation dependant, and also handled different with different PIC16 compilers. Thus it doesn't make much sense to dicuss it without telling a particular compiler product.
|
|
| Back to top |
|
 |
arthur0
Joined: 28 Nov 2003 Posts: 73 Helped: 9 Location: Stockholm, Sweden
|
19 Aug 2009 8:00 calling interrupt in c |
|
|
|
|
Ah, what a waste...
| Quote: |
| but if u have 2 interrupts ,how you can call it in program???? |
I think (hope) you meant to ask "how would I name the second ISR...?"
If that's the case, the answer is that you can choose whatever name you please for your ISRs as long as you include the proper keyword that lets the compiler know it is an ISR.
From btbass' concise answer I gather that that keyword is interrupt. The name of the ISR in his example is isr.
If you had more interrupt vectors, you would've needed to specify the vector as well (different on different compilers), but the actual ISR name you choose at your discretion (say my_service_routine).
Does this clarify the matter?
Arthur
|
|
| Back to top |
|
 |
digitalkim
Joined: 27 Oct 2009 Posts: 7 Location: punjab
|
06 Nov 2009 6:50 Re: interrupts in C language code |
|
|
|
|
can anyone please post an example of c code using interrupts...?
any example with microcontroller
thanks
|
|
| Back to top |
|
 |
Google AdSense

|
06 Nov 2009 6:50 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
Slorn
Joined: 20 Oct 2009 Posts: 14 Location: Belgium
|
|
| Back to top |
|
 |