当flash溢出时,如何给程序瘦身

当选定了一款MCU,产品的开发一切都很顺利,快到收尾时猛然间发现Flash发生了溢出错误,这是很悲催的事情,总不能再更换芯片吧,我不想这么麻烦,老板更不希望项目的进度跟不上。没办法只能给程序进行瘦身运动了。

Error[e104]: Failed to fit all segments into specified ranges. Problem discovered in segment CODE. Unable to place 84 block(s) (0x3f68 byte(s) total) in 0x3dee byte(s) of memory. The problem occurred  while processing the segment placement command "-P(CODE)CODE=C000-FFDF", where at the moment of placement the available memory ranges were "CODE:c1f2-ffdf"

1、菜单程序中使用了大量的条件判断语句

if( SetOptionsFlag == PresetTemp ) /* 是否进入预置温度设置模式 */
{ ... }
else if(SetOptionsFlag == PresetTemp3th)/* 第三次定时加热预置温度 */
{ ... }
else if(SetOptionsFlag == PresetTemp2th)/* 第二次定时加热预置温度 */
{ ... }
else if(SetOptionsFlag == PresetTemp1th)/* 第一次定时加热预置温度 */
{ ... }
... ..
else
/* 不在设置模式时温控模块显示 */ { ... }

把if...else if...else 语句 换成 swith()语句

之间的if...else if...else语句占用Flash 4496byte;换成switch语句后程序占用4328byte。节省flash空间168KB。当有很多分支选择时最好采用switch语句,当然,如果的你flash足够大,那就无所谓了。

  当然,给程序瘦身最重要的一点应该是在不影响程序执行效率的前提下,能写成子程序的模块尽量写成单独的子程序,每当应用程序涉及到此模块的时候就去调用相关的子程序函数,这样能很大程度上节省代码空间,尤其实在程序分支较多的情况下。

 

posted @ 2013-05-10 10:59  此岸幸福  阅读(1023)  评论(0编辑  收藏  举报