gqzjl 发表于 2024-12-8 20:57:15

【求助】8H内部32K时钟误差问题 | 用外部32768RTC晶振

<p>使用的代码是我从ai-isp中下的,修改了一点(设置时钟为内部高精度IRC、启用内部32K低速IRC)</p>
<p>测试下来一分钟差了10秒左右,各位大佬能帮忙想想办法吗</p>
<pre><code>#include &quot;STC8H.h&quot;
#include &quot;intrins.h&quot;
#include &quot;stdio.h&quot;


sfr   TH2   =   0xD6;
sfr   TL2   =   0xD7;


#define MAIN_Fosc   22118400L
#define Baudrate    115200L
#define TM          (65536 -(MAIN_Fosc/Baudrate/4))

bit B1S_Flag;

void RTC_config(void);


void Delay100ms(void)        //@24.000MHz
{
        unsigned char data i, j, k;

        _nop_();
        _nop_();
        i = 13;
        j = 45;
        k = 214;
        do
        {
                do
                {
                        while (--k);
                } while (--j);
        } while (--i);
}


void UartInit(void)
{
    SCON = (SCON &amp; 0x3f) | 0x40;
    TL2= TM;
    TH2= TM&gt;&gt;8;
    AUXR |= 0x15;
}

void UartPutc(unsigned char dat)
{
    SBUF = dat;
    while(TI==0);
    TI = 0;
}

char putchar(char c)
{
    UartPutc(c);
    return c;
}

void RTC_Isr() interrupt 13
{
    char store;

    store = P_SW2;
    P_SW2 |= 0x80;                              //使能XFR访问

    if(RTCIF &amp; 0x08)                            //判断是否秒中断
    {
      RTCIF &amp;= ~0x08;                         //清中断标志
      B1S_Flag = 1;
    }

    P_SW2 = store;
}

void main(void)
{
    P0M1 = 0;   P0M0 = 0;                     //设置为准双向口
    P1M1 = 0;   P1M0 = 0;                     //设置为准双向口
    P2M1 = 0;   P2M0 = 0;                     //设置为准双向口
    P3M1 = 0;   P3M0 = 0;                     //设置为准双向口
    P4M1 = 0;   P4M0 = 0;                     //设置为准双向口
    P5M1 = 0;   P5M0 = 0;                     //设置为准双向口
    P_SW2 |= 0x80;                              //使能XFR访问

    UartInit();
X32KCR=0x00;
HIRCCR|=0xff;
//while(HIRCCR&amp;0x01);
IRC32KCR|=0xff;
while(IRC32KCR&amp;0x01);
Delay100ms();
    RTC_config();
    EA = 1;
    printf(&quot;STC8H4K64TLR RTC Test Programme!\r\n&quot;); //UART发送一个字符串

    while (1)
    {
      if(B1S_Flag)
      {
            B1S_Flag = 0;
            P_SW2 |= 0x80;                      //使能XFR访问

            printf(&quot;Year=20%bd&quot;, YEAR);
            printf(&quot;Month=%bd&quot;, MONTH);
            printf(&quot;Day=%bd&quot;, DAY);
            printf(&quot;Hour=%bd&quot;, HOUR);
            printf(&quot;Minute=%bd&quot;, MIN);
            printf(&quot;Second=%bd&quot;, SEC);
            printf(&quot;\r\n&quot;);

            P_SW2 &amp;= ~0x80;                     //禁止XFR访问
      }
    }
}

void RTC_config(void)
{
    INIYEAR = 21;                               //Y:2021
    INIMONTH = 12;                              //M:12
    INIDAY = 31;                              //D:31
    INIHOUR = 23;                               //H:23
    INIMIN = 59;                              //M:59
    INISEC = 50;                              //S:50
    INISSEC = 0;                              //S/128:0
    RTCCFG |= 0xff;                           //触发RTC寄存器初始化

    RTCIF = 0;                                  //清中断标志
    RTCIEN = 0x08;                              //使能RTC秒中断
    RTCCR = 0x01;                               // RTC使能
   // P_SW2 &amp;= ~0x80;                           //禁止XFR访问
}

/*

;将以下代码保存为ASM格式文件,一起加载到项目里,例如:isr.asm

    CSEG    AT0123H
    JMP   006BH
    END

*/


</code></pre>

gqzjl 发表于 2024-12-8 20:59:36

<p>顶一下<img alt="dizzy" class="emoji" src="https://www.stcaimcu.com/static/image/smiley/default/dizzy.gif" title="dizzy" /></p>

DebugLab 发表于 2024-12-8 21:40:40

内部32K

正常的
作为掉电唤醒定时器
低功耗
无抗温飘压飘电路
无校准
制造误差大
正常

DebugLab 发表于 2024-12-8 21:42:48

RTC想误差小必须用外部32768Hz晶振

QQ624353765 发表于 2024-12-9 07:35:49

下载程序的时候能看到内部32K时钟的实际频率,差不多有35KHZ,你可以做一个误差纠正,每隔1分钟纠正一次
就算你使用外部32k高精度时钟源那也是有误差的,这个误差通过纠正后效果是非常好的

xxkj2010 发表于 2024-12-9 08:06:56

<p>想做电子钟用或需要相对准确的场合,请使用外部晶振吧。</p>

gqzjl 发表于 2024-12-9 21:11:17

<p><img alt="shuai" class="emoji" src="https://www.stcaimcu.com/static/image/smiley/default/shuai.gif" title="shuai" /> STC啥时能出个内置带温补晶振的片子</p>

梁工 发表于 2024-12-10 22:21:24

gqzjl 发表于 2024-12-9 21:11
STC啥时能出个内置带温补晶振的片子

内部高速时钟就是带温补的。
页: [1]
查看完整版本: 【求助】8H内部32K时钟误差问题 | 用外部32768RTC晶振