1
\$\begingroup\$

I have an 8051 MCU which I use keil to program it with C. My problem is that the micro-controller restarts at the end of some functions while they should return.

Using a debugger I tried to check it line by line. And I figured out the the MCU is not restated right after the end of the function. The PC(progam counter) goes to somewhere else (sometime somewhere that there is not any code to execute) and then it increases one by one until the end of code. It is very strange for me because I write my code in C and never seen the type of error previously.

I think this problem might be related to the compiler settings and thus I post the settings here:

Assembler settings: XR GEN DB EP NOMOD51

Compiler settings: DB OE BR LARGE OPTIMIZE(11, SIZE) DEFINE(SILABS_PLATFORM_WMB912) DEFINE(SILABS_WMCU_SI106x)

Linker settings: CODE REMOVEUNUSED CLASSES(BIT(0x20.0-0x2F.7), DATA(D:0-D:0x7F), IDATA(I:0-I:0xFF), XDATA(X:0-X:0xFFF), CODE(C:0-C:0xFBFF), CONST(C:0-C:0xFBFF))

I tried both large and small settings but the problem remains. Kindly help me if you know the reason.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you post the exact MCU model you're using? That would be helpful as well as the code you're trying to run. \$\endgroup\$ – georgjz Mar 18 '18 at 19:03
  • 1
    \$\begingroup\$ Seeing the code would help people answer. It could be that it's overwriting the memory locations needed for the return. \$\endgroup\$ – Bob Jacobsen Mar 18 '18 at 19:08
  • 1
    \$\begingroup\$ Does it crash if you go into the function or when you return? The latter most likely cause is an out-of-bounds write to a local array. \$\endgroup\$ – Oldfart Mar 18 '18 at 19:09
  • 1
    \$\begingroup\$ Seems to be the same story you told earlier: electronics.stackexchange.com/questions/363239/… . Is this a different question? \$\endgroup\$ – jonk Mar 18 '18 at 19:22
  • \$\begingroup\$ 1drv.ms/u/s!AivB_SrRBF80bPzWuZuWEj7f9g8 My project can be found. The mcu model is SI1064. Yes I wrote another question because I made some new improvements and wanted to ask with this conditions. @georgjz \$\endgroup\$ – amir mohamad hatami Mar 18 '18 at 19:31
2
\$\begingroup\$

I found out the reason. It is actually the problem of the compiler. The keil C51 v9.53 has a problem that sometimes it mixes the stack with data. They have proposed a new linker. But I upgraded to v 9.55 and the problem solved.

\$\endgroup\$
  • \$\begingroup\$ You can mark this as the accepted answer to indicate the solution. \$\endgroup\$ – Transistor Jul 9 '18 at 6:03
1
\$\begingroup\$

You’re probably overwriting the stack, thereby corrupting the return address.

It would be good to see the actual code, not just a sketch. But one possible cause is passing an argument of the wrong size or type, so that the routine you’re calling thinks it’s writing to somewhere safe but is actually writing outside what you provided with the call.

It’s possible that there’s not enough stack space, too. Particularly if you’re calling a complicated function that calls other things.

But without real detail, it’s hard to say more.

\$\endgroup\$
  • \$\begingroup\$ Thank u. I posted a link in previous comments which contains my full project. \$\endgroup\$ – amir mohamad hatami Mar 21 '18 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.