We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Who can tell me the main usage of "using x" in interrupt service?
Assuming you are using C51: All main() loop (or background) functions use register bank 0 by default, let them. For all interrupts at a given priority set them to use register bank 1. If you have a standard 8051 with two interrupt priority levels you could use three register banks to reduce ISR vector latencies. For example, let all your normal function use register bank 0 (you don't need to specify using 0). Then, let's say you have a timer and the serial port at normal priority, define both of the ISR's with "using 1", and lastly if you had an external interrupt at high priority define its ISR with "using 2". E.g.
int main(void) { /* I'm using reg. bank 0 */ return 0; } void isrUart(void) interrupt UART_VECT using 1 { /* I'm using reg. bank 1 but I am ** mutually exclusive to isrTimer0(). */ } void isrTimer0(void) interrupt TMR0_VECT using 1 { /* I'm using reg. bank 1 but I am ** mutually exclusive to isrUart(). */ } void isrExt0(void) interrupt EXT0_VECT using 2 { /* I'm using reg. bank 2 an I can ** interrupt both isrUart() and ** isrTimer0(). */ }
I have another question. If there will be a confliction when I search a special "char" in a Array in main function and the Array is used in UART interrupt service? how about I use "using 0" in isr_UART()? For example: #define DATALTH 64 char SerBuffer[DATALTH]; char I_ptr; void main(void) { while(1) { if(!strchr(SerBuffer,'E')) break; } ..... ..... } void isr_UART(void) interrupt SIO_VECTOR using 3 { char c; if(RI) { c = SBUF; RI = FALSE; SerBuffer[I_ptr++ & (DATALENGTH -1)] = c; } }
There will be no conflict since neither the array nor the index are in registers. They are outside function scope and thus have static duration. You will however need to tell the C compiler that the variables may change without notice (that is, the background function will see the values change magically after the serial ISR occurs). Use volatile for this. Be sure to type every variable with data, idata, xdata, code, etc. That way you don't get any surprises if you change memory models. Try to use the SMALL model always.
#define SER_BUF_SIZE 64 volatile char xdata s_serBuf[SER_BUF_SIZE]; volatile char data bufIdx; void main(void) { while (strchr(s_serBuf, 'E')); ... } void isrUart(void) interrupt UART_VEC using 1 { if (RI) { RI = 0; s_serBuf[serIdx] = SBUF; serIdx = (serIdx + 1) % sizeof s_serBuf; } }