//interfacing ds12c887 real time clock using p89c61x2bn
/*pin out connections of the rtc with the micro controller.
micro controller <--> rtc
P89V51RD2 <--->  DS12C887
-------------------------
P0<---->ad0-ad7
;LCD dbo-db7 connected to P0.
ALE(pin 30)---->AS
!RD(pin16)----->Data strobe
;LCD RS is connected to the !RD pins
!WR(pin17)----->R/W
;LCD RW is connected to the !WR pins
--------------------------
other rtc connections;
MOT,CS,--->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*/
/* desire output:
no access to RTC. no updating of time in hyper terminal window. 
the output i am getting:
accessing RTC, and also updated time in hyperterminal window.*/
//program
#include<reg51.h>
#include<absacc.h>
//whether i used xbyte or xdata, there is no change in //my output
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;
sbit leden=P2^0;
#define output P0
void bcdconvert(unsigned x);
void serialsend(unsigned x);
void Delay(unsigned int);
unsigned char second,hour,minute;
void main(void)
{
	
	rtcce=1;//disable RTC...

	output=0x0fF;
	leden=1;
	leden=0;//disable all leds connected to P0 via //latch. leden is lactch enable.
	Delay(200);//i have given delay of 1 sec appx.
	OSC=0X20;//to turn on oscillator
	
	Delay(200);
	Delay(200);
	TMOD=0x20;//serial communication settings
	TH1=0xfd;
	SCON=0x50;
	TR1=1;
	Delay(200);
	
	OSC=0X2f;//to turn on the square wave
	UPDATE=0X8b;//to stop update

	SEC=0X01;	//set time
	MIN=0X45;	
	HR=0X04;
   	DD=0X28;	//21-02-08
	MM=0X02;
	YY=0X09;

	UPDATE=0X0b;//start update

	while(1){
	while( OSC & 0x80 );//to monitor uip
		second=SEC;
		hour=HR;
		minute=MIN;
		bcdconvert(hour);
		serialsend(':');
		bcdconvert(minute);
		serialsend(':');
		bcdconvert(second);
		serialsend(0x0d);
      serialsend(0x0a);				  
	}
}
//to convert to ascii
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
