使用的代码是我从ai-isp中下的,修改了一点(设置时钟为内部高精度IRC、启用内部32K低速IRC)
测试下来一分钟差了10秒左右,各位大佬能帮忙想想办法吗
#include "STC8H.h"
#include "intrins.h"
#include "stdio.h"
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 & 0x3f) | 0x40;
TL2 = TM;
TH2 = TM>>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 & 0x08) //判断是否秒中断
{
RTCIF &= ~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&0x01);
IRC32KCR|=0xff;
while(IRC32KCR&0x01);
Delay100ms();
RTC_config();
EA = 1;
printf("STC8H4K64TLR RTC Test Programme!\r\n"); //UART发送一个字符串
while (1)
{
if(B1S_Flag)
{
B1S_Flag = 0;
P_SW2 |= 0x80; //使能XFR访问
printf("Year=20%bd ", YEAR);
printf("Month=%bd ", MONTH);
printf("Day=%bd ", DAY);
printf("Hour=%bd ", HOUR);
printf("Minute=%bd ", MIN);
printf("Second=%bd ", SEC);
printf("\r\n");
P_SW2 &= ~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 &= ~0x80; //禁止XFR访问
}
/*
;将以下代码保存为ASM格式文件,一起加载到项目里,例如:isr.asm
CSEG AT 0123H
JMP 006BH
END
*/