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.

[51] problem regarding DS1307 using I2C with AT89C51

Status
Not open for further replies.

jay_3189

Banned
Joined
Sep 19, 2013
Messages
104
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Location
Ahmedabad
Activity points
0
I have written code for real time clock to display time in hyper terminal using c in keil IDE.

but I am not getting perfect time in hyper terminal as I have set through function.
my DS1307 Code is as below.

Code:
#define DS1307_ID 0xD0		// DS1307 ID
#define SEC_ADDRESS	0x00	// Address to access Ds1307 SEC register
#define DATE_ADDRESS 0x04	// Address to access Ds1307 DATE register
#define CONTROL 0x07		// Address to access Ds1307 CONTROL register


void main(void)
{
	
		DS1307_Init();
	              DS1307_SetDate(12, 11, 13);
		serial_init();
		while(1)
	   	{
				DS1307_GetDate(0x04, 0x05, 0x06);//Address to access Date,Month,Year  
				delay_us(5000);
	   	}
}

/*This function is used to set Date(dd,mm,yy) into the Ds1307 RTC.
Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. 
After selecting DS1307, select the RAM address 0x04 to point to day Initilze Day,Month and Year one after the other.
Stop the I2c communication.*/ 
void DS1307_SetDate(unsigned char DD, unsigned char MM, unsigned char YY)
{
	I2C_Start();	// Start I2C communication
	WriteI2C(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus 
	WriteI2C(DATE_ADDRESS); // Request DAY RAM address at 04H

	DD = dec_to_bcd(DD);
	MM = dec_to_bcd(MM);
	YY = dec_to_bcd(YY);	

	WriteI2C(DD);	// Write date on RAM address	04H
	WriteI2C(MM);	// Write month on RAM address 05H
	WriteI2C(YY);	// Write year on RAM address	06h
	I2C_Stop();	// Stop I2C communication after Setting the Date
}

/*This function is used to get the Date(y,m,d) from Ds1307 RTC.
Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
After selecting DS1307, select the RAM address 0x00 to point to DAY Get Day, Month, Year one after the other.
Stop the I2c communication*/
void DS1307_GetDate(unsigned char *D_ptr,unsigned char *M_ptr,unsigned char *Y_ptr)
{
	I2C_Start();	// Start I2C communication
	WriteI2C(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus 
  WriteI2C(DATE_ADDRESS); // Request DAY RAM address at 04H
	I2C_Stop();	// Stop I2C communication after selecting DAY Register

	I2C_Start();	// Start I2C communication
	WriteI2C(0xD1);	// connect to DS1307( under Read mode) by sending its ID on I2c Bus

*D_ptr = ReadI2C(); SDA=0; SCL=1;
*M_ptr = ReadI2C(); SDA=0; SCL=1;
*Y_ptr = ReadI2C(); SDA=1; SCL=1;

*D_ptr = bcd_to_dec(*D_ptr);
*M_ptr = bcd_to_dec(*M_ptr);
*Y_ptr = bcd_to_dec(*Y_ptr);

printf("Date is in dd/mm/yy =  %p/%p/%p\n", *D_ptr, *M_ptr, *Y_ptr);

I2C_Stop();	// Stop I2C communication after readin3g the Time  
}


if you want to see my full code then follow the below link.
https://www.edaboard.com/threads/305721/
 

if you want to see my full code then follow the below link.
The code is different, not only regarding function names.

The read sequence with acknowledge is e.g. incorrect by generating 8 clock pulses instead of 9.
Code:
*M_ptr = ReadI2C(); SDA=0; SCL=1;
 

Capture.JPG
The code is different, not only regarding function names.

The read sequence with acknowledge is e.g. incorrect by generating 8 clock pulses instead of 9.
Code:
*M_ptr = ReadI2C(); SDA=0; SCL=1;


the code is in given link that not worked properly so I have make few changes in DS1307 only. but most of that code is same only.

- - - Updated - - -

What time appears on terminal, and what is the one present on RTC ?
Had you tried compile some other shared examples ?

https://www.edaboard.com/threads/170866/#post718974


+++

yes I have tried this and also others. but in most of case its only for LCD not for Hyper terminal so, I might do some mistakes in changing code.
the output in my hyper terminal is as shown in below figure.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top