I am using Analog Device ADuC848, which has 4 kbyte on-chip nonvolatile data flash. Because each page of this data memory has 4 bytes, so I define
typedef struct { UINT8 value; UCHAR unused[3]; } NVM_SingleByte_T;
I use quite a lot of NVM_SingleByte_T to define variables throughout my program. Now I have a very strange problem. A subroutine as below:
void cmdhandler(void) {NVM_SingleByte_T tmpByte; tmpByte.value = 1; }
this routine will trigger my relay which is controlled by GPIO. And if I change tmpByte.value to other number not equal to 1, it will not trigger relay. This cmdhandler has nothing to do with relay control, so how come this local variable affecting other part of my program? Thanks in advance
I found out that if I change code optimization level below 4(Register variables), I don't have this problem, and at level 4 and above, relay starts clicking.
Anyone know what Keil is doing with that optimization?
If a local variable affects anything else it can be you ran out of RAM. Or, a pointer is not correct.
that cmdhandler is very small as you can see, only 1 line, and there is no pointer. So I am not sure your explanation is right... plus, how come there is no problem when I am not using using optimizer?
Who said the problem is in cmdhandler Arrays , passing arrays, passing structs all use pointers. un-initialized variables can also cause issues with changes of optimization levels. Otherwise insure the compiler is upto date. Look at the asm output by the compiler. Make a small project with the issue (Not a code snippet) and send it to support
un-initialized variables can also cause issues with changes of optimization levels.
What a nasty "optimization effect" (NOT an optimizer error). An uninitialized overlaid variable could have a different content at function entry when a program is optimized.
Erik
especially if the uninitialised variable happens to be a pointer...