void LongLatPrintf(unsigned char row,unsigned char col, signed int du, unsigned int fen,float miao) { unsigned char T[2]={0},D[4]={0},P[6]={0},Q[3]={0}; unsigned int temp=0; temp = abs(du); sprintf(D,"%d",temp);//度 if (temp < 10) { D[3] = D[0]; D[2] = ' '; D[1] = ' '; } else if (temp >= 10 && temp <100) { D[3] = D[1]; D[2] = D[0]; D[1] = ' '; } else if (temp >= 100 && temp <= 180) { D[3] = D[2]; D[2] = D[1]; D[1] = D[0]; } if (du < 0) D[0] ='-'; else D[0] =' '; D[4] = 0X00; PrintFunc(3,row,col,D);//度 SignDegree(row,col+25);//度符号 sprintf(T,"%d",fen); if (fen<10) { T[1]=T[0]; T[0]=' '; T[2]=0X00; } PrintFunc(3,row,col+28,T);//分值 SignFen(row,col+41); sprintf(P,"%f",miao); // P[6] = 0X00; if (miao<10) { T[0]=' '; T[1]=P[0]; Q[0]=P[2]; Q[1]=P[3]; Q[2]=P[4]; Q[3]=0X00; } else { T[0] = P[0]; T[1] = P[1]; Q[0] = P[3]; Q[1] = P[4]; Q[2] = P[5]; Q[3] = 0X00; } PrintFunc(3,row,col+42,T); SignFull(row,col+55); PrintFunc(3,row,col+58,Q); } It is one subroutine of my program,which function is displaying LCD .In this subroutine ,the varivble T[2],D[4],ect,is just defined when the subroutine running .But in my project , these variable occupy the RAM alltimes like static variable.How can I get it free after the subroutine finished?
See the sections in the manual about the compile-time stack analysis, reentrant keyword, and the OVERLAY directive. The Keil C compiler will normally analyze the call tree of a program and assign absolute memory addresses to the automatic variables. This is more efficient than the typical approach of allocating the locals on a stack. It can, however, cause problems with reentrant functions. The compiler will re-use the memory assigned to these locals in other branches of the call tree. The memory is not permanently dedicated to one routine. The OVERLAY directive allows you to edit the call tree to control which functions have their auto variables overlaid. You can, if you want, except functions from this process so that their memory areas won't be reused. Or, you can adjust the call tree to inform the compiler of the actual overlay needs, which is often needed when using function pointers.
This code appears wasteful in various ways. For starters, why don't you just use the flexibility the sprintf() command has, instead of rolling your own complicated scheme like that? All the shuffling of characters inside the strings could easily be avoided by using "% d" or "% f" as the format strings, from what I can see.