Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ds1307 is not proper output

Status
Not open for further replies.

pradeep.pavai

Newbie level 3
Joined
Aug 2, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,298
i have interfaced ds1307 with 89v51rd2.i am getting the output in uart,but it is wrong.i am getting as time=36:32:0D,Date=1F/1C/3F.can any one help me soon.i have to complete it....

here is the code...

Code:
#include "MF_RC500.h"
#include "MF_Func.h"

//com interrupt server route frandy
void serial_interrupt() interrupt 4 using 1
{   
	if(RI)
 		{
		RI=0;
		com_tmr_rx=5;
		com_r_busy=1;
		datas[0]=SBUF;
		}

	if(TI)
		{       
    		TI=0;
    		TX_RX=0;
   		if(com_t_index<com_t_length)  
   			{
   			TX_RX=1;
				//if(datas[com_t_index++])
      			//SBUF=hextoascii(((datas[com_t_index])>>4)&0x0F);  
				//SBUF=hextoascii((datas[com_t_index])&0x0F);
				//com_t_index++;
				SBUF=(datas[com_t_index++]);
      			}
   		else   			
			{
			com_t_index=com_t_length=0; 
			TX_RX=0;
			}
			
		//SBUF='\r';	
   		}

} 

void Sys_Init(void)
{
     
	RC500CS = 0;
	RC500RST=0;	   
	IE=0x00;
	PCON &= ~0x80;                
	SCON = 0x50;          //mode 1,ren=1              
	
	TMOD=0x21;            //timer 0 is 16 bit
	TH1   = BAUD_9600;
	TL1   = BAUD_9600;
	TR1   = 1;             

	TH0 = TH0_20ms;       //0x2800 time is 60ms
	TL0 = TL0_20ms;
	TR0 = 1;                 //timer 1 run
	ET0 = 1;    
	ET1 = 0;
	ES = 1;    
	EX0=1;
	IT0 = 1;	
    size=0;
    sec_num=block_num/4;     
	EA=1;  
	TX_RX=0;
	//OPEN=0;
	AUXR = 0x02;
}

void timer0_int(void) interrupt 1 using 2
{
	TR0=0;
	TH0=TH0_20ms;
	TL0=TL0_20ms;
	if(com_tmr_rx)	com_tmr_rx--;
	TR0=1;	   
}

void  watch_dog()
{
     WDG=~WDG;
}

void delay_10ms(uchar _10ms)
{              
            while (_10ms--)
            {
            		WDG=!WDG;
                     delay_1ms(10);
            }
}

void delay_20ms(uchar _20ms)
{              
            while (_20ms--)
            {
            		WDG=!WDG;
                     delay_1ms(20);
            }
}

void delay_50ms(uchar _50ms)
{              
            while (_50ms--)
            {
            		WDG=!WDG;
                     delay_1ms(50);
            }
}


void delay_100ms(uchar _100ms){              
            while (_100ms--)
            {
            		WDG=!WDG;
                     delay_1ms(100);
            }
}

void delay_50us(unsigned char _50us)
{
	while(_50us--)
	{
	WDG=!WDG;
   	_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
 	_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
	_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
   	}
}
void delay_1ms(uchar _1ms)
{
  while (_1ms--)
  {	
  	WDG=!WDG;
	delay_50us(25);
  }
}

