/*
时钟读写头文件
文件名:RTC.c
*/
#include "Common.h"
#include "IOPortMap.h"
#include "RTC.h"
#include "GM20IOPort.H"
#include "InportMap.h"
#include "Macro.h"
#include "F281x.h"
INLINE VOID RTCEnable();
INLINE VOID RTCDisable();
INLINE VOID WriteByte(UINT value);
INLINE UINT ReadByte();
INLINE VOID RTCWrite(UINT port,UINT value);
INLINE UINT RTCRead(UINT port);
INLINE UINT BCDToASCII(UINT value);
INLINE UINT ASCII2BCD(UINT value);
INLINE VOID SetSecond(UINT second);
INLINE UINT GetSecond();
INLINE VOID SetMinute(UINT minute);
INLINE UINT GetMinute();
INLINE VOID SetHour(UINT hour);
INLINE UINT GetHour();
INLINE VOID SetDay(UINT day);
INLINE UINT GetDay();
INLINE VOID SetMonth(UINT month);
INLINE UINT GetMonth();
INLINE VOID SetYear(UINT year);
INLINE UINT GetYear();
VOID SetDate(TIME date);
TIME GetDate();
VOID OSCBegin();
INLINE VOID WriteByte(UINT value)
{
UINT data,i;
Delay(RTC_DELAY);
RTCIO_Out;
Delay(RTC_DELAY);
data = value;
for(i=0;i<8;i++)
{
if(((data>>i)&0x0001)==0x0001)
{
RTCIO_High;
}else
{
RTCIO_Low;
}
NOP;
RTCCLK_Low;
Delay(RTC_DELAY);
RTCCLK_High;
Delay(RTC_DELAY);
}
}
INLINE UINT ReadByte()
{
UINT data,i;
Delay(RTC_DELAY);
RTCIO_IN;
Delay(RTC_DELAY);
data = 0;
for(i=0;i<8;i++)
{
Delay(RTC_DELAY);
RTCCLK_High; //Clock 1
Delay(RTC_DELAY);
RTCCLK_Low; //Clock 0
data = data>>1;
if(ifRTCIO)
{
data = data|0x0080;
}else
{
data = data|0x0000;
}
}
return data;
}
INLINE VOID RTCEnable()
{
RTCCLK_Low; //Clock = 0
Delay(RTC_DELAY);
RTCCS_High; //CS = 1
Delay(RTC_DELAY);
}
INLINE VOID RTCDisable()
{
RTCCS_Low;
Delay(RTC_DELAY);
RTCCLK_Low;
Delay(RTC_DELAY);
}
INLINE UINT RTCRead(UINT port)
{
UINT value;
RTCEnable();
WriteByte(0x008E);
WriteByte(0x0000);
RTCDisable();
RTCEnable();
WriteByte(port);
value = ReadByte();
RTCDisable();
RTCEnable();
WriteByte(0x008E);
WriteByte(0x0080);
RTCDisable();
return value;
}
INLINE VOID RTCWrite(UINT port,UINT value)
{
RTCEnable();
WriteByte(0x008E);
WriteByte(0x0000);
RTCDisable();
RTCEnable();
WriteByte(port);
WriteByte(value);
RTCDisable();
RTCEnable();
WriteByte(0x008E);
WriteByte(0x0080);
RTCDisable();
}
//BCD TO ASCII
INLINE UINT BCDToASCII(UINT value)
{
UINT valueH,valueL;
valueH = (value>>4)&0X0007;
valueL = value&0X000F;
value = valueH*10+valueL;
return value;
}
INLINE UINT ASCII2BCD(UINT value)
{
UINT bcd_data;
UINT temp;
temp=value%100;
bcd_data=((UINT)value)/100<<8;
bcd_data=bcd_data|temp/10<<4;
bcd_data=bcd_data|temp%10;
return bcd_data;
}
INLINE VOID SetSecond(UINT second)
{
RTCWrite(0x0080,ASCII2BCD(second));
}
INLINE UINT GetSecond()
{
UINT second;
second = RTCRead(0x0081);
second = BCDToASCII(second);
return second;
}
INLINE VOID SetMinute(UINT minute)
{
RTCWrite(0x0082,ASCII2BCD(minute));
}
INLINE UINT GetMinute()
{
UINT minute;
minute = RTCRead(0x0083);
minute = BCDToASCII(minute);
return minute;
}
INLINE VOID SetHour(UINT hour)
{
RTCWrite(0x0084,ASCII2BCD(hour));
}
INLINE UINT GetHour()
{
UINT hour;
hour = RTCRead(0x0085);
hour = BCDToASCII(hour);
return hour;
}
INLINE VOID SetDay(UINT day)
{
RTCWrite(0x0086,ASCII2BCD(day));
}
INLINE UINT GetDay()
{
UINT day;
day = RTCRead(0x0087);
day = BCDToASCII(day);
return day;
}
INLINE VOID SetMonth(UINT month)
{
RTCWrite(0x0088,ASCII2BCD(month));
}
INLINE UINT GetMonth()
{
UINT month;
month = RTCRead(0x0089);
month = BCDToASCII(month);
return month;
}
INLINE VOID SetYear(UINT year)
{
RTCWrite(0x008C,ASCII2BCD(year));
}
INLINE UINT GetYear()
{
UINT year;
year = RTCRead(0x008D);
year = BCDToASCII(year);
return year;
}
VOID OSCBegin()
{
UINT second;
second = GetSecond();
second = second&0x007F;
SetSecond(second);
}
VOID SetDate(TIME date)
{
SetSecond(date.second);
SetMinute(date.minute);
SetHour(date.hour);
SetDay(date.day);
SetMonth(date.month);
SetYear(date.year);
}
TIME GetDate()
{
TIME date;
date.second = GetSecond();
date.minute = GetMinute();
date.hour = GetHour();
date.day = GetDay();
date.month = GetMonth();
date.year = GetYear();
return date;
}