??? 07/04/04 18:12 Read: times |
#73601 - Advantage of polling Responding to: ???'s previous message |
Sometimes it is useful to use polling instead interrupts.
E.g. if the baud rate was very high, it might be important, that polling need less time as interrupts, because then it is not needed to push and pop and also to determine if it was a receive or transmit interrupt. Follwing a listing of both, which points out, that the interrupt need about 100% more time: stmt level source 1 #pragma cd 2 #include<reg51.h> 3 #include<intrins.h> 4 5 6 #define RX_SIZE 64 7 8 unsigned char idata *rx_in; // receive pointer 9 unsigned char idata *rx_out; 10 unsigned char idata *rx_end; 11 bit sti; // software TI bit 12 13 14 void uart_poll(void) 15 { 16 1 if( RI ){ 17 2 RI = 0; 18 2 *rx_in = SBUF; 19 2 rx_in++; 20 2 if( rx_in == rx_end ) 21 2 rx_in -= RX_SIZE; 22 2 } 23 1 } // 15 cycle 24 25 26 void uart_int(void) interrupt 4 27 { 28 1 if( RI ){ 29 2 RI = 0; 30 2 *rx_in = SBUF; 31 2 rx_in++; 32 2 if( rx_in == rx_end ) 33 2 rx_in -= RX_SIZE; 34 2 } 35 1 if( TI ){ 36 2 TI = 0; 37 2 sti = 1; 38 2 } 39 1 } // 31 cycle 40 ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION uart_poll (BEGIN) ; SOURCE LINE # 14 ; SOURCE LINE # 14 ; SOURCE LINE # 15 ; SOURCE LINE # 16 0000 309811 JNB RI,?C0003 ; SOURCE LINE # 17 0003 C298 CLR RI ; SOURCE LINE # 18 0005 A800 R MOV R0,rx_in 0007 A699 MOV @R0,SBUF ; SOURCE LINE # 19 0009 0500 R INC rx_in ; SOURCE LINE # 20 000B E500 R MOV A,rx_in 000D B50004 R CJNE A,rx_end,?C0003 ; SOURCE LINE # 21 0010 24C0 ADD A,#0C0H 0012 F500 R MOV rx_in,A ; SOURCE LINE # 22 ; SOURCE LINE # 23 0014 ?C0003: 0014 22 RET ; FUNCTION uart_poll (END) ; FUNCTION uart_int (BEGIN) 0000 C0E0 PUSH ACC 0002 C0D0 PUSH PSW 0004 C000 PUSH AR0 ; SOURCE LINE # 26 ; SOURCE LINE # 28 0006 309811 JNB RI,?C0004 ; SOURCE LINE # 29 0009 C298 CLR RI ; SOURCE LINE # 30 000B A800 R MOV R0,rx_in 000D A699 MOV @R0,SBUF ; SOURCE LINE # 31 000F 0500 R INC rx_in ; SOURCE LINE # 32 0011 E500 R MOV A,rx_in 0013 B50004 R CJNE A,rx_end,?C0004 ; SOURCE LINE # 33 0016 24C0 ADD A,#0C0H 0018 F500 R MOV rx_in,A ; SOURCE LINE # 34 001A ?C0004: ; SOURCE LINE # 35 001A 309904 JNB TI,?C0007 ; SOURCE LINE # 36 001D C299 CLR TI ; SOURCE LINE # 37 001F D200 R SETB sti ; SOURCE LINE # 38 ; SOURCE LINE # 39 0021 ?C0007: 0021 D000 POP AR0 0023 D0D0 POP PSW 0025 D0E0 POP ACC 0027 32 RETI ; FUNCTION uart_int (END) Also the polling can be useful, if no intererupts possible, e.g. inside a bootloader on such devices, which not allow to redirect the interrupt vectors into the bootloader space. Peter |