RTC.H
/****************/
/* This file is part of the uVision/ARM development tools */
/* Copyright KEIL ELEKTRONIK GmbH 2002-2004 */
/**************/
/* */
/* RTC.H: Header file for Real Time Clock (RTC) */
/* Samsung S3C44B0X */
/* */
/****************/
#ifndef __RTC_H
#define __RTC_H
#define RTC_BASE 0x01D70040 // Real Time Clock Base Address
// Real Time Clock Registers
typedef struct {
reg8 RTCCON; // RTC Control
reg8 pad0[15];
reg8 RTCALM; // RTC Alarm
reg8 pad1[3];
reg8 ALMSEC; // Alarm Second
reg8 pad2[3];
reg8 ALMMIN; // Alarm Minute
reg8 pad3[3];
reg8 ALMHOUR; // Alarm Hour
reg8 pad4[3];
reg8 ALMDAY; // Alarm Day
reg8 pad5[3];
reg8 ALMMON; // Alarm Month
reg8 pad6[3];
reg8 ALMYEAR; // Alarm Year
reg8 pad7[3];
reg8 RTCRST; // RTC Round Reset
reg8 pad8[3];
reg8 BCDSEC; // BCD Second
reg8 pad9[3];
reg8 BCDMIN; // BCD Minute
reg8 padA[3];
reg8 BCDHOUR; // BCD Hour
reg8 padB[3];
reg8 BCDDAY; // BCD Day
reg8 padC[3];
reg8 BCDDATE; // BCD Date
reg8 padD[3];
reg8 BCDMON; // BCD Month
reg8 padE[3];
reg8 BCDYEAR; // BCD Year
reg8 padF[3];
reg8 TICINT; // Tick Time Count
reg8 padG[3];
} regRTC;
#define pRTC ((regRTC *)RTC_BASE)
// RTCCON: RTC Control Register
#define RTCEN 0x00000001
#define CLKSEL 0x00000002
#define CNTSEL 0x00000004
#define CLKRST 0x00000008
// RTCALM: RTC Alarm Register
#define SECEN 0x00000001
#define MINEN 0x00000002
#define HOUREN 0x00000004
#define DAYEN 0x00000008
#define MONREN 0x00000010
#define YEAREN 0x00000020
#define ALMEN 0x00000040
// RTCRST: RTC Round Reset Register
#define SECCR_NONE 0x00000000
#define SECCR_30 0x00000003
#define SECCR_40 0x00000004
#define SECCR_50 0x00000005
#define SRSTEN 0x00000008
//*****************************************************
// TICNT: Tick Time Count Register
#define TICKINT_EN 0x00000080
#ifndef __RTC_STRUCT_
#define __RTC_STRUCT_
//*****************************************************
//定义时间数据结构
//*****************************************************
typedef struct {
unsigned char Year; //采用BIN方法记录
unsigned char Mon;
unsigned char Day;
unsigned char Hour;
unsigned char Min;
unsigned char Sec;
unsigned char mS;
unsigned char Week;
}RTC_TIMER;
//***************************************************************************
//周期中断tick 10ms中断1次
//***************************************************************************
typedef struct{
unsigned int Tick;
unsigned char Fl200ms;
unsigned char Fl500ms;
unsigned char Fl1000ms;
unsigned char Divk[3]; //记录TICK对应3个标志的取摸系数
}RTC_TICK;
//*****************************************************
extern void RTC_Timer_Init(void);
extern void RTC_Read_Timer(RTC_TIMER * p);
extern void RTC_Write_Timer(RTC_TIMER *p);
extern void RTC_Alarm_Init(RTC_TIMER *p);
extern void RTC_Tick_Init (void);
#endif
#endif /* __RTC_H */
#include "../inc/44b.h"
#include "../S3C44B0X_INC/S3C44B0X.H"
//*******************************************************************
RTC_TIMER SysTimer;
regRTC * const pRtc=pRTC; //定义RCT的指针
//*****************************************************
//定义初始化时间BIN
//*****************************************************
RTC_TIMER DefTimer={
6, //2006 06 10 08:20:50:00 6(week)
6,
10,
8,
20,
50,
0,
6,
};
//*******************************************************************
unsigned char BinToBCD (unsigned char i){
unsigned char bcd;
bcd =i/10;
bcd |=i%10;
return bcd;
}
//*******************************************************************
unsigned char BCDToBin (unsigned char bcd){
unsigned char i;
i = 10*(bcd & 0xf0);
i += bcd & 0x0f;
return i;
}
//*******************************************************************
//时间BIN<===>BCD
//*******************************************************************
void Turn_Bin_BCD (RTC_TIMER *pS,RTC_TIMER *pT){
pT->Year = BinToBCD(pS->Year); //2进制时间转化成BCD
pT->Mon = BinToBCD(pS->Mon);
pT->Day = BinToBCD(pS->Day);
pT->Hour = BinToBCD(pS->Hour);
pT->Min = BinToBCD(pS->Min);
pT->Sec = BinToBCD(pS->Sec);
pT->Week = BinToBCD(pS->Week);
}
//********************************************************************
//时间输入S3C44B0XRTC寄存器
//********************************************************************
void PutToRtc (RTC_TIMER *p){
pRtc->BCDYEAR = p->Year;
pRtc->BCDMON = p->Mon;
pRtc->BCDDAY = p->Day; // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
pRtc->BCDDATE = p->Week;
pRtc->BCDHOUR = p->Hour;
pRtc->BCDMIN = p->Min;
pRtc->BCDSEC = p->Sec;
}
//*********************************************************************
//S3C44B0X RTC 初始化
//*********************************************************************
void RTC_Timer_Init(void){
/* CLKRST [3] RTC 时钟计数复位
0 = No reset, 1 = Reset
CNTSEL [2] BCD计数复位
0 = Merge BCD counters
1 = 保留 (Separate BCD counters)
CLKSEL [1] BCD 时钟选择
0 = XTAL 1/2 15 divided clock 1Hz
1 = Reserved (XTAL clock only for test)
RTCEN [0] RTC读写允许
0 = Disable, 1 = Enable
*/
RTC_TIMER timer;
Turn_Bin_BCD (&timer,&DefTimer);
pRtc->RTCCON |= 0x01; // R/W enable, 1/32768, Normal(merge), No reset
PutToRtc(&timer);
pRtc->RTCCON &=~0x01; //禁止R/W
}
//*************************************************************************
//读取时间
//*************************************************************************
void RTC_Read_Timer(RTC_TIMER * p){
p->Year =BCDToBin( pRtc->BCDYEAR);
p->Mon =BCDToBin( pRtc->BCDMON);
p->Day =BCDToBin( pRtc->BCDDAY);
p->Week =BCDToBin( pRtc->BCDDATE);
p->Hour =BCDToBin( pRtc->BCDHOUR);
p->Min =BCDToBin( pRtc->BCDMIN);
p->Sec =BCDToBin( pRtc->BCDSEC);
}
//**************************************************************************
//写时间
//**************************************************************************
void RTC_Write_Timer(RTC_TIMER *p){
RTC_TIMER timer;
Turn_Bin_BCD (&timer,&SysTimer);
pRtc->RTCCON |= 0x01; // R/W enable, 1/32768, Normal(merge), No reset
PutToRtc(&timer);
pRtc->RTCCON &=~0x01; //禁止R/W
}
//********************************************************************************
//定时处理中断
//********************************************************************************
void __irq RTC_ISP_Alarm(void){
rI_ISPC=BIT_RTC;
//rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
//Uart_Printf("/nRTC Alarm Interrupt O.K./n");
}
//**********************************************
RTC_TIMER SysAlarm;
//**********************************************
//定时时间初始化
//*********************************************
void RTC_Alarm_Init(RTC_TIMER *p){
RTC_TIMER timer;
Turn_Bin_BCD (&timer,p);
pRtc->ALMYEAR=timer.Year;
pRtc->ALMMON =timer.Mon;
pRtc->ALMDAY =timer.Day;
pRtc->ALMHOUR=timer.Hour;
pRtc->ALMMIN =timer.Min;
pRtc->ALMSEC =timer.Sec;
pISR_RTC=(unsigned)RTC_ISP_Alarm;
//***********************************************************
//全部允许报警
//***********************************************************
pRtc->RTCALM=0x7f; //=1 打开 RTC ALARM CONTROL REGISTER (RTCALM)
//***********************************************************
rINTMSK=~(BIT_GLOBAL|BIT_RTC); //=0允许中断!
}
//*****************************************************************************
RTC_TICK RtcTick; //定义数据
//*****************************************************************************
//中断处理涵数
//*****************************************************************************
void __irq RTC_ISP_Tick(void){
rI_ISPC=BIT_TICK;
RtcTick.Tick++;
if(RtcTick.Tick % RtcTick.Divk[0]==0 ){
RtcTick.Fl200ms = ~RtcTick.Fl200ms;
}
if(RtcTick.Tick % RtcTick.Divk[1]==0 ){
RtcTick.Fl500ms = ~RtcTick.Fl500ms;
}
if(RtcTick.Tick % RtcTick.Divk[2]==0 ){
RtcTick.Fl1000ms = ~RtcTick.Fl1000ms;
}
}
/*****************************************************************************
TICNT 计算10MS的分频系数 CLK=32768Hz 计算:Divk[3]
Period = ( n+1 ) / 128 second
n : Tick time count value (1-127)
如果要10ms中断==》n=1; 7.8ms中断一次
TICK INT ENABLE [7] 时间滴答中断允许
0 = disable 1 = enable
TICK TIME COUNT [6:0] 时间滴答计数值. (1-127)
这个计数值内部递减, 用户不能读它的实时值
******************************************************************************/
void RTC_Tick_Init (void){
pRtc->TICINT = 0x11; //打开 n=1
RtcTick.Divk[0] = 2*13; //k=200ms/(1000ms*(n+1)/128)
RtcTick.Divk[1] = 5*13; //k=200ms/(1000ms*(n+1)/128)
RtcTick.Divk[2] = 10*13;//k=200ms/(1000ms*(n+1)/128)
//***************************************************************
//pISR_TICK (*(unsigned *)(_ISR_STARTADDRESS+0x24))
// _ISR_STARTADDRESS+0x24 等于 Rtc_Tick 涵数的地址!
//***************************************************************
pISR_TICK=(unsigned)RTC_ISP_Tick;//中断向量地址转化
//*(unsigned *)(_ISR_STARTADDRESS+0x24)=(unsigned)Rtc_Tick;
//***************************************************************
rINTMSK=~(BIT_GLOBAL|BIT_TICK);
}
|