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.
Hi,
I need to put a statement in a interrupt routine to shift a variable right one-bit. Like this:
dat >>=1;
It generated assembly code like this:
MOV A,dat CLR C RRC A MOV dat,A
What statement in C language sould be written to complete the shift operation but not effect the "C,carry" bit?
This is to save the "PUSH PSW", "POP PSW" instruction in my ISR. The inline assembly statement is not good to me, since I am not familar with it.
Thanks, John
Yes, it is not a good way in general application. But by strict ISR time requirement, I'm forced to find a way to save the time as more as I can.
P.S. 1. This ISR is define to be the only one high priority interrupt.
2.PUSH and POP cost total 48 cycle on my system, it takes nearly 1.2 microsecond(40MHz,12T), but this ISR limited to complete task in 20 microsecond.And it happened to be the module of system with most time-sensitive.
I'm forced to find a way to save the time as more as I can.
In that case, you'll have to dive into assembly.
The compiler does several things that usually don't mix well with tight timing constraints in interrupts.
For example, it always locates the ISR somewhere in program memory, and performs a LJMP from the interrupt vector table to the start address of the ISR, even though there are 8 bytes available in the vector table that can hold a complete (yet short) interrupt service routine.
In that case, you'll have to dive into assembly. I agree now. It is the last thing I can do after I re-contrust the data structure to eliminate more control statement.
So far, it is hard to keep the state machine tight and clear.
"2.PUSH and POP cost total 48 cycle on my system, ..."
Given your tight timing budget, writing the whole ISR in assembly seems really to be your best way.
The context saving/restore code can be effectively optimized if you use banked registers. By selecting the ISR bank in RS1:RS0 you save 8 push/pop instructions. If your ISR handler can be written using only the 8 banked registers, you don't even need to set up local variables, since the ISR banked regs will hold their value between interrupts.
About the RR/RRC: Using
CLR C RRC A
takes one program byte less than
RR A ANL A, #0x7F
... and you can always save the CLR C by preceding the RRC A with an operation that guarantees leaving the carry cleared.