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 RTC with 8051 ? comes while printing

Status
Not open for further replies.

td micro

Member level 5
Joined
Jun 26, 2012
Messages
86
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,929
hi
i our project,we have connected 89lv51 with RTC DS1307.
here we set the time as
time23:59:36
date31/10/12
and displayed on hyperterminal.at that time, we are getting the time.it is incrementing and displayed on hyper.but some times, it is showing ?.
Code:
time23:59:36
date31/10/12

time23:59:37
date31/10/12

time??:??:??
date??/??/??

time??:??:??
date??/??/??

time23:59:40
date31/10/12

time23:59:41
date31/10/12

time??:??:??
date??/??/??
......................................
.................................
..............................
time00:01:51
date01/11/12

time00:01:52
date01/11/12


time??:??:??
date??/??/??

time00:01:54
date01/11/12

time00:01:55
date01/11/12

time00:01:57
date1?/??/??

time??:??:??
date??/??/??

time??:??:??
date??/??/??

time??:??:??
date??/??/??

time00:02:02
date01/11/12

time00:02:03
date01/11/12

time1?:02:05
date??/??/??

time??:??:??
date??/??/??

time??:??:??
date??/??/??

time00:02:09
date01/11/12

time00:02:10
date01/11/12

time00:02:11
date01/11/12

time??:??
in this, time and date are incrementing in correct way.but , some times, '?' is coming..
please help me to solve this...
 

I would suggest posting your code using the code/syntax tags, so we may examine it.

BigDog
 

thanks for reply...

actually there are 2 programs with me.one is written by me and another one is that i got from internet.in both programs, while executing i m getting question mark frequently.
here is the code
Code:
///// main.c
 #include<reg51.h>
#include<stdio.h>
#include<ds1307.h>
#include<serial.h>
#include<delay.h>

unsigned char  RTC_ARR[7];  // Buffer for second,minute,.....,year
void SendByte1(unsigned char);
unsigned char p;
void WriteSerial(unsigned char*);
void SendByte(unsigned char s);
//---------------------------------------
// Main program
//---------------------------------------
 void main(void)
{


	InitSerial();  		// Initialize serial port
	
	//printf("%s",&Int2Day(1));
	   DelayMs(100);
	
	//-----------------------------------
	// Setup time and enable oscillator
	//-----------------------------------
	  
	ReadRTC(&RTC_ARR[0]);        
	RTC_ARR[0] = RTC_ARR[0] & 0x7F;	// enable oscillator (bit 7=0)
	RTC_ARR[1] = 0x59;	// minute = 59
	RTC_ARR[2] = 0x23;	// hour = 05 ,24-hour mode(bit 6=0)
	RTC_ARR[3] = 0x04;	// Day = 1 or sunday
	RTC_ARR[4] = 0x31;	// Date = 30
	RTC_ARR[5] = 0x10;	// month = August
	RTC_ARR[6] = 0x12;	// year = 05 or 2005
	WriteRTC(&RTC_ARR[0]);	// Set RTC
	   DelayMs(100);

	
	while(1)
	{
		  DelayMs(100);
		ReadRTC(&RTC_ARR[0]);        
		  DelayMs(100);
		 	
			 	
	 	//WriteSerial("day");
		//WriteSerial(Int2Day(RTC_ARR[3]));
		//WriteSerial("\r\n");
		WriteSerial("time");
		SendByte1(RTC_ARR[2]); //DelayMs(100);
		WriteSerial(":");
		SendByte1(RTC_ARR[1]);	//DelayMs(100);
		WriteSerial(":");
		SendByte1(RTC_ARR[0]);	//DelayMs(100);
		WriteSerial("\r\n");
		WriteSerial("date");
		SendByte1(RTC_ARR[4]);	 //DelayMs(100);
		WriteSerial("/");
		SendByte1(RTC_ARR[5]);	// DelayMs(100);
		//WriteSerial(Int2Month(RTC_ARR[5]));
		 WriteSerial("/");
		 SendByte1(RTC_ARR[6]);	//DelayMs(100);

		 WriteSerial("\r\n");
	
		 WriteSerial("\r\n");
				
		  DelayMs(500);
	}
}

 void SendByte1(unsigned char value)
{
	unsigned char buf = 0;	
	
	buf = value & 0xF0;		/* Filter for high byte */
	buf = (buf>>4)|(0x30);		/* Convert  to ascii code */

 SendByte(buf);			

	buf = value & 0x0F;		/* Filter for low byte */
	buf = buf | 0x30;       	/* Convert to ascii code */

 SendByte(buf);			
 		
}


