- 打卡等级:常住居民I
- 打卡总天数:62
- 最近打卡:2025-06-24 21:59:31
中级会员
- 积分
- 431
|
刚学STC8H8K单片机关于RTC例程,下面代码是官方源代码,
代码应该是每秒更新一次年月日,结果如果图:
不理解,希望老师们指点。
#include <STC8H.h>
#include "intrins.h"
#include "stdio.h"
#define MAIN_Fosc 22118400L
#define Baudrate 115200L
#define TM (65536 -(MAIN_Fosc/Baudrate+2)/4)
bit B1S_Flag;
void RTC_config(void);
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
{
if(RTCIF &0x08)
{
RTCIF &=~0x08;
B1S_Flag = 1;
}
}
void main(void)
{
P_SW2 |=0x80;
P0M1 = 0; P0M0 = 0; //设置为准双向口
P1M1 = 0; P1M0 = 0; //设置为准双向口
P2M1 = 0; P2M0 = 0; //设置为准双向口
P3M1 = 0; P3M0 = 0; //设置为准双向口
P4M1 = 0; P4M0 = 0; //设置为准双向口
P5M1 = 0; P5M0 = 0; //设置为准双向口
UartInit();
RTC_config();
EA = 1;
printf("STC8H4K64TLR RTC Test Programme!\r\n"); //UART发送一个字符串
while (1)
{
if(B1S_Flag)
{
B1S_Flag = 0;
printf("Year=20%bd ", RTCYEAR);
printf("Month=%bd ", RTCMONTH);
printf("Day=%bd ", RTCDAY);
printf("Hour=%bd ", RTCHOUR);
printf("Minute=%bd ", RTCMIN);
printf("Second=%bd ", RTCSEC);
printf("\r\n");
}
}
}
void RTC_config(void)
{
X32KCR = 0XC0;
while(!(X32KCR & 0x01));
RTCCFG &=~0x02;
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 |= 0x01; //触发RTC寄存器初始化
RTCIF = 0; //清中断标志
RTCIEN = 0x08; //使能RTC秒中断
RTCCR = 0x01; // RTC使能
}
|
-
|