The DS1307 i2c Serial Real Time Clock which is a low–power chip and has full BCD clock/calendar. The chip has plus 56 bytes of nonvolatile SRAM for data storage. The data communication between micro controller or microprocessor is done by using I2C protocol such that the address and data are transferred serially via the 2–wire bi–directional bus. The clock/calendar of RTC ds1307 provides the time in this sequence ; seconds, minutes, hours, day, date, month, and year. Time and date or year are automatically updated and user also have facility to change the real time to desired one. The DS1307 has a built–in power sense circuit which detects power failures and automatically switches to the battery supply.
KEIL c51 program or code in c language for the interfacing of Real time clock RTC with microcontroller 8051 core is as below.
// DS1307 program for microcontroller at89s51 or at89c2051
//#include
//#include
//#define ack 1
//#define no_ack 0
unsigned char buff[7];
sbit SDA = P1^6; // connect to SDA pin (Data)
sbit SCL = P1^7; // connect to SCL pin (Clock)
// Convert BCD 1 byte to HEX 1 byte sub-routine
unsigned char BCD_2_HEX(unsigned int b)
{
unsigned char temp;
temp=((b>>8)*100)((b>>4)*10)(b&0x0f);
return temp;
}
// I2C start sub-routine
void start_i2c(void)
{
SDA = 1;
SCL = 1;
_nop_();_nop_();
SDA = 0;
_nop_();_nop_();
SCL = 0;
_nop_();_nop_();
}
// I2C stop sub-routine
void stop_i2c(void)
{
SDA = 0;
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SDA = 1;
}
// I2C Write one byte (a) sub-routine
void write_I2C(unsigned char a)
{
for (i=0;i<8;i++)
{
SDA = (a & 0x80) ? 1:0;
SCL=1;SCL=0;
a<<=1;
}
SCL = 1;
_nop_();_nop_();
SCL = 0;
}
//-------------------------------
// 2C read one byte (b) sub-routine
//-------------------------------
unsigned char read_I2C(bit ack)
{
unsigned char b=0;
SDA = 1;
for (i=0;i<8;i++)
{ SCL = 1;
b<<= 1;
b = (b SDA);
SCL = 0;
_nop_();
}
if (ack == 1) SDA = 0; // Send 'ack'
else SDA = 1; // Send 'no ack'
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SCL = 0;
return b;
}
// Read from RTC full data of time that include real time, day , month and year
void readRTC()
{
unsigned int i;
start();
write_I2C(0xD0);
write_I2C(0x00);
start();
write_I2C(0xD1);
for (i=0; i<6;i++)>
buff[7] = read_I2C(no_ack);
stop();
}
// Write to RTC full data of time that include real time, day , month and year
void writeRTC()
{
unsigned int i;
start();
write_I2C(0xD0);
write_I2C(0x00);
for (i=0; i<7;i++)>
write_I2C(buff[i]);
stop();
}
// mSec Delay 11.0592 Mhz
void DelayMs(unsigned int count)
{
unsigned int i;
while(count)
{
i = 115;
while(i>0) i--;
count--;
}
}
void main(void)
{
readRTC(buff[]);
//if you want to update the time and date of RTC then use following code
buff[0] = buff[0] & 0x7F; // enable oscillator (bit 7=0)
buff[1] = 0x01;
buff[2] = 0x01;
buff[3] = 0x01;
buff[4] = 0x01;
buff[5] = 0x01;
buff[6] = 0x01;
writeRTC(buff[]); // Set RTC
while(1)
{
readRTC();
DelayMs(1000); // delay about 1 second
}
}
count--;
}
}
void main(void)
{
readRTC(buff[]);
//if you want to update the time and date of RTC then use following code
buff[0] = buff[0] & 0x7F; // enable oscillator (bit 7=0)
buff[1] = 0x01;
buff[2] = 0x01;
buff[3] = 0x01;
buff[4] = 0x01;
buff[5] = 0x01;
buff[6] = 0x01;
writeRTC(buff[]); // Set RTC
while(1)
{
readRTC();
DelayMs(1000); // delay about 1 second
}
}






real time clock and its iterfacing with microcotroller i was searching from a long time.
ReplyDeletei am very happy to learn from this post on the microcontroller51 website.
|I will now build this project in my home as i am hobbiest of microcontroller programming.
can you please upload the hex file of this project also\/
hex files are always helping in the context that if some one donot have the complier to compile the project.
hope i will find the hexfile of rtc interface with 8051 and dislplay with led digits very soon.
thank !
ReplyDelete