📄 rtc.c
字号:
//====================================================================
// File Name : rtc.c
// Function : S3C2413 RTC Test Program
// Date : Sep. 27, 2005
// Version : 0.0
// History
// 0.0 : Programming start (Sep. 27, 2005)
//====================================================================
#include <stdio.h>
#include <stdlib.h>
#include "2413addr.h"
#include "System.h"
#include "Exception.h"
#include "Console.h"
#include "def.h"
#include "rtc.h"
char *day[8] = {" ","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
volatile U32 Tick=0, Alarm=0;
void * rtc_func[][2]=
{
(void *)RTC_time_tick, "RTC Time Tick Int Test",
(void *)Rtc_Alarm, "RTC Alarm Test ",
// (void *)Round_Reset, "Round Reset Test ",
0,0
};
void Ch16_RTC(void)
{
int i=0;
while(1) {
printf("\n");
while(1)
{ //display menu
printf("%2d:%s",i,rtc_func[i][1]);
i++;
if((int)(rtc_func[i][0])==0)
{
printf("\n");
break;
}
if((i%4)==0) printf("\n");
}
printf("\nSelect(-1 to exit): ");
i = GetIntNum();
if(i==-1) break;
if(i>=0 && (i<(sizeof(rtc_func)/4)) )
( (void (*)(void)) (rtc_func[i][0]) )(); // execute selected function.
}
}
void RTC_time_tick(void)
{
int i;
RTC_Init();
printf("[0]1/32768\t[1]:1/2048 :");
do {
i=GetIntNum();
} while (i<0 && i>1);
if(i) {
rRTCCON |= 0x10;
Tick_Count_Set(32767);
}
else
Tick_Count_Set(2047);
pISR_TICK = (U32)Rtc_Tick_ISR;
rINTMSK &= ~(BIT_TICK);
rTICNT0 |= (1<<7); // Tick Time Interrupt Enable
while(!Uart_GetKey()) {
while(!Tick); // Wait Tick Interrupt
printf("Tick\t");
Print_RTC();
Tick=0;
}
rRTCCON &= ~(1<<4);
rTICNT0 &= ~(1<<7); // Tick Time Interrupt Disable
rINTMSK |= (BIT_TICK);
ClearPending(BIT_TICK);
}
void Rtc_Alarm(void)
{
RTC_Init();
Rtc_Alarm_Set();
pISR_RTC = (U32)Rtc_Alarm_ISR;
rINTMSK &= ~(BIT_RTC);
rRTCALM=0x47;
Alarm=0;
while(!Alarm); // Wait Alarm Interrupt
printf("Alarm\t");
Print_RTC();
rINTMSK |= (BIT_RTC);
ClearPending(BIT_RTC);
}
void Round_Reset(void)
{
U32 save_rGPGCON, save_rGPGDN,save_rEXTINT1;
rGPGCON = save_rGPGCON;
rGPGDN = save_rGPGDN;
rEXTINT1 = save_rEXTINT1;
rGPGCON=rGPGCON&~(3<<6)|(2<<6); //GPG3=EINT11
rGPGDN |= (1<<3);
rEXTINT1=rEXTINT1&~(7<<12)|(2<<12); //EINT11=falling edge triggered
RTC_Init();
printf("Press any key to exit.\n\n");
printf("Press EINT11 key to test round reset.\n");
rRTCCON = rRTCCON | 0x1; //RTC Control enable
rRTCRST = (3);
Tick_Count_Set(2047);
pISR_TICK = (U32)Rtc_Tick_ISR;
pISR_EINT8_23 = (U32)EINT11_int;
rINTMSK &= ~(BIT_TICK|BIT_EINT8_23);
rEINTMASK&=~(1<<11);
rTICNT0 |= (1<<7); // Tick Time Interrupt Enable
while(!Uart_GetKey()) {
while(!Tick); // Wait Tick Interrupt
Print_RTC();
Tick=0;
}
rTICNT0 &= ~(1<<7); // Tick Time Interrupt Disable
rEINTMASK|=(1<<11);
rINTMSK |= (BIT_TICK|BIT_EINT8_23);
ClearPending(BIT_TICK|BIT_EINT8_23);
rRTCCON = rRTCCON &~(0x1); //RTC Control disable
save_rGPGCON = rGPGCON;
save_rGPGDN = rGPGDN;
save_rEXTINT1 = rEXTINT1;
}
void Round_Reset_Enable(U32 boundary)
{
if(boundary==30)
rRTCRST = (1<<3)|(3);
else if(boundary==40)
rRTCRST = (1<<3)|(4);
else if(boundary==50)
rRTCRST = (1<<3)|(5);
else
printf("Round Reset Boundary Error\n");
}
void Round_Reset_Disable(void)
{
rRTCRST &= ~(1<<3);
}
void Tick_Count_Set(U32 count)
{
rTICNT0 = count/256;
rTICNT1 = count%256;
}
void Rtc_Alarm_Set()
{
rRTCCON = rRTCCON | 0x1; //RTC Control enable
rALMHOUR =rBCDHOUR;
rALMMIN =rBCDMIN;
rALMSEC =rBCDSEC+5;
printf("After 5 second, Alarm Interrupt Occur\n");
rRTCCON = rRTCCON & ~(0x1); //RTC Control disable
}
void Print_RTC(void)
{
int year,month,date,hour,min,sec,weekday;
rRTCCON = rRTCCON | 0x1; //RTC Control enable
if(rBCDYEAR==0x99)
year = 0x1999;
else
year = 0x2000 + rBCDYEAR;
month = rBCDMON;
weekday = rBCDDAY;
date = rBCDDATE;
hour = rBCDHOUR;
min = rBCDMIN;
sec = rBCDSEC;
rRTCCON = rRTCCON & ~(0x1); //RTC Control disable
printf("%2x : %2x : %2x %10s, %2x/%2x/%4x\n",hour,min,sec,day[weekday],month,date,year);
}
void RTC_Init(void)
{
int year,month,date,hour,min,sec;
year = 5;
month= 12;
date = 31;
hour = 23;
min = 59;
sec = 20;
rRTCCON = rRTCCON | 0x1; //RTC Control enable
rBCDYEAR =( ((year/10)<<4) + (year%10) );
rBCDMON =( ((month/10)<<4)+ (month%10));
rBCDDATE =( ((date/10)<<4) + (date%10) );
rBCDDAY = rBCDDAY & ~(0x7) | 0x3; //SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
rBCDHOUR =( ((hour/10)<<4) + (hour%10) );
rBCDMIN =( ((min/10)<<4) + (min%10) );
rBCDSEC =( ((sec/10)<<4) + (sec%10) );
rRTCCON = rRTCCON & ~(0x1); //RTC Control disable
printf("%2d : %2d : %2d %10s, %2d/%2d/20%02d\n",hour,min,sec,day[3],month,date,year);
}
void __irq Rtc_Tick_ISR(void)
{
ClearPending(BIT_TICK);
Tick=1;
}
void __irq Rtc_Alarm_ISR(void)
{
ClearPending(BIT_RTC);
Alarm=1;
}
void __irq EINT11_int(void)
{
rEINTMASK|=(1<<11);
rINTMSK |= (BIT_EINT8_23);
rEINTPEND = (1<<11);
ClearPending(BIT_EINT8_23);
rRTCRST |= (1<<3);
printf("EINT14 interrupt is requested for RTC round RESET !\n");
rRTCRST &= ~(1<<3);
rINTMSK &= ~(BIT_EINT8_23);
rEINTMASK&=~(1<<11);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -