C51读写DS12887程序
2005-12-1 15:31:07
C51读写DS12887程序
已用于开发的无线电子衡器上
包括RTC.h及RTC.c
/////////////////////////////////////////
// 接口 RTC.h
////////////////////////////////////////
#i nclude <reg52.h>
//*******DS12887 *******
#define YEAR_IN_RTC 0
#define MONTH_IN_RTC 1
#define DAY_IN_RTC 2
#define HOUR_IN_RTC 3
#define MINUTE_IN_RTC 4
#define SECOND_IN_RTC 5
#define SECOND_ALARM_IN_RTC 6
#define MINUTE_ALARM_IN_RTC 7
#define HOUR_ALARM_IN_RTC 8
#define RTC_SECONDS XBYTE[0xfe00]
#define RTC_SECONDS_ALARM XBYTE[0xfe01]
#define RTC_MINUTES XBYTE[0xfe02]
#define RTC_MINUTES_ALARM XBYTE[0xfe03]
#define RTC_HOURS XBYTE[0xfe04]
#define RTC_HOURS_ALARM XBYTE[0xfe05]
#define RTC_DAYS XBYTE[0xfe07]
#define RTC_MONTHS XBYTE[0xfe08]
#define RTC_YEARS XBYTE[0xfe09]
#define DSA XBYTE[0xfe0a] //DS12887 register A
#define DSB XBYTE[0xfe0b]
#define DSC XBYTE[0xfe0c]
#define DSD XBYTE[0xfe0d]
sbit RTC_CS=P2^0;
bit Init_RTC(void);
bit Second_Interrupt(void);
unsigned char Read_RTC(unsigned char);
void Write_RTC(unsigned char,unsigned char);
/////////////////////////////////////////
// 实现部分 RTC.C
////////////////////////////////////////
#i nclude <reg52.h>
#i nclude<absacc.h>
#i nclude<RTC.h>
unsigned char Read_RTC(unsigned char option)
{
switch(option)
{
case YEAR_IN_RTC:return(RTC_YEARS);break;
case MONTH_IN_RTC:return(RTC_MONTHS);break;
case DAY_IN_RTC:return(RTC_DAYS);break;
case HOUR_IN_RTC:return(RTC_HOURS);break;
case MINUTE_IN_RTC:return(RTC_MINUTES);break;
case SECOND_IN_RTC:return(RTC_SECONDS);break;
}
}
void Write_RTC(unsigned char option,unsigned char j)
{
switch(option)
{
case YEAR_IN_RTC:RTC_YEARS=j;break;
case MONTH_IN_RTC:RTC_MONTHS=j;break;
case DAY_IN_RTC:RTC_DAYS=j;break;
case HOUR_IN_RTC:RTC_HOURS=j;break;
case MINUTE_IN_RTC:RTC_MINUTES=j;break;
case SECOND_IN_RTC:RTC_SECONDS=j;break;
case SECOND_ALARM_IN_RTC:RTC_SECONDS_ALARM=j;break;
case MINUTE_ALARM_IN_RTC:RTC_MINUTES_ALARM=j;break;
case HOUR_ALARM_IN_RTC:RTC_HOURS_ALARM=j;break;
}
}
bit Second_Interrupt(void)
{
if( (DSC&0x20)==0x20 )return(1);
return(0);
}
/* initialize DS12887 */
bit Init_RTC(void)
{
DSA=0x20;
DSB=0x72; //SET=0,*IE=disenable,SQW=disenable ,BCD,NO DSE
DSC=0x00;
ACC=DSD;
//every second interrupt
Write_RTC(HOUR_ALARM_IN_RTC,0xff);
Write_RTC(MINUTE_ALARM_IN_RTC,0xff);
Write_RTC(SECOND_ALARM_IN_RTC,0xff);
//if(ACC&0x80)return(0); //when low bat return 0
//return(1);
/* DSB=0x82; //set SET=1
RTC_HOURS=0x23;
RTC_MINUTES=0x12;
RTC_SECONDS=0x30;
DSB=0x02;
*/
return(1);
}