📄 rtc.c
字号:
/*************************************
;Ade7169 demo program
;*************************************
;AUTHOR: Su RuTong
;DATE: 03 09 2006
;*************************************/
#include "hal.h"
#include "utilities.h"
#include "sys_init.h"
#include "public.h"
#include "RTC.h"
#include "sys_event.h"
#include "UART.h"
#include "storage.h"
#define IsLeapYear(year) ((year%4)==0)
//__code const unsigned char DataStr[]=__DATE__;
//__code const unsigned char TimeStr[]=__TIME__;
/*
typedef struct{
unsigned char HTHSecond;
unsigned char Second;
unsigned char Munite;
unsigned char Hour;
//unsigned char Day;
//unsigned char Week;
//unsigned char Month;
//unsigned int Year;
//unsigned char TimeCs;
}RTCTime;
*/
/*
typedef union{
};
*/
TimeType data RTCTime;
SysTimeType idata Systime; //System[2];
void SetupRTC(void)
{
IEIP2 &= ~(BIT2);//ETI = 0; // Disable RTC interrupt
//TIMECON &= ~(BIT0);// TIMECON_bit.RTCEN=0;
TIMECON = 0;
//waits 62.5us at 4.096MHz
delay(100);
// D7 D6 D5.D4 D3 D2 D1 D0
// MIDNIGHT TFH ITS[1:0] SIT ALARM ITEN RTCEN
TIMECON = (BIT6|BIT4|BIT1|BIT0); // Addr:0xA1, RTC configuration
// bit7:Mid night,bit6:24Hour,bit5-4:Interval=1s,bit3:Alarm sel,bit2:Alarm flg,bit1:Interval en,bit0:RTCEN
// HTHSEC = 0; // Addr:0xA2, Hundredth of a second counter
// SEC // Addr:0xA3, Seconds counter
// MIN // Addr:0xA4, Minutes counter
// HOUR // Addr:0xA5, Hours counter
INTVAL = 60; // Addr:0xA6, Alarm interval.Once the number of counts is equal to INTVAL, the ALARM flag is set and a pending RTC interrupt is created.
// Interval = 60s, for munite event.
// RTCCOMP // Addr:0xF6, RTC nominal compensation
// TEMPCAL // Addr:0xF7, RTC temperature compensation
IEIP2 |= BIT2;//ETI = 1; // Enable RTC interrupt
// Load Date from eeprom
// ee_read_bytes(unsigned short addr, unsigned char idata * dat, unsigned char len);
TIMECON &= ~(BIT0);
SEC = SCRATCH1;
MIN = SCRATCH2;
HOUR = SCRATCH3;
TIMECON |= BIT0;
ee_read_bytes(ADDR_OF_DATE,&Systime.Date.Day,6);
}
void GetRTCTime(TimeType idata *buf)
{
//do{
buf->Second=SEC;
buf->Minute=MIN;
buf->Hour=HOUR;
buf->HTHSecond=HTHSEC;
///}while(buf.HTHSecond==0);
if(buf->HTHSecond==0)
{
buf->Second=SEC;
buf->Minute=MIN;
buf->Hour=HOUR;
buf->HTHSecond=HTHSEC;
}
}
/*
void GetRTCDate(TimeType buf)
{
}
*/
void RTCRecover(void)
{
TIMECON &= ~(BIT0);//RTCEN=0;
HTHSEC = Systime.Time.HTHSecond;
SEC = Systime.Time.Second;
MIN = Systime.Time.Minute;
HOUR = Systime.Time.Hour;
TIMECON |= BIT0;//RTCEN=1;
}
/********************************************************************************************/
// softclock
#if 0
__code const unsigned char days_every_month[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
};
void SecondUp(void)
{
unsigned char month_days;
g_time[SPACE_OF_SECOND]++;
if(g_time[SPACE_OF_SECOND]>=60)
{
g_time[SPACE_OF_SECOND]-=60;
g_time[SPACE_OF_MINUTE]++;
}
if(g_time[SPACE_OF_MINUTE]>=60)
{
g_time[SPACE_OF_MINUTE]-=60;
g_time[SPACE_OF_HOUR]++;
}
if(g_time[SPACE_OF_HOUR]>=24)
{
g_time[SPACE_OF_HOUR]-=24;
g_time[SPACE_OF_DAY]++;
month_days=days_every_month[g_time[SPACE_OF_MONTH]];
if(((g_time[SPACE_OF_YEAR]&0x03)==0)&&(g_time[SPACE_OF_MONTH]==1))
{
month_days=29;
}
if(g_time[SPACE_OF_DAY]>=month_days)
{
g_time[SPACE_OF_DAY]-=month_days;
g_time[SPACE_OF_MONTH]++;
}
if(g_time[SPACE_OF_MONTH]>=12)
{
g_time[SPACE_OF_MONTH]-=12;
g_time[SPACE_OF_YEAR]++;
}
if(g_time[SPACE_OF_YEAR]>=100)
{
g_time[SPACE_OF_YEAR]=0;
}
}
}
#endif
/********************************************************************************************/
code const unsigned char DaysOfMonth[]={31,28,31,30,31,30,
31,31,30,31,30,31};
// software calendar,invoke by midnight interrupt
void UpdCalendar(void)
{
unsigned char month_days;
GetRTCTime(&Systime.Time);
// IsLeapYear(year) ((year%4)==0)
month_days=DaysOfMonth[Systime.Date.Month];
if(((Systime.Date.Year&0x03)==0)&&(Systime.Date.Month==1))
{
month_days=29;
}
if(Systime.Date.Day>=month_days)
{
Systime.Date.Month++;
Systime.Date.Day -= month_days;
}
if(Systime.Date.Month>=12)
{
Systime.Date.Year++;
Systime.Date.Month -= 12;
}
if(Systime.Date.Year>=100)
{
Systime.Date.Year=0;
}
ee_write_bytes(ADDR_OF_DATE,&Systime.Date.Day,6);
}
//DATA[3...10]=YY MM DD hh mm ss hs
void _SetRTC(void)
{
TIMECON &= ~(BIT0);//RTCEN=0;
HTHSEC = Systime.Time.HTHSecond=0;//Uart_buf[3];
SEC = Systime.Time.Second=Uart_buf[8];
MIN = Systime.Time.Minute=Uart_buf[7];
HOUR = Systime.Time.Hour=Uart_buf[6];
TIMECON |= BIT0;//RTCEN=1;
Systime.Date.Day = Uart_buf[5];
Systime.Date.Month = Uart_buf[4];
Systime.Date.Year = Uart_buf[3];
ee_write_bytes(ADDR_OF_DATE,&Systime.Date.Day,6);
// ACK
Uart_buf[1]=2;Uart_buf[3]=0x00;
_uart_start_tx();
}
//DATA[3...10]=20YY MM DD hh mm ss hs
void _ReadRTC(void)
{
GetRTCTime(&Systime.Time);
Uart_buf[10]=Systime.Time.HTHSecond;
Uart_buf[9]=Systime.Time.Second;
Uart_buf[8]=Systime.Time.Minute;
Uart_buf[7]=Systime.Time.Hour;
Uart_buf[6]=Systime.Date.Day;
Uart_buf[5]=Systime.Date.Month;
Uart_buf[4]=Systime.Date.Year;
Uart_buf[3]=0x20;
// ACK
Uart_buf[1]=0x09;
Uart_buf[2]=0x01;
_uart_start_tx();
}
// RTC interval timer interrupt service
#ifdef __IAR_SYSTEMS_ICC__
#pragma vector=irtc
__interrupt void _rtc_isr(void)
#else
void _rtc_isr(void) interrupt 10
#endif
{
if((TIMECON&BIT7))//MIDNIGHT
{
TIMECON &= ~(BIT7);//MIDNIGHT = 0;
ADD_EVT(EVT_OF_DAY);
}
else if((TIMECON&BIT2))//(ALARM)
{
TIMECON &= ~(BIT2);//ALARM = 0;
ADD_EVT(EVT_OF_MINUTE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -