2 questions:
1) See http://www.keil.com/support/man/docs/c51/c51_le_bit.htm I don't understand the meaning of the following description. "Functions that disable interrupts (#pragma disable) and functions that are declared using an explicit register bank (using n) cannot return a bit value. The Cx51 Compiler generates an error message for functions of this type that attempt to return a bit type."
Can someone provide more explanagtion and example for my reference ? Thanks!
2) For interrupt, see http://www.keil.com/support/man/docs/c51/c51_le_interruptfuncs.htm "Interrupt function declarations may not include a return value. They must be declared as void (see the above examples). The compiler emits an error message if any attempt is made to define a return value for the interrupt function. The implicit int return value, however, is ignored by the compiler."
Why interrupt function(interrupt service routine, ISR) can not have return value? Should declare as void ?
Can someone tell me why ? Thanks!
Where exactly would they return it too?
The reason some functions can't return a bit is that said bit would be returned in the carry flag of the PSW, which the exit sequences for those types of functions clobber. Ultimately, it's because the register bank selector and the global interrupt disable are in the same register as the carry bit.
"Functions that disable interrupts (#pragma disable) and functions that are declared using an explicit register bank (using n) cannot return a bit value"
That's two clearly-defined cases in which you cannot return a bit value.
What further explanation do you require? What is unclear?
Hi,
I know the two cases restrict the bit return. I just want to know why. Could you have explanation about why ? Thanks!
ONE:
A return value of a bit cannot be used when the function requires a specific register bank because (within the processor) the register that controls the register bank selection also contains the place where a returned bit would be given.
The sequence takes the form:
Function_that_states_specific_bank_and_returns_bit PreserveRegisterThatSpecifiesRegisterBank SelectRegisterBank DoTheJob ConditionReturnValue_InCarry RestoreOriginalValueOfRegisterThatSpecifiesRegisterBank <<< This register contains the carry flag! Return
So, just consider it and you will see why the compiler doesn't like it.
Of course, the exit sequence of the compiler could be modified to accommodate the situation, but it would be rather inefficient. Keil obviously decided that it would be too costly a facility to support.
TWO:
If you want to write a function that returns a value, then that value would have to be taken by the called.
In the case of an interrupt, consider what calls the function and what would the return value be expected to be taken by.
If the value cannot be used (which it can't) then there is no reason to return any value. Hence the return type for that interrupt routine might as well be void.
The compiler simply enforces the obvious requirement.
About part 2: "Functions that disable interrupts (#pragma disable)"
has nothing to do with the fact that interrupt functions don't return a result. It's about normal functions, that return a value, but shall NOT be interrupted.
See more in http://www.keil.com/support/docs/1700.htm
It is about the temporary interrupt disable, that -by the way- must use the JBC instruction, the only one that can test and reset a bit in one uninterruptable instruction. (atomicy)
In the document you will see that the Carry flag is used to save the previous EA state. Other solutions are possible, but would create larger code. So, now as the carry flag is inside the PSW (as the two register bank flags in the part 1 of your question), the restriction has the same cause, bit functions are not possible, as they also need the carry flag as a return value.
Clear now?
You should have searched simply for "pragma disable" in the Keil pages and could have read it yourself.