/////////////ds1307.c

 #include<reg51.h>
#include<delay.h>
#include<intrins.h>

#define ACK		1
#define NO_ACK	0
#define SLAVE	0xD0
#define WRITE   0x00
#define READ    0x01
#define ERR_ACK 0x01


unsigned char i;

const unsigned char * DayStr[7]  =     {{"Sun"},
				   						{"Mon"},
				   						{"Tue"},
				   						{"Wen"},
				   						{"The"},
				   						{"Fri"},
				   						{"Sat"}};


const unsigned char * MonthStr[12]    ={{"Jan"},
										{"Feb"},
										{"Mar"},
										{"Apr"},
										{"May"},
										{"Jun"},
										{"Jul"},
										{"Aug"},
										{"Sep"},
										{"Oct"},
										{"Nov"},
										{"Dec"}};


sbit SDA  =  P2^1;	// connect to SDA pin (Data)
sbit SCL  =  P2^0;	// connect to SCL pin (Clock)



//-------------------------------
// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
	unsigned char temp;
	temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
	return temp;
	
}

//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
    SDA = 1;
	SCL = 1;
	_nop_();_nop_();   _nop_();_nop_();_nop_();_nop_();
	SDA = 0;
	_nop_();_nop_();  _nop_();_nop_();	_nop_();_nop_();
	SCL = 0;
	_nop_();_nop_();   _nop_();_nop_();	_nop_();_nop_();
}

//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
	SDA = 0;	    	
	_nop_();_nop_();  
	SCL = 1;
	_nop_();_nop_();   
	SDA = 1;	
}

//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{    

	for (i=0;i<8;i++)
	{
        SDA = (Data & 0x80) ? 1:0;
		SCL=1;_nop_();SCL=0;
		Data<<=1;
	}

  	SCL = 1; 
	_nop_();_nop_();
	SCL = 0;

}

//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{
    
    unsigned char Data=0;

    SDA = 1;
	for (i=0;i<8;i++)
	{
		SCL   = 1;	
		Data<<= 1;
		Data  = (Data | SDA);		
		SCL   = 0;
		_nop_(); _nop_();
	}
    
 	if (ACK_Bit == 1)
	SDA = 0; // Send ACK		
	else		
	SDA = 1; // Send NO ACK				

	_nop_();_nop_();
	SCL = 1;		
	_nop_();_nop_(); 
	SCL = 0;
	
	return Data;
}

//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned char Addr)
{
   	unsigned char Data;
	Start();		  
	WriteI2C(0xD0);	 
	WriteI2C(Addr);	 
	Start();		
	WriteI2C(0xD1);	  
	Data = ReadI2C(NO_ACK);	
	Stop();
   	return(Data);
}

//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned char Addr,unsigned char Data)
{
	Start();
	WriteI2C(0xD0);
	WriteI2C(Addr);
	WriteI2C(Data);
	Stop();	
}

//-------------------------------
// Read RTC (all real time)
//-------------------------------
void ReadRTC(unsigned char * buff)
{
	
	
	Start();		  
	WriteI2C(0xD0);	  
	WriteI2C(0x00);	   

	Start();
	WriteI2C(0xD1);	 
	*(buff+0)=ReadI2C(ACK);	// Second
		
			  
	*(buff+1)=ReadI2C(ACK);	// Minute
			   
	*(buff+2)=ReadI2C(ACK);	// hour
			
				  
	*(buff+3)=ReadI2C(ACK);	// Day
			 
				  
	*(buff+4)=ReadI2C(ACK);	// date
				
				  
	*(buff+5)=ReadI2C(ACK);	// month
				 
					 
	*(buff+6)=ReadI2C(NO_ACK);	// year
				 
	Stop();		
}

//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{

	Start();
	WriteI2C(0xD0);
	WriteI2C(0x00);	
	WriteI2C(*(buff+0));	
	WriteI2C(*(buff+1));
	WriteI2C(*(buff+2));
	WriteI2C(*(buff+3));
	WriteI2C(*(buff+4));
	WriteI2C(*(buff+5));
	WriteI2C(*(buff+6));	
	Stop();	
}


char * Int2Day(unsigned char day)
{
	return DayStr[day-1];
}


char * Int2Month(unsigned char month)
{
  return MonthStr[BCD2HEX(month)-1];
}
 

hi,
the above programs works well some times..but after 2 display, it is showing '?' . please guide me to solve the problem.
 

please help me... is there any mistake in my program???
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top