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.
Hello everyone,
I am currently having an issue with some code I have that seems to vary based on optimization:
In CommBuff ISR:
if (commData == 0x61) /* 0x61 = 'a' */ { if(DAC1DifVoltn==1) { DigPressure1 = DiffPressure1; // Enters this section, problem here } else { DigPressure1 = Pressure1>>3; } COMBUF = DigPressure1>>8; } else if (commData == 0x62) /* 0x62 = 'b' */ { COMBUF = DigPressure1; }
Currently, DAC1DifVoltn is set to 1. When the COMBUF register gets a 0x61, it updates DigPressure1 and puts out the MSB on the COMBUF. When it receives 0x62, it sends out the LSB bits.
The problem I'm having is with the line:
DigPressure1 = DiffPressure1;
DiffPressure1 is set to 0x0FF8. When optimization is set to 4-8, DigPressure1 is set to 0x0097. When it's set to 3, it gets 0x0F97. When it's set to 2-0, it gets the correct value (0x0FF8). Realistically, this has to do with the way my code is written rather than it being an assembly issue but I can't seem to see a better way to handle this.
Does anyone have any explanations as to what I can do? If you need me to post the assembly code, I can do that as well. Any help would be appreciated. Thanks!
The question I have is is this something that was done by Keil (since changing that checkbox fixed it) or is this something that was done/could have been avoided by me and if so, how? I'd like to be correct my programming practices if this is something I caused by missing an important programming step.
Here is how it works. The 8051 CPU supports multiple register banks. In assembly, you can access registers as R0...R7 in which case you get the current register bank (0...3). Or you can access them by absolute address: say, 0...7, which means you are accessing register bank 0 only.
www.keil.com/.../is51_ov_cpu8051variants.htm
So if the compiler uses absolute register accesses, it assumes that register bank 0 is selected. If such code is executed with a different bank selected, you'll get bugs like the one you experienced, since some parts of the code access currently selected register bank, and others access registers from a different bank.
http://www.keil.com/support/man/docs/c51/c51_le_regbankaccess.htm http://www.keil.com/support/man/docs/c51/c51_le_regbankspec.htm
a) which bank is this ISR using b) does this ISR call functions c) is any such function called from Main as well d) do you have interrupts of differnt priorities
and, finally, post the complete ISR you are running
Erik