王锦平 发表于 2025-2-24 17:27:15

代码膨胀后,延时函数不起作用了 | xdata超了

<p>芯片:STC8C2K64S2</p>
<p><img src="data/attachment/forum/202502/24/172523du6mrsbsqxevmb1s.png" alt="image.png" title="image.png" /></p>
<p>在我尚未添加 <code>QRcode.c</code>跟随项目编译的时候,编译输出如下</p>
<pre><code class="language-text">Rebuild target 'Target 1'
assembling STARTUP.A51...
compiling Timer.c...
compiling UART.c...
compiling lcd_init.c...
compiling zk.c...
compiling lcd.c...
compiling protocol.c...
compiling model_bt.c...
compiling main.c...
source\main.c(44): warning C322: unknown identifier
source\main.c(196): warning C294: unreachable code
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_DISPLAY_GB2312_STRING?ZK
Program Size: data=15.2 xdata=881 code=14130
creating hex file from &quot;.\Objects\2025_02_10&quot;...
&quot;.\Objects\2025_02_10&quot; - 0 Error(s), 3 Warning(s).
Build Time Elapsed:00:00:01
</code></pre>
<p>在我添加了 <code>QRcode.c</code>以后,编译输出如下</p>
<pre><code class="language-text">Rebuild target 'Target 1'
assembling STARTUP.A51...
compiling Timer.c...
compiling UART.c...
compiling lcd_init.c...
compiling zk.c...
compiling lcd.c...
compiling QR_Encode.c...
compiling protocol.c...
compiling model_bt.c...
compiling main.c...
source\main.c(44): warning C322: unknown identifier
source\main.c(196): warning C294: unreachable code
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_DISPLAY_GB2312_STRING?ZK
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_ENCODEDATA?QR_ENCODE
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_ISCHINESEDATA?QR_ENCODE
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_CHINESETOBINALY?QR_ENCODE
Program Size: data=95.2 xdata=2284 code=30135
creating hex file from &quot;.\Objects\2025_02_10&quot;...
&quot;.\Objects\2025_02_10&quot; - 0 Error(s), 6 Warning(s).
Build Time Elapsed:00:00:01
</code></pre>
<h1>然后我的延时函数就失效了?</h1>
<p>我的延时是依靠定时器计算的。代码如下</p>
<pre><code class="language-C">void Timer3_Isr(void) interrupt 19
{
    cnt++;
    return;
}

/**
* @brief 定时器3初始化
* @param us
*/
int Timer3Init(unsigned long us)
{
    unsigned long timerCnt = 0UL;
    float timerCnt_f = 0.0;
    unsigned int tm3ps = 1;
    timerCnt_f = TIMER_MAX_COUNT - (FOSC * us / CONSTANT / 1.0);
    timerCnt = timerCnt_f;
    if ( timerCnt &gt; MAX_UINT )
    {
      SET_BIT(P_SW2, 7); // 需要先开启扩展功能
      if ( !FIND_BIT(P_SW2, 7) )
            return TIMER3_INIT_FAIL;
      TM3PS = 0U; // 初始化预分频
      // 增加预分频系数直到重载值不超过0xFFFF
      do
      {
            tm3ps = tm3ps + 1;
            timerCnt_f = TIMER_MAX_COUNT - (FOSC * us / CONSTANT / tm3ps);
            timerCnt = timerCnt_f;
      } while ( timerCnt &gt; MAX_UINT );
      TM3PS = tm3ps - 1;// 增加预分频系数
    }
    CLEAR_BIT(T4T3M, 0);// Timer3, not output
    SET_BIT(T4T3M, 1);// Timer3 set mode 1T
    CLEAR_BIT(T4T3M, 2);// Timer3 set mode count
#if TIMER_CHECK
    t3 = timerCnt;
    t3_f = timerCnt_f;
#endif
    T3L = (unsigned char)(timerCnt &amp; 0xff);//Initial timer value
    T3H = (unsigned char)((timerCnt &gt;&gt; 8) &amp; 0xff);//Initial timer value
    SET_BIT(IE2, 5);// Enable timer3 interrupt
    return TIMER3_INIT_OK;
}
/**
* @brief 延时
* @param target 根据初始化来输入
*/
int UserDelay(const unsigned int target)
{
    cnt = 0;
    SET_BIT(T4T3M, 3);// Timer3 run
#if 0
    while ( cnt != target )
      WDT_RESET();// 防止因为延时导致WDT超时
#else
    while ( cnt &lt; target )
    {
      if ( cnt % 300 == 0 )
            WDT_RESET();
    }
#endif
    CLEAR_BIT(T4T3M, 3);// Timer3 stop
    return cnt;
}
#ifdef TIMER_DEFAULT_US
#define DEFAULT_DALEY_INIT() Timer3Init(TIMER_DEFAULT_US)
#define Delay_1ms() UserDelay(1U)
#define Delay_10ms() UserDelay(10U)
#define Delay_30ms() UserDelay(30U)
#define Delay_50ms() UserDelay(50U)
#define Delay_100ms() UserDelay(100U)
#define Delay_1s() UserDelay(1000U)
#endif // TIMER_DEFAULT_US
</code></pre>
<p>在我代码膨胀前,定时器都是有效且稳定可用的,但在膨胀后延时给我的感觉是起不了一点效果了。</p>
<p>希望有大神能帮我粗浅分析延时不起效果的原因???</p>
<p>由于手头上没有逻辑分析仪,Debug都是依赖UART来判断的。</p>

_奶咖君_ 发表于 2025-2-24 17:34:13

<p>内存超了<img alt="cahan" class="emoji" src="https://www.stcaimcu.com/static/image/smiley/default/cahan.gif" title="cahan" /></p>

soma 发表于 2025-2-24 18:17:16

xdata超了,只有2048,你程序有2284。

Program Size: data=95.2 xdata=2284 code=30135

王锦平 发表于 2025-2-24 18:44:59

<h1>感谢各位大大的回复</h1>
<p>还是我太菜了,都没用懂 keil5<img alt="shuai" class="emoji" src="https://www.stcaimcu.com/static/image/smiley/default/shuai.gif" title="shuai" /></p>
页: [1]
查看完整版本: 代码膨胀后,延时函数不起作用了 | xdata超了