//interfacing ds12c887 real time clock using p89c61x2bn
/*pin out connections of the rtc with the micro controller.
micro controller <--> rtc
-------------------------
P0<---->ad0-ad7
!cs<---P2.5
ALE(pin 30)---->AS
!RD(pin16)----->Data strobe
!WR(pin17)----->R/W
--------------------------
other rtc connections;
MOT,--->GND; intel bus timimg is selected.
RST-->Vcc; rest all i have left as no connection(SQW and IRQ).

note: the other micro controller pins such as 
!PSEN , RST, XLAT0, XLAT1 and the max232 connections 
are made accordingly. and i am pretty sure about them*/
//program
#include<reg51.h>
#include<absacc.h>
volatile char xdata OSC _at_ 0x800a;//register a
volatile char xdata UPDATE _at_ 0x800b;//register b
volatile char xdata SEC _at_ 0x8000;
volatile char xdata HR _at_ 0x8004;
volatile char xdata MIN _at_ 0x8002;
volatile char xdata DD _at_ 0x8007;
volatile char xdata MM _at_ 0x8008;
volatile char xdata YY _at_ 0x8009;
sbit rtcce=P2^5;
#define output P0
void bcdconvert(unsigned x);
void serialsend(unsigned x);
void Delay(unsigned int);
unsigned char second,hour,minute;
void main(void)
{
	TMOD=0x20;//serial communication settings
	TH1=0xfd;
	SCON=0x50;
	TR1=1;
	Delay(200);
	rtcce=1;

	OSC=0X2f;//to turn on the oscillator
        //RTC is already initialised & time is set.
	while(1){
	rtcce=0;//here comes the problem whether it  
	//is high or low. 
        //the micro controller can read RTC.
	
        while( OSC & 0x80 );//to monitor uip
	   	second=SEC;
		hour=HR;
		minute=MIN;

		bcdconvert(hour);
		serialsend(':');
		bcdconvert(minute);
		serialsend(':');
		bcdconvert(second);
		serialsend(0x0d);
		serialsend(0x0a);
						  
	}
}
void bcdconvert(unsigned mybyte){
	unsigned  char x,y;
	x=mybyte&0x0f;
	x=x|0x30;
	y=mybyte&0xf0;
	y=y>>4;
	y=y|0x30;
	serialsend(y);
	serialsend(x);
	}
void serialsend(unsigned x){
	SBUF=x;	
	while(TI==0);
	TI=0;
	}
void Delay(unsigned int itime){
	unsigned int i,j,k;
	for(i=0;i<=itime;i++)
		for(j=0;j<=0xff;j++)
			for(k=0;k<=0x08;k++);
	}

//end of program
