SM59R RTC 功能使用方法 RTC(实时时钟)功能使用方法 1 适用产品:SM59R16A5/ SM59R09A5/ SM59R05A5 2 RTC 使用说明: 1. 建内一组 RTC,时间具有秒、分、时、日、星期、月、年。 2. RTC 晶振选用 32.768KHz,匹配电容建议使用 25pF,或依实际晶振做调教。 3. 二种中断,分别,为 period interrupt 和 ALARM interrupt,且共享 interrupt vector(8Bh)。 32768Hz devider RTC counter Second (0~59) RTC counter Minute (0~59) RTC counter Hour (0~23) RTC counter Day (1~31) RTC counter Month (1~12) RTC counter Year (00~99) RTC counter Week Alarm INT Alarm Reg. (Min, Hour, Day, Week, Month) Periodical INT Counter (0.5 Sec) Periodical INT Counter (1 Sec) Periodical INT Counter (0.5Min) Compare logic Periodical INT Counter (1 Min) Periodical INT Counter (0.5 Hour) Periodical INT Counter (1 Hour) Periodical INT 实时时钟与相关缓存器控制图(一) 实时时钟与相关缓存器控制图(二) Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 1 Ver.A 2010/06 SM59R RTC 功能使用方法 4. RTC 功能在硬件规划时必须注意,依系统选用内振和外振会有差异,如下说明: 使用 RTC 功能, 且系统使用外部晶振时: 使用 RTC 功能,且系统使用内部晶振时: RTC 晶振则使用#16 及#17 RTC 晶振则使用#18 及#19 系统晶振脚位使用#18 和#19 建议电路如下图: 建议电路如下图: 16 17 18 FRTC 19 X32OUT/WR/P3.6 X32IN/RD/P3.7 X32OUT/P5.4/XTAL2 X32IN/P5.5/XTAL1 20 3 RTC 相关缓存器: Mnemonic Description Direct Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET RTC RTCADDR RTC 地址 9Eh RTCDATA RTC 数据 9Fh Mnemonic Description Address - - - - RTCADDR[3:0] 00H RTCDATA[7:0] Bit 7 Bit 6 Bit 5 00H Bit 4 Bit 3 ALARMIF PeriodIntEn Bit 2 Bit 1 Bit 0 RESET RTC function RTCCTRL RTC 控制缓存器 00h RTCen SEC RTC 秒缓存器 01h - MIN PeriodIF SEC[6:4] RTC 分缓存器 02h - HOUR RTC 时缓存器 03h - - DAY RTC 日缓存器 04h - - RTC 星期缓存器 05h - - WEEK ALARMInten MIN[6:4] HOUR[5:4] DAY[5:4] - INT_SEL[2:0] SEC[3:0] - - 00h 00h MIN[3:0] 00h HOUR[3:0] 00h DAY[3:0] 01h WEEK[2:0] Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 2 Ver.A 2010/06 06h SM59R RTC 功能使用方法 MONTH RTC 月缓存器 06h YEAR RTC 年缓存器 07h MINen HOURen - ALMMIN - - - MONTH[4] YEAR[7:4] RTC 分示警 08h ALMHOUR RTC 时示警 09h ALMDAY RTC 日示警 10h DAYen - RTC 星期示警 11h WEEKen - - - RTC 月示警 12h - - ALMMONTH[4] ALMWEEK ALMMONT H Mnemonic: RTCCTRL 7 6 RTCen PeriodIF ALMMIN[6:4] MONTHe n 5 ALARMInten 4 ALARMIF ALMHOUR[5:4] ALMDAY[5:4] 3 PeriodIntEn - MONTH[3:0] 01h YEAR[3:0] 00h ALMMIN[3:0] 00h ALMHOUR[3:0] 00h ALMDAY[3:0] 00h ALMWEEK[2:0] ALMMONTH[3:0] 2 1 INT_SEL[2:0] Address: 00H 0 Reset 00h RTCen: RTC 电路启动位 0: 禁止 1: 启动 PeriodIF: 周期性中断旗标位 1: RTC 中断由周期性中断旗标位产生.他必须由软件清零. ALARMInten: 示警中断启动位 当示警数值与RTC数值相等时将产生示警中断. 0: 示警中断禁止 1: 示警中断启动 ALARMIF: 示警中断旗标位 1: RTC 中断由示警中断产生.他必须由软件清零. PeriodIntEn: 周期性中断启动位 0: 禁止 1: 启动 INT_SEL[2:0]: RTC 周期性中断之周期由INT_SEL[2:0]选择 000: 设定为每经过0.5秒产生周期性中断 001: 设定为每经过1秒产生周期性中断 010: 设定为每经过0.5分产生周期性中断 011: 设定为每经过1分产生周期性中断 100: 设定为每经过0.5小时产生周期性中断 101: 设定为每经过1小时产生周期性中断 附注: 周期性中断的计数将从RTC缓存器写入的值开始计数. 举例来说,现在时间为 3:23,我们设定INT_SEL[2:0]为0.5小时;第一个周期性中断将在 3:53产生,不是3:30。 Mnemonic: SEC 7 6 5 SEC[6:4] 4 3 2 1 SEC[3:0] Address: 01H 0 Reset 00H SEC[6:0]: 以BCD格式显示目前秒数,区间为0~59秒. SEC[6:4]: 此3位代表秒之十位. SEC[3:0]: 此4位代表秒之个位. Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 3 Ver.A 2010/06 00h 00h SM59R RTC 功能使用方法 Mnemonic: MIN 7 6 5 MIN[6:4] 4 MIN[3:0] Address: 02H 0 Reset 00H 3 2 1 HOUR[3:0] Address: 03H 0 Reset 00H 3 2 3 2 1 MIN[6:0]: 以BCD格式显示目前分钟数,区间为0~59分钟. MIN[6:4]: 此3位代表分钟之十位. MIN[3:0]: 此4位代表分钟之个位. Mnemonic: HOUR 7 6 5 4 HOUR[5:4] HOUR[5:0]: 以BCD格式显示目前时数,区间为0~23小时. HOUR[5:4]: 此2位代表时之十位. HOUR[3:0]: 此4位代表时之个位. Mnemonic: DAY 7 6 5 4 DAY[5:4] 1 DAY[3:0] Address: 04H 0 Reset 00H DAY[5:0]: 以BCD格式显示目前日期,区间为0~31日. DAY[5:4]: 此2位代表日期之十位. DAY[3:0]: 此4位代表日期之个位. Mnemonic: WEEK 7 6 5 4 3 2 1 WEEK[2:0] Address: 05H 0 Reset 00H WEEK[2:0]: 显示目前星期. 此缓存器必须由使用者自行填入,并不会于填入年月日后自动产生. 000: 星期日 001: 星期一 010: 星期二 011: 星期三 100: 星期四 101: 星期五 110: 星期六 Mnemonic: MONTH 7 6 5 4 MONTH[4] 3 2 1 MONTH[3:0] Address: 06H 0 Reset 00H MONTH[4:0]: 以BCD格式显示目前月份,区间为1~12月. MONTH[4]: 此位代表月之十位. MONTH[3:0]: 此4位代表月之个位. Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 4 Ver.A 2010/06 SM59R RTC 功能使用方法 Mnemonic: YEAR 7 6 5 YEAR[7:4] 4 3 2 1 YEAR[3:0] Address: 07H 0 Reset 00H 3 2 1 ALMMIN[3:0] Address: 08H 0 Reset 00H YEAR[7:0]: 以BCD格式显示目前年份,区间为0~99年. YEAR[7:4]: 此4位代表年之十位. YAER[3:0]: 此4位代表年之个位. Mnemonic: ALMMIN 7 6 5 MINen ALMMIN[6:4] 4 MINen: 分示警启动位 0: 禁止; ALMMIN[6:0]填入值不需与RTC值比较,也不会产生示警中断(ALARM INT). 1: 启动; ALMMIN[6:0]填入值需与RTC值比较,并产生示警中断(ALARM INT). ALMMIN[6:0]: 以BCD格式设定示警中断之分钟数,区间为0~59分钟. ALMMIN[6:4]: 此3位设定示警中断分钟之十位. ALMMIN[3:0]: 此4位设定示警中断分钟之个位. Mnemonic: ALMHOUR 7 6 5 4 HOURen ALMHOUR[5:4] 3 2 1 ALMHOUR[3:0] 0 Address: 09H Reset 00H HOURen: 时示警启动位 0: 禁止; ALMHOUR[5:0]填入值不需与RTC值比较,也不会产生示警中断(ALARM INT). 1: 启动; ALMHOUR[5:0]填入值需与RTC值比较,并产生示警中断(ALARM INT). ALMHOUR[5:0]: 以BCD格式设定示警中断之时,区间为0~23时. ALMHOUR[5:4]: 此2位代表示警中断时之十位. ALMHOUR[3:0]: 此4位代表示警中断时之个位. Mnemonic: ALMDAY 7 6 DAYen - 5 4 ALMDAY[5:4] 3 2 1 ALMDAY[3:0] 0 Address: 10H Reset 00H DAYen: 日示警启动位 0: 禁止; ALMDAY[5:0]填入值不需与RTC值比较,也不会产生示警中断(ALARM INT). 1: 启动; ALMDAY[5:0]填入值需与RTC值比较,并产生示警中断(ALARM INT). ALMDAY[5:0] 以BCD格式设定示警中断之日期,区间为0~31日. ALMDAY[5:4]: 此2位代表示警中断日期之十位. ALMDAY[3:0]: 此4位代表示警中断日期之个位. Mnemonic: ALMWEEK 7 6 5 WEEKen 4 3 2 1 ALMWEEK[2:0] 0 Address: 11H Reset 00H WEEKen: 星期示警启动位 0: 禁止; ALMWEEK[2:0]填入值不需与RTC值比较,也不会产生示警中断(ALARM INT). Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 5 Ver.A 2010/06 SM59R RTC 功能使用方法 1: 启动; ALMWEEK[2:0]填入值需与RTC值比较,并产生示警中断(ALARM INT). ALMWEEK[2:0] 示警星期设定 000: 星期日 001: 星期一 010: 星期二 011: 星期三 100: 星期四 101: 星期五 110: 星期六 Mnemonic: ALMMONTH 7 6 5 MONTHen - 4 ALMMONTH[4] 3 2 1 ALMMONTH[3:0] 0 Address: 12H Reset 00H MONTHen: 月示警启动位 0: 禁能; ALMMONTH[4:0]填入值不需与RTC值比较,也不会产生示警中断(ALARM INT). 1: 启动; ALMMONTH[4:0]填入值需与RTC值比较,并产生示警中断(ALARM INT). ALMMONTH[4:0]: 以BCD格式设定示警中断之月份,区间为1~12月. ALMMONTH[4]: 此位代表示警中断月之十位. ALMMONTH[3:0]: 此4位代表示警中断月之个位. 4 RTC 中断 4.1 中断向量表(Interrupt vectors table): Interrupt Vector Interrupt Number Address *(use Keil C Tool) IE0 – External interrupt 0 0003h 0 TF0 – Timer 0 interrupt 000Bh 1 IE1 – External interrupt 1 0013h 2 TF1 – Timer 1 interrupt 001Bh 3 RI0/TI0 – Serial channel 0 interrupt 0023h 4 TF2/EXF2 – Timer 2 interrupt 002Bh 5 PWMIF – PWM interrupt 0043h 8 SPIIF – SPI interrupt 004Bh 9 ADCIF – A/D converter interrupt 0053h 10 KBIIF – keyboard Interface interrupt 005Bh 11 LVIIF – Low Voltage Interrupt 0063h 12 IICIF – IIC interrupt 006Bh 13 RI1/TI1 – Serial channel 1 interrupt 0083h 16 RTC/ALARM interrupt 008Bh 17 Interrupt Request Flags Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 6 Ver.A 2010/06 SM59R RTC 功能使用方法 Comparator interrupt 0093h 18 *See Keil C about C51 User’s Guide about Interrupt Function description 4.2 中断相关缓存器(Interrupt SFR) Mnemonic Description Bit 7 Direct Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET Interrupt IEN0 IEN1 IEN2 IRCON IRCON2 IP0 IP1 Interrupt Enable 0 register Interrupt Enable 1 register Interrupt Enable 2 register Interrupt request register Interrupt request register 2 Interrupt priority level 0 Interrupt priority level 1 A8h EA - ET2 ES0 ET1 EX1 ET0 EX0 00h B8h EXEN2 - IEIIC IELVI IEKBI IEADC IESPI IEPWM 00h 9Ah - - - - - ECmpI ERTC ES1 00h C0H EXF2 TF2 IICIF LVIIF KBIIF ADCIF SPIIF PWMIF 00H 97H - - - - - CmpIF RTCIF - 00H A9h - - IP0.5 IP0.4 IP0.3 IP0.2 IP0.1 IP0.0 00h B9h - - IP1.5 IP1.4 IP1.3 IP1.2 IP1.1 IP1.0 00h Interrupt Enable 0 register(IEN0) Mnemonic: IEN0 Address: A8h 7 6 5 4 3 2 1 0 Reset EA - ET2 ES0 ET1 EX1 ET0 EX0 00h EA: EA = 0 : disable all interrupt. EA = 1 : enable all interrupt. ET2: ET2 = 0 : 禁能 Timer 2 溢位中断及 Timer 2 T2EX 外部中断。 (disable Timer 2 overflow interrupt or Timer 2 external reload interrupt.) ET2 = 1 : 致能 Timer 2 溢位中断及 Timer 2 T2EX 外部中断。 (enable Timer 2 overflow interrupt or Timer 2 external reload interrupt.) ES0: ES0=0 – Disable Serial channel 0 interrupt. ES0=1 – Enable Serial channel 0 interrupt. Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 7 Ver.A 2010/06 SM59R RTC 功能使用方法 ET1: ET1=0 – Disable Timer 1 overflow interrupt. ET1=1 – Enable Timer 1 overflow interrupt. EX1: EX1=0 – Disable external interrupt 1. EX1=1 – Enable external interrupt 1. ET0: ET0=0 – Disable Timer 0 overflow interrupt. ET0=1 – Enable Timer 0 overflow interrupt. EX0: EX0=0 – Disable external interrupt 0. EX0=1 – Enable external interrupt 0. Interrupt Enable 2 register(IEN2) Mnemonic: IE2 Address: 9Ah 7 6 5 4 3 2 1 0 Reset - - - - - ECmpI ERTC ES1 00h ECmpI: ECmpI =0 – Disable Comparator interrupt. ECmpI =1 – Enable Comparator interrupt(include comparator_0 and comparator_1). ERTC: ERTC =0 – Disable RTC interrupt. ERTC =1 – Enable RTC interrupt(include Periodical and Alarm Int). ES1: ES1=0 – Disable Serial channel 1 interrupt. ES1=1 – Enable Serial channel 1 interrupt. Interrupt request register 2(IRCON2) Mnemonic: Address: 97h 7 6 5 4 3 2 1 0 Reset - - - - - CmpIF RTCIF - 00H RTCIF RTC interrupt flag(include Periodical and Alarm Int). HW will clear this flag automatically when enter interrupt vector. SW can clear this flag also.(in case RTC INT disable) CmpIF Comparator interrupt flag, HW will clear this flag automatically when enter interrupt vector. SW can clear this flag also.(in case analog comparator INT disable) Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 8 Ver.A 2010/06 SM59R RTC 功能使用方法 5 Timer 2 中断应用参考程序及设定: (1) 中断致能设定: IEN2 |= 0x02; // Enable RTC_INT. EA = 1; // All interrupt enable RTCADDR = RTCCTRL; RTCDATA = 0xA8; // RTCen=1, ALARMInten=1, PeriodIntEn=1, Period=0.5 sec (2) 中断程序表示: void ISR_RTC(void) interrupt 17 { RTCADDR = RTCCTRL; if(RTCDATA & PeriodIF) { // Must clear by software write "0". RTCADDR = RTCCTRL; RTCDATA &= (~PeriodIF); } else if(RTCDATA & ALARMIF) { // Must clear by software write "0". RTCADDR = RTCCTRL; RTCDATA &= (~ALARMIF); } // HW will clear this flag automatically. //IRCON2 &= (~RTCIF); } Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 9 Ver.A 2010/06 SM59R RTC 功能使用方法 6 RTC 范例程序说明: 6.1 此范例可使用超机终端机做通讯,并依据操作说明输入选项及设定即可,超机终端机请设定如下: 7 RTC 应用的范例程序(一) Description Define //以下定义已加入 SM59R16A5.h /* RTC Define */ #define RTCCTRL 0 #define RTCSEC 1 #define RTCMIN 2 #define RTCHOUR 3 #define RTCDAY 4 #define RTCWEEK 5 #define RTCMONTH 6 #define RTCYEAR 7 #define RTCALMMIN 8 #define RTCALMHOUR 9 #define RTCALMDAY 10 #define RTCALMWEEK 11 #define RTCALMMONTH 12 #define #define #define #define #define RTCen PeriodIF ALARMInten ALARMIF PeriodIntEn 0x80 0x40 0x20 0x10 0x08 Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 10 Ver.A 2010/06 SM59R RTC 功能使用方法 #define RTCIF Main program 0X02 #define RTC_half_sec #define RTC_one_sec #define RTC_half_min #define RTC_one_min #define RTC_half_hour #define RTC_one_hour #include <stdio.h> #include <SM59R16A5.h> xdata xdata xdata xdata char char char char 0 1 2 3 4 5 CMD_001=0, CMD_002=0; RTC_BFU[13]; RTC_Period_target=5; RTC_Period_count=0; #define Pause_RTC_INT() IEN2 #define Gono_RTC_INT() IEN2 &= 0xFD; |= 0x02; // Disable RTC_INT. // Enable RTC_INT. /*void delay(int dd) { while(dd--); } */ void Reset_MCU(void) { TAKEY = 0x55; TAKEY = 0xAA; TAKEY = 0x5A; SWRES = 0xFF; } void Show_BCD(char dat) { putchar((dat>>4) | 0x30); putchar((dat&0x0F) | 0x30); } void Show_Manual(void) { printf ("\n"); printf ("SM59R16A5 RTC manual: \n"); printf ("1. RTC now \n"); printf ("2. SET RTC time\n"); printf ("3. SET Period \n"); printf ("4. ON/OFF Period \n"); printf ("5. SET ALARM \n"); printf ("6. ON/OFF ALARM \n"); printf ("Other. System Reset!! \n"); printf ("Enter:"); } void Write_RTC_SFR(char addr, char dat) { RTCADDR = addr; RTCDATA = dat; } char Read_RTC_SFR(char addr) Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 11 Ver.A 2010/06 SM59R RTC 功能使用方法 { char dat; RTCADDR = addr; dat = RTCDATA; return dat; } void Read_RTC_Now_Time(void) { RTC_BFU[RTCYEAR] = Read_RTC_SFR(RTCYEAR); RTC_BFU[RTCMONTH] = Read_RTC_SFR(RTCMONTH); RTC_BFU[RTCDAY] = Read_RTC_SFR(RTCDAY); RTC_BFU[RTCWEEK] = Read_RTC_SFR(RTCWEEK); RTC_BFU[RTCHOUR] = Read_RTC_SFR(RTCHOUR); RTC_BFU[RTCMIN] = Read_RTC_SFR(RTCMIN); RTC_BFU[RTCSEC] = Read_RTC_SFR(RTCSEC); } void Show_RTC_Now_Time(void) { printf("RTC Now:\t"); Read_RTC_Now_Time(); Show_BCD( 0x20 ); Show_BCD( RTC_BFU[RTCYEAR] ); putchar(0x2F); Show_BCD( RTC_BFU[RTCMONTH] ); putchar(0x2F); Show_BCD( RTC_BFU[RTCDAY] ); putchar(0x20); // year // // // // // / month / day SPACE putchar(0x28); Show_BCD( RTC_BFU[RTCWEEK] ); putchar(0x29); printf("\t"); // // // // ( week ) tab Show_BCD( RTC_BFU[RTCHOUR] ); putchar(0x3A); Show_BCD( RTC_BFU[RTCMIN] ); putchar(0x3A); Show_BCD( RTC_BFU[RTCSEC] ); printf("\n"); // // // // // hour : min : sec } void Show_RTC_ALARM_Set(void) { RTCADDR = RTCCTRL; if(RTCDATA&0x20) printf("ALARM Status:\tON\n"); else printf("ALARM Status:\tOFF\n"); printf("ALARM Setting:\tYYYY/MM/DD(week)hr:min\n"); printf("\t\t"); RTCADDR = RTCYEAR; putchar(0x2F); Show_BCD( 0x20 ); Show_BCD( RTCDATA ); // year // / Show_BCD(Read_RTC_SFR(RTCALMMONTH) & 0x1F); // month putchar(0x2F); // / Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 12 Ver.A 2010/06 SM59R RTC 功能使用方法 Show_BCD(Read_RTC_SFR(RTCALMDAY) & 0x3F ); putchar(0x20); // day // SPACE putchar(0x28); Show_BCD(Read_RTC_SFR(RTCALMWEEK) & 0x07 ); putchar(0x29); printf("\t"); // // // // Show_BCD(Read_RTC_SFR(RTCALMHOUR) & 0x3F ); putchar(0x3A); Show_BCD(Read_RTC_SFR(RTCALMMIN) & 0x07 ); printf("\n"); // hour // : // min ( week ) tab } void SET_Period(void) { // char h_dat, l_dat; Show_RTC_Now_Time(); Show_RTC_ALARM_Set(); printf printf printf printf printf printf printf printf ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); printf printf printf printf printf printf printf printf ("1. 01 sec \n"); ("2. 05 sec \n"); ("3. 10 sec \n"); ("4. 20 sec \n"); ("5. 30 sec \n"); ("6. 60 sec \n"); ("other. Exit!! \n"); ("Enter:"); CMD_002 = getchar(); switch (CMD_002) { case 0x31: RTC_Period_target= 1;// break; case 0x32: RTC_Period_target= 5;// break; case 0x33: RTC_Period_target=10;// break; case 0x34: RTC_Period_target=20;// break; case 0x35: RTC_Period_target=30;// break; case 0x36: RTC_Period_target=60;// break; default: break; }// end switch printf ("\n"); 01 sec 05 sec 10 sec 20 sec 30 sec 60 sec } void ON_OFF_Period(void) { printf ("\t"); printf ("1. Set period interrupt ON \n"); printf ("\t"); printf ("other. Set period interrupt OFF \n"); printf ("\t"); printf ("Enter:"); Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 13 Ver.A 2010/06 SM59R RTC 功能使用方法 CMD_002 = getchar(); RTCADDR = RTCCTRL; if(CMD_002==0x31) printf ("\n"); RTCDATA &= 0xF7; RTCDATA |= 0x08; // PeriodIntEn=0 // PeriodIntEn=1 } void ON_OFF_ALARM(void) { printf ("\t"); printf ("1. Set ALARM ON \n"); printf ("\t"); printf ("other. Set ALARM OFF \n"); printf ("\t"); printf ("Enter:"); CMD_002 = getchar(); RTCADDR = RTCCTRL; if(CMD_002==0x31) printf ("\n"); RTCDATA &= 0xDF; RTCDATA |= 0x20; // ALARMIntEn=0 // ALARMIntEn=1 } void SET_RTC_Time(void) { char h_dat, l_dat; while(1) { Show_RTC_Now_Time(); printf printf printf printf printf printf printf printf printf printf ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); ("\t"); printf printf printf printf printf printf printf printf printf printf ("<Which one is your choice?>\n"); ("1. Year \n"); ("2. Month \n"); ("3. Day \n"); ("4. Week \n"); ("5. Hour \n"); ("6. Minute \n"); ("7. Second \n"); ("other. Exit!!\n"); ("Enter:"); CMD_002 = getchar(); printf ("\n"); if(CMD_002<0x31 | CMD_002>0x37) break; switch (CMD_002) { case 0x31: printf ("\tYear:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCYEAR, (h_dat<<4) | l_dat ); printf ("\n"); break; case 0x32: printf ("\tMonth:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCMONTH, (h_dat<<4) | l_dat ); printf ("\n"); break; case 0x33: printf ("\tDay:\n"); h_dat = getchar()-0x30; Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 14 Ver.A 2010/06 SM59R RTC 功能使用方法 l_dat = getchar()-0x30; Write_RTC_SFR(RTCDAY, (h_dat<<4) | l_dat ); printf ("\n"); break; case 0x34: printf ("\tWeek:"); Write_RTC_SFR(RTCWEEK, (getchar()-0x30) | 0x80); printf ("\n"); break; case 0x35: printf ("\tHour:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCHOUR, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; case 0x36: printf ("\tMinute:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCMIN, (h_dat<<4) | l_dat | 0x80); printf ("\n"); case 0x37: printf ("\tSecond:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCSEC, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; default: break; }// end switch }// end while(1) } void SET_ALARM(void) { char h_dat, l_dat; while(1) { Show_RTC_Now_Time(); Show_RTC_ALARM_Set(); printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf printf ("\t"); printf ("<Which one is your choice?>\n"); ("1. Month \n"); ("2. Day \n"); ("3. Week \n"); ("4. Hour \n"); ("5. Minute \n"); ("Other. Exit!!\n"); ("Enter:"); CMD_002 = getchar(); printf ("\n"); if(CMD_002<0x31 | CMD_002>0x35) break; switch (CMD_002) { Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 15 Ver.A 2010/06 SM59R RTC 功能使用方法 case 0x31: printf ("\tMonth:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCALMMONTH, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; case 0x32: printf ("\tDay:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCALMDAY, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; case 0x33: printf ("\tWeek:"); Write_RTC_SFR(RTCALMWEEK, (getchar()-0x30) | 0x80); printf ("\n"); break; case 0x34: printf ("\tHour:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCALMHOUR, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; case 0x35: printf ("\tMinute:"); h_dat = getchar()-0x30; l_dat = getchar()-0x30; Write_RTC_SFR(RTCALMMIN, (h_dat<<4) | l_dat | 0x80); printf ("\n"); break; default: break; }// end switch }// end while(1) } void Init_UART(void) { SCON = 0x50; TMOD |= 0x20; PCON = 0x80; // TH1 = 0xD0; // TH1 = 0xFE; TH1 = 0xFF; // TH1 = 0xFF; TCON= 0x40; TI = 1; Printf("")*/ // EA = 1; // ES = 1; } //SCON: serail mode 1, 8-bit UART, enable receive /* TMOD: timer 1, mode 2, 8-bit reload //SMOD = 1; //Baud:2400 fosc=22.1184MHz OK! //Baud:57600 fosc=22.1184MHz OK! //Baud:115200 fosc=22.1184MHz OK! //Baud:57600 fosc=11.0592MHz OK! //timer 1 run /*TI: set TI to send first char of UART */ for //all interrupt enable //UART enable void Init_RTC(void) Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 16 Ver.A 2010/06 SM59R RTC 功能使用方法 { // // // sec IEN2 IEN2 EA RTCADDR RTCDATA |= 0x02; &= 0xFD; = 1; = RTCCTRL; = 0x81; // Enable RTC_INT. // Disable RTC_INT. // All interrupt enable // RTCen=1, ALARMInten=0, PeriodIntEn=0, Period= 1 Write_RTC_SFR(RTCCTRL, RTCen | RTC_one_sec ); Write_RTC_SFR( Write_RTC_SFR( Write_RTC_SFR( Write_RTC_SFR( Write_RTC_SFR( Write_RTC_SFR( Write_RTC_SFR( RTCYEAR, RTCMONTH, RTCDAY, RTCWEEK, RTCALMMONTH, RTCALMDAY, RTCALMWEEK, (0x10)); (0x05)); (0x01)); (0x06)); (0x05)); (0x01)); (0x06)); } void ISR_RTC(void) interrupt 17 { RTCADDR = RTCCTRL; if(RTCDATA & PeriodIF) { RTC_Period_count++; if(RTC_Period_count >= RTC_Period_target) { printf ("\n<RTC Period ISR occur> "); Show_RTC_Now_Time(); RTC_Period_count =0; } RTCADDR = RTCCTRL; RTCDATA &= (~PeriodIF); // Must clear by software write "0". } // else if(RTCDATA & ALARMIF) if(RTCDATA & ALARMIF) { printf("\n<RTC ALARM ISR occur>"); Show_RTC_Now_Time(); Show_Manual(); RTCADDR = RTCCTRL; RTCDATA &= (~ALARMIF); // Must clear by software write "0". } //IRCON2 &= (~RTCIF); //RTCIF=0;// HW will clear this flag automatically. } void main(void) { Init_UART(); putchar(0x31); putchar(0x32); putchar(0x33); printf("\n"); printf("\n"); printf("\n"); Init_RTC(); Show_RTC_Now_Time(); Show_RTC_ALARM_Set(); while(1) Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 17 Ver.A 2010/06 SM59R RTC 功能使用方法 { Show_Manual(); CMD_001 = getchar(); printf ("\n\n"); Pause_RTC_INT(); switch (CMD_001) { case 0x31: Show_RTC_Now_Time(); break; case 0x32: SET_RTC_Time(); break; case 0x33: SET_Period(); break; case 0x34: ON_OFF_Period(); break; case 0x35: SET_ALARM(); break; case 0x36: ON_OFF_ALARM(); break; default: Reset_MCU(); break; }//end switch //printf("\n"); CMD_001=0; Gono_RTC_INT(); } } Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0202 18 Ver.A 2010/06