void main(void)
{ 	
	unsigned int sec, min, hour, date, month, year;
   //uchar sec=0x00, min=0x00, hour=0x00, date=0x00, month=0x00, year=0x00;
//   unsigned int i;
//	RLED=1;   
//	GLED=1;
//	WP=1;
//	SPK=1;
//	WDG = 0;
//	WDG = 1;
	//RC500CS=0;
   	Sys_Init(); 
    //watch_dog();
	DS1307_init();
 //  	RLED=1;
	DS1307_settime(0x01,0x01,0x01);		/* Set Time (hh:mm:ss) */
//	delay_10ms(10);
//	DS1307_setdate(0x01,0x01,0x01);  	/* Set Date (dd/mm/yy) */
   	while (1)
   	{ 	    
		//watch_dog();
			GLED=0;
			delay_10ms(10);
			GLED=1;
		/* Get Date & Time */
		sec   = (DS1307_get(SEC));	
	//	delay_10ms(10);
		min   = (DS1307_get(MIN));	
	//	delay_10ms(10);
		hour  = (DS1307_get(HOUR));
	//	delay_10ms(10);
		date  = (DS1307_get(DATE));
	//	delay_10ms(10);
		month = (DS1307_get(MONTH));
	//	delay_10ms(10);
		year  = (DS1307_get(YEAR));
	//	delay_10ms(10);

		
		
	//	bcd2ascii(sec);
	//	bcd2ascii(min);
	//	bcd2ascii(hour);
	//	bcd2ascii(date);
	//	bcd2ascii(month);
	//	bcd2ascii(year);
		
	
		datas[0] = hextoascii(((BCD2HEX(hour))>>4)&0x0F);
		datas[1] = hextoascii((BCD2HEX(hour))&0x0f);
		datas[2]=':';
		datas[3]=hextoascii(((BCD2HEX(min))>>4)&0x0F);
		datas[4]=hextoascii((BCD2HEX(min))&0x0f);
		datas[5]=':';
		datas[6]=hextoascii(((BCD2HEX(sec))>>4)&0x0F);
		datas[7]=hextoascii((BCD2HEX(sec))&0x0f);
		datas[8]=' ';
		datas[9]=hextoascii(((BCD2HEX(date))>>4)&0x0F);
		datas[10]=hextoascii((BCD2HEX(date))&0x0f);
		datas[11]='/';
		datas[12]=hextoascii(((BCD2HEX(month))>>4)&0x0F);
		datas[13]=hextoascii((BCD2HEX(month))&0x0f);
		datas[14]='/';
		datas[15]=hextoascii(((BCD2HEX(year))>>4)&0x0F);
		datas[16]=hextoascii((BCD2HEX(year))&0x0f);
		datas[17]='\r';
		com_t_index=0;
		com_t_length=18;
		TX_RX=1;
		watch_dog();
		TI=1;
	  /* datas[0]=hextoascii((sec>>4)&0x0F);
		datas[1]=hextoascii((sec)&0x0F);
		datas[2]=':';
		datas[3]=hextoascii((min>>4)&0x0F);
		datas[4]=hextoascii((min)&0x0F);
		datas[5]=':';
		datas[6]=hextoascii((hour>>4)&0x0F);
		datas[7]=hextoascii((hour)&0x0F);
		datas[8]=' ';
		datas[9]=hextoascii((date>>4)&0x0F);
		datas[10]=hextoascii((date)&0x0F);
		datas[11]='/';
		datas[12]=hextoascii((month>>4)&0x0F);
		datas[13]=hextoascii((month)&0x0F);
		datas[14]='/';
		datas[15]=hextoascii((year>>4)&0x0F);
		datas[16]=hextoascii((year)&0x0F);
		datas[17]='\r';
		com_t_index=0;
		com_t_length=18;
		TX_RX=1;
		watch_dog();
		TI=1;*/
	    delay_50ms(50);
	}
}

void DS1307_init(void)
{
	I2C_start();  
	I2C_write(DS1307_ID);	
	I2C_write(0x00);	
	I2C_write(0x00);
	I2C_stop();
	I2C_start();  
	I2C_write(DS1307_ID);	
	I2C_write(0x07);	
	I2C_write(0x10);
	I2C_stop();     
}
unsigned int DS1307_get(unsigned char addr)
{
	unsigned int ret=0;	
	I2C_write(DS1307_ID);   /* Connect to DS1307 */
	I2C_write(addr);		/* Request RAM address on DS1307 */
	
	I2C_start();			/* Start i2c bus */

	I2C_write(DS1307_ID+1);	/* Connect to DS1307 for Read */
	ret = I2C_read();		/* Receive data */
/*	datas[0]=hextoascii((ret>>4)&0x0F);
	datas[1]=hextoascii((ret)&0x0F);
		com_t_index=0;
		com_t_length=2;
		TX_RX=1;
		watch_dog();
		TI=1;*/
	//delay_10ms(10);
	I2C_stop();				/* Stop i2c bus */

   return ((((ret>>4)&0x0F)*10)+(ret&0x0F));;	
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
	I2C_start(); 

	I2C_write(DS1307_ID);	/* connect to DS1307 */ 
	I2C_write(0x00);		/* Request RAM address at 00H */ 

	I2C_write(ss);			/* Write sec on RAM address 00H */
	I2C_write(mm);			/* Write min on RAM address 01H */
	I2C_write(hh);			/* Write hour on RAM address 02H */
	//delay_20ms(20);
	I2C_stop();           	/* Stop i2c bus */
}

void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
 	I2C_start();	

	I2C_write(DS1307_ID);	/* connect to DS1307 */
	I2C_write(0x04);		/* Request RAM address at 04H */ 

	I2C_write(dd);			/* Write date on RAM address 04H */
	I2C_write(mm);			/* Write month on RAM address 05H */
	I2C_write(yy);			/* Write year on RAM address 06H */
	//delay_20ms(20);
	I2C_stop();				/* Stop i2c bus */
}

void I2C_delay(void)
{
	unsigned char i;

	for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
	I2C_delay();

	SCL_1 = 1;		/* Start clock */

	I2C_delay();    

	SCL_1 = 0;		/* Clear SCL */
}

void I2C_start(void)
{
	if(SCL_1)
	SCL_1 = 0;		/* Clear SCL */

	SDA_1 = 1;        /* Set SDA */
	SCL_1 = 1;		/* Set SCL */

	I2C_delay(); 

	SDA_1 = 0;        /* Clear SDA */

	I2C_delay(); 

	SCL_1 = 0;        /* Clear SCL */
}

void I2C_stop(void)
{
	if(SCL_1)	
	SCL_1 = 0;			/* Clear SCL */

	SDA_1 = 0;			/* Clear SDA */
	I2C_delay();

	SCL_1 = 1;			/* Set SCL */

	I2C_delay();

	SDA_1 = 1;			/* Set SDA */
}

bit I2C_write(unsigned char dat)
{
	bit data_bit;		
	unsigned char i;	

	for(i=0;i<8;i++)				/* For loop 8 time(send data 1 byte) */
	{
		data_bit = dat & 0x80;		/* Filter MSB bit keep to data_bit */
		SDA_1 = data_bit;				/* Send data_bit to SDA */

		I2C_clock();      			/* Call for send data to i2c bus */

		dat = dat<<1;  
	}

	SDA_1 = 1;			/* Set SDA */

	I2C_delay();	
	
	SCL_1 = 1;			/* Set SCL */

	I2C_delay();	

	data_bit = SDA_1;   	/* Check acknowledge */
//	I2C_ack();
/*	datas[0]=hextoascii((data_bit));
	 com_t_index=0;
		com_t_length=1;
		TX_RX=1;
		watch_dog();
		TI=1;*/
	SCL_1 = 0;			/* Clear SCL */

	I2C_delay();

	return data_bit;	/* If send_bit = 0 i2c is valid */	
 	
}

unsigned char I2C_read(void)
{
	bit rd_bit;	
	unsigned char i;
	unsigned int dat=0;

	for(i=0;i<8;i++)		/* For loop read data 1 byte */
	{
		I2C_delay();

		SCL_1 = 1;			/* Set SCL */

		I2C_delay(); 

		rd_bit = SDA_1;		/* Keep for check acknowledge	*/
		dat = dat<<1;		
		dat = dat | rd_bit;	/* Keep bit data in dat */
	
		SCL_1 = 0;			/* Clear SCL */
	}
	
	return dat;
}

/*void I2C_ack()
{
	SDA_1 = 0;		// Clear SDA 

	I2C_delay();    

	I2C_clock();	//Call for send data to i2c bus 

	SDA_1 = 1;		// Set SDA 
}

void I2C_noack()
{
	SDA_1 = 1;		// Set SDA 

	I2C_delay();

	I2C_clock();	// Call for send data to i2c bus 

	SCL_1 = 1;		// Set SCL 
}*/

unsigned short int hextoascii (unsigned char c)
{
	unsigned char ch = c;
	unsigned char as1;
	unsigned char as2;

	// take the char and turn it to ASCII 
	
	as1 = ch;
	as1 >>= 4;
	as1 &= 0x0F;
	if ( as1<10 )
		as1 += 48;
	else
		as1 += 55;
		
	as2 = ch;
	as2 &= 0x0F;
	if ( as2<10 )
		as2 += 48;
	else
		as2 += 55;
		
	return( ((unsigned short int)(as1)<<8) + as2 );
	
	
}

unsigned char BCD2HEX(unsigned int bcd)
{
	unsigned char temp;
	temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
	return temp;
	
}

/*unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}*/

void bcd2ascii(unsigned int val)
  {
     unsigned int d1=0,d2=0;
      d1= val & 0xf0; 
      d1=d1 >> 4; 
      d2=val & 0x0f;
      d1=d1  | 0x30; //ascii values                 
      d2=d2 | 0x30;//ascii values..    
	  datas[0]=d1;
		datas[1]=d2;
		com_t_index=0;
		com_t_length=2;
		TX_RX=1;
		watch_dog();
		TI=1;
 }

 unsigned char asciitohex (unsigned char ch)
{
/*	if( (ch < 0x3A) && (ch > 0x2F) )
		return(ch - 0x30);
	
	if( (ch < 0x47 ) && (ch > 0x40) )
		return(ch - 0x37); 
if( (ch < 0x67 ) && (ch > 0x60) )
		return(ch - 0x57);	
	
	return(0xff);*/
	if((ch>=48)&&(ch<=57))
		return(ch-'0');
	else
		return(ch-'A'+10);
	
}
 

i have a program that works well try this program
Code:
#include<reg51.h>
#include<intrins.h>

sbit rs=P0^0;
sbit en=P0^1;

sbit sda=P3^6;
sbit scl=P3^7;

unsigned char rtc_adr[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06};
unsigned char rtc_dat[8]={0x20,0x58,0x23,0x05,0x31,0x12,0x03};
unsigned char dis[7]={0x20,0x20,0x28,0x08,0x0c,0x06,0x80};
unsigned char a,b,c,d;

void strt_bt();
void stp_bt();
void dta_wr();
void dta_rd();					
void cmdwrt();
void datawrt();
void delay();
void rtc_wr();
void rtc_rd();
void display();

main()
{
	int i;
	for (i=0;i<7;i++)
	{
		a=dis[i];
		cmdwrt();
		delay();
	}

	P0=0x00;

	for(i=0;i<8;i++)
	{
		b=rtc_adr[i];
		c=rtc_dat[i];
		rtc_wr();
	}
                                                                         
	while(1)
	{
		a=0x80;
		cmdwrt();
		delay();

		b=0x04;	//date
		rtc_rd();
		display();

		a='/';
		datawrt();
		delay();

		b=0x05;		//month
		rtc_rd();
		display();

		a='/';
		datawrt();
		delay();

		b=0x06;		//yr
		rtc_rd();
		display();

		a='-';
		datawrt();
		delay();

		b=0x03;		//week
		rtc_rd();
		display();

		a=0xc0;
		cmdwrt();
		delay();

		b=0x02;		//hr
		rtc_rd();
		display();

		a=':';
		datawrt();
		delay();	

		b=0x01;		//min
		rtc_rd();
		display();

		a=':';
		datawrt();
		delay();

		b=0x00;		//sec
		rtc_rd();
		display();
	}

}

void rtc_wr()
{
	strt_bt();        //write

	a=0xd0;
	dta_wr();

	a=b;
	dta_wr();

	a=c;
	dta_wr();

	stp_bt();
	delay();
}

void rtc_rd()
{
	strt_bt();      //dummy write

	a=0xd0;
	dta_wr();

	a=b;
	dta_wr();

	strt_bt(); 		//read
	  
	a=0xd1;
	dta_wr();

	dta_rd();


/*	sda=1;
	scl=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	scl=0;
*/
	stp_bt();
	delay();
}

void strt_bt()
{
sda=1;
scl=1;
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}

void stp_bt()
{
//scl=0;
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
//_nop_();
//_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}

void dta_wr()
{
int j;
char m;
m=a;
sda=1;
for(j=0;j<8;j++)
{
a=m;
a=a & 0x80;
if(a==(0x80))
sda=1;
else 
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
m=m<<1;
}

sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();

scl=0;
delay();
}

void dta_rd()					
{
int j;
int c=0,b;
sda=1;
for(j=0;j<8;j++)
{
a=a<<1;
scl=1;
c=sda;
if(c)
b=0x01;
else
b=0x00;
scl=0;
a=a|b;
}

sda=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
scl=1;
_nop_();
_nop_();
_nop_();	
scl=0;
//datawrt();
delay();
}
                                       
void display()
{
d=a;
a=(a>>4);
a=a & 0x0f;
a=a + 0x30;
datawrt();
delay();

a=d;
a=a & 0x0f;
a=a + 0x30;
datawrt();
delay();
}


void cmdwrt()
{
unsigned char s=0;
s=a;
a=a & 0xf0;
a=a>>2;
rs=0;
P0=a;
rs=0;
en=1;
en=0;
a=s;
a=(a<<4);
a=a & 0xf0;
a=a>>2;
rs=0;
P0=a;
rs=0;
en=1;
en=0;
delay();
}


void datawrt()
{
unsigned char s=0;
s=a;
a=a & 0xf0;
a=a>>2;
rs=1;
//mdata();
//delay();
P0=a;
rs=1;
en=1;
en=0;
a=s;
a=a<<4;
a=a & 0xf0;
a=a>>2;
rs=1;
//mdata();
P0=a;
rs=1;
en=1;
en=0;
delay();
}


void delay()
{
int i;
for(i=0;i<1000;i++);
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top