|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Multi-Interrupt |
Posted: Wed May 18, 2005 12:58 pm |
|
|
Hi all, I need help.
I am new to PIC and microcontroller and I need some help from you all.
My question is: Can I use more than 1 interrupt in the same PIC? For
example: I want to use the interrupt in order as follow,
use the #INT_EXT to monitor pin RB0 -- >tied to light sensor,
use the #INT_RB to monitor pin RB4-7 -- > tied to 4 switches
use the #INT_TIMER0 to set time delay for alarm piezo.
use the #INT_USART to detect any incoming characters - to remote reseting purpose.
Can I use same PIC to do all those at the sametime?
Any body have a example code (layout format)to use multiple interrupt subroutines.
I did multiple interrupt search in this forum and found nothing relate.
Please Help...... |
|
|
valemike Guest
|
|
Posted: Wed May 18, 2005 1:43 pm |
|
|
Yes, you can have multiple interrupts.
Use the #priority directive, e.g.
#priority INT_EXT, INT_SSP
to determine which ISRs will be serviced first if there are more than one pending interrupts.
If you need to let certain interrupts interrupt other interrupts, then you'll need to devise your own isr using the fast interrupt directive. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Wed May 18, 2005 1:43 pm |
|
|
Sorry Valemike, you got in just before I did and covered much of what I said...:-)
Since you failed to mention what PIC and CCS version you are using you may have a High Priority interrupt to use.
Otherwise you have to determine what the highest priority is going to be then use the #priority pre-processor directive to set the order. I would recommend setting the USART interrupt at the highest priority.
In the case of the RB4-7, in the interrupt routine I would store the entire port into a byte, set a flag and exit. After you exit the interrupt, mask off and test the upper four bits in the input byte to determine which switche(s) where activated. You can also save the port byte and use it as a mask the next time to see which ones changed from the previous.
Just be sure to get out of any interrupt routine as quickly as possible!!
Last edited by dyeatman on Wed May 18, 2005 1:44 pm; edited 1 time in total |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Wed May 18, 2005 1:44 pm |
|
|
I have used multiple interrupts before with no problem. The interrupt handler used in compiler will take care of all the register saves and and flag clearing as needed. They will execute in the order listed in code (I beleive). _________________ -Matt |
|
|
Guest
|
|
Posted: Wed May 18, 2005 1:54 pm |
|
|
Yes, you can have multiple interrupts enabled.
No, nothing _ever_ happens at the 'same time'. A second interrupt will not be serviced, till the handler for the first interrupt finishes. This is one part of the reason, why keeping interrupt handlers _short_ is vital.
#Priority (or the order in which you define the interrupt routines), determined the order in which the handler will be tested when the interrupt occurs).
You can simply combine a code example, like the 'SISR' one, which is a generic serial handler, with one of the other examples, to see how multiple interrupts work.
The problem is that there is no such thing as a 'simple' example of this, and most will be dependant on the hardware involved. However this is part of a fairly generic serial handler using both receive and transmit interrupts. This requires defines for the UART registers and interrupt bits:
Code: |
//Buffer tests and handling
#define isempty(buff,free,in,out,size) (free>=size)
#define isfull(buff,free,in,out,size) (free<2)
#define tobuff(buff,free,in,out,size,chr) {buff[in++]=chr;\
--free;\
in=(in & (size-1));}
/* Sizes and numbers for the buffers */
/* sizes of the individual buffers */
#define SRBUFF (32)
#define STBUFF (32)
/* Each buffer uses a character array. Size is in the 'S' defines, and
the init_buff routine will have to be amended to add extra buffers if required */
int RSRcount,RSRin,RSRout;
int RSTcount,RSTin,RSTout;
unsigned int RSRbuff[SLBUFF+1],RSTbuff[STBUFF+1];
unsigned int RSTfrom()
{
/* Get character from the RS232 TX buffer */
static unsigned int temp;
temp=RSTout;
RSTout=(++RSTout) & (STBUFF-1);
temp=(RSTbuff[temp]);
RSTcount++;
return(temp);
}
#INT_TBE /* Transmit buffer empty interrupt */
void TXINT(void)
{
/* If characters are waiting in the buffer, the next one is transferred to the UART otherwise the interrupt is disabled, waiting for the next transmission. */
if (!isempty(RSTbuff,RSTcount,RSTin,RSTout,STBUFF))
{
putc(RSTfrom());
}
else
DISABLE_INTERRUPTS(INT_TBE); /* RS232 TX */
}
void tchar(unsigned int chr)
{
/* routine to send one character on the RS232.
This puts the specified character into the software transmit buffer
(if data is allready
being transmitted), or else sends the single character to the
RS232 UART. */
/* First check if the interrupt is enabled, and if not, write directly */
if (TXIE==0) && (isempty (RSTbuff,RSTcount,RSTin,RSTout,STBUFF)))
{
/* Wait if the TX buffer has not cleared */
while (TXIF==0) ;
/* send character */
TXREG=chr;
}
else
{
/* Hold transmission if the buffer is full */
while (isfull(RSTbuff,RSTcount,RSTin,RSTout,STBUFF)) {
if (TXIF==1) {
/* Here the transmit hardware buffer is empty */
TXREG=RSTfrom();
}
}
/* put character into the output buffer */
tobuff(RSTbuff,RSTcount,RSTin,RSTout,STBUFF,chr);
}
/* Enable interrupts */
enable_interrupts(INT_TBE);
}
#INT_RDA
void RDA_isr(void) {
//Here a character has been received.
tobuff(RSRbuff,RSRcount,RSRin,RSRout,SRBUFF,getc());
}
|
Best Wishes |
|
|
Guest
|
|
Posted: Wed May 18, 2005 2:35 pm |
|
|
What is the main() routine will look like. The manual does not show any example for #PRIORITY.
Does it look like this?
Code: |
void main()
{
enable_interrupts(global);
enable_interrupts(int_priority);
while(1)
{
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|