Tải bản đầy đủ (.docx) (27 trang)

Đồng hồ kỹ thuật số sử dụng vi điều khiển 8 051

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (293.92 KB, 27 trang )

Đồn g h ồk ỹthu ật s ốs ửd ụng vi đi ều khi ển 8.051
B ởi Saddam 6 Nh ận xét

Đồng hồ
kỹ thuật số sử dụng vi điều khiển 8.051

Trong dự án này, chúng ta s ẽ chứng minh vi ệc tạo ra m ột đồng hồ RTC sử d ụng vi
đi ều khiển 8051 . Nếu b ạn mu ốn làm d ự án này v ới Arduino, hãy ki ểm tra đồng hồ
số này b ằng Arduino . Thành phần chính của dự án này là DS1307 là IC đồng h ồ th ời
gian thực. Cho phép biết chi tiết về IC này.
RTC DS1307 :
The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal
(BCD) clock/calendar plus 56 bytes of NV SRAM. This chip works on I²C protocol. The
clock/calendar provides seconds, minutes, hours, day, date, month, and year
information. The end of the month date is automatically adjusted for months with fewer
than 31 days, including corrections for leap year. The clock operates in either the 24hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense
circuit that detects power failures and automatically switches to the backup supply.
Timekeeping operation continues while the part operates from the backup supply.
DS1307 chip can continuously run till 10 year.


8051 based Real time clock is a digital clock to display real time using a RTC
DS1307, which works on I2C protocol. Real time clock means it runs even after
power failure. When power is reconnected, it displays the real time irrespective to the
time and duration it was in off state. In this project we have used a 16x2 LCD module
to display the time in - (hour, minute, seconds, date, month and year) format. Real
time clocks are commonly used in our computers, houses, offices and electronics
device for keeping them updated with real time.
I2C protocol is a method to connect two or more devices using two wires to a single
system, and so this protocol is also called as two wire protocol. It can be used to
communicate 127 devices to a single device or processor. Most of I2C devices run


on 100Khz frequency.
Steps for data write master to slave (slave receiving mode)
1. Sends START condition to slave.
2. Sends slave address to slave.
3. Send write bit (0) to slave.
4. Received ACK bit from slave
5. Sends words address to slave.
6. Received ACK bit from slave
7. Sends data to slave.
8. Received ACK bit from slave.
9. And last sends STOP condition to slave.


Steps for data reading from slave to master (slave transmitting mode)
1. Sends START condition to slave.
2. Sends slave address to slave.
3. Send read bit (1) to slave.
4. Received ACK bit from slave
5. Received data from slave
6. Received ACK bit from slave.
7. Sends STOP condition to slave.

Circuit Diagram and Description
In circuit we have used 3 most components DS1307, AT89S52 and LCD. Here
AT89S52 is used for reading time from DS1307 and display it on 16x2 LCD screen.
DS1307 sends time/date using 2 lines to the microcontroller.


Kết n ối m ạch đi ện r ất đơn gi ản và đượ c th ể hi ện trong s ơ đồ trên. SDA và SCL chip
DS1307 đượ c k ết n ối v ới các chân P2.1 và P2.0 c ủa vi đi ều khi ển 89S52 v ới đi ện tr ở

kéo lên giữ giá trị mặc định CAO ở d ữ li ệu và đường đồng h ồ. Và bộ dao động tinh
th ể 32.768KHz đượ c k ết n ối v ới DS1307chip để t ạo ra s ự ch ậm tr ễ chính xác 1
giây. Và pin 3 volt cũng đượ c kết nối v ới pin 3 thứ (BAT) của DS1307 mà gi ữ th ời gian
chạy sau khi thất bại đi ện. Màn hình 16x2 đượ c kết nối với 8.051 ở chế độ 4-bit. Các


chân đi ều khi ển RS, RW và En được k ết n ối tr ực ti ếp t ới chân 89S52 P1.0, GND và
P1.1. Và chân dữ liệu D0-D7 được kết nối với P1.4-P1.7 của 89S52.
Ba nút SET, INC / CHANGE và Next được s ử d ụng để thi ết l ập th ời gian đồng h ồ để
pin P2.4, P2.3 và P2.2 của 89S52 (ho ạt động th ấp). Khi nhấn SET, chế độ thời gian
s ẽ kích ho ạt và bây gi ờ chúng ta c ần đặt th ời gian b ằng cách s ử d ụng nút INC /
CHANGE và nút Next để di chuy ển đến ch ữ s ố. Sau khi thiết lập đồng h ồ th ời gian
liên tục.

Mô t ả ch ương trình
Trong mã chúng tôi đã bao g ồm 8.051 gia đình th ư vi ện và m ột đầu vào tiêu chu ẩn
đầu vào thư viện. Và các chân được xác định mà chúng ta đã s ử d ụng, và l ấy m ột s ố
biến cho tính toán.
# Include <reg51.h>
#include <stdio.h>
# Xác định lcdport P1

Sbit rs = P1 ^ 0;
Sbit en = P1 ^ 1;

Sbit SDA = P2 ^ 1;
Sbit SCL = P2 ^ 0;

Sbit next = P2 ^ 2; // số tăng
Sbit inc = P2 ^ 3; // gia tăng giá trị

Sbit đặt = P2 ^ 4; //cài đặt thời gian

Char ack;
Unsigned char day1 = 1;
Unsigned char k, x;
unsigned int date=1, mon=1, hour=0, min=0, sec=0;
int year=0;

void delay(int itime)
{
int i,j;


for(i=0;ifor(j=0;j<1275;j++);
}

And the given function is used for driving LCD.
void daten()
{
rs=1;
en=1;
delay(1);
en=0;
}

void lcddata(unsigned char ch)
{
lcdport=ch & 0xf0;
daten();

lcdport=(ch<<4) & 0xf0;
daten();
}

void cmden(void)
{
rs=0;
en=1;
delay(1);
en=0;
}

void lcdcmd(unsigned char ch)

This function is used for initialise RTC and and read time and date from form RTC IC.
I2CStart();
I2CSend(0xD0);


I2CSend(0x00);
I2CStart();
I2CSend(0xD1);
sec=BCDToDecimal(I2CRead(1));
min=BCDToDecimal(I2CRead(1));
hour=BCDToDecimal(I2CRead(1));
day1=BCDToDecimal(I2CRead(1));
date=BCDToDecimal(I2CRead(1));
mon=BCDToDecimal(I2CRead(1));
year=BCDToDecimal(I2CRead(1));
I2CStop();

Show_time (); // hiển thị thời gian / ngày / ngày
Chậm trễ (1);

Các chức năng này được sử dụng để chuyển đổi số thập phân sang BCD và BCD
thành Số thập phân.
Int BCDToDecimal (char bcdByte)
{
Char a, b, dec;
A = ((bcdByte & 0xF0) >> 4) * 10);
B = (bcdByte & 0x0F);
Dec = a + b;
Trở lại dec;
}

Char DecimalToBCD (int thập phânByte)
{
Char a, b, bcd;
A = ((thập phânByte / 10) << 4);
B = (thập phânByte% 10);
Bcd = a | b;
Trả lại bcd;
}

Các chức năng dưới đây được sử dụng cho truyền thông I2C.


Void I2CStart () {SDA = 1; SCL = 1, SDA = 0, SCL = 0;} // chức năng "bắt đầu" để giao
tiếp với ds1307 RTC
Void I2CStop () {SDA = 0, SCL = 1, SDA = 1;} // "chức năng" dừng để truyền thông với
ds1307 RTC


Unsigned char I2CSend (unsigned char Data) // gửi dữ liệu đến ds1307
{
Char i;
Char ack_bit;
Cho (i = 0; i <8; i ++)
{
Nếu (Dữ liệu & 0x80) SDA = 1;
Khác SDA = 0;
SCL = 1;
Dữ liệu << = 1;
SCL = 0;
}
SDA = 1, SCL = 1;
Ack_bit = SDA;
SCL = 0;
Trở lại ack_bit;
}

Unsigned char I2CRead (char ack) // nhận dữ liệu từ ds1307
{
Unsigned char i, Dữ liệu = 0;
SDA = 1;
Cho (i = 0; i <8; i ++)
{
Dữ liệu << = 1;
Do {SCL = 1;}
Trong khi (SCL == 0);
Nếu (SDA) Dữ liệu | = 1;
SCL = 0;

}


Nếu (ack) SDA = 0;
Khác SDA = 1;
SCL = 1;
SCL = 0;
SDA = 1;
Dữ liệu trở lại;
}

Chức năng set_time được sử dụng để thiết lập thời gian trong đồng hồ và chức năng
show_time dưới đây được sử dụng để hiển thị thời gian trên màn hình LCD.
Void show_time () // function để hiển thị thời gian / ngày / ngày trên màn hình LCD
{
Char var [5];
Lcdcmd (0x80);
Lcdprint ("Ngày:");
Sprintf (var, "% d", ngày tháng);
Lcdprint (var);
Sprintf (var, "/% d", mon);
Lcdprint (var);
Sprintf (var, "/% d", năm 2000);
Lcdprint (var);
Lcdprint ("");
Lcdcmd (0xc0);
Lcdprint ("Thời gian:");
Sprintf (var, "% d", giờ);
Lcdprint (var);
Sprintf (var, ":% d", min);

Lcdprint (var);
Sprintf (var, ":% d", giây);
Lcdprint (var);
Lcdprint ("");
// ngày (ngày1);
Lcdprint ("");
}


Mã s ố:
# Include <reg51.h>
#include <stdio.h>
#define lcdport P1
Sbit rs = P1 ^ 0;
Sbit en = P1 ^ 1;
Sbit SDA = P2 ^ 1;
Sbit SCL = P2 ^ 0;
Sbit next = P2 ^ 2; / / Tăng số
sbit inc = P2 ^ 3; / / Increment giá trị
sbit thiết lập = P2 ^ 4; //cài đặt thời gian
Char ack;
Unsigned char day1 = 1;
Unsigned char k, x;
Unsigned int date = 1, mon = 1, hour = 0, min = 0, sec = 0;
Năm int = 0;
Void delay (int itime)
{
int i, j;
Cho (i = 0; i cho (j = 0; j <1275; j ++);

}
Void daten ()
{
rs = 1;
En = 1;
Chậm trễ (1);
En = 0;
}
Void lcddata (unsigned char ch)
{
lcdport = ch & 0xf0;
Daten ();
Lcdport = (ch << 4) & 0xf0;
Daten ();
}
Void cmden (void)
{
rs = 0;
En = 1;
Chậm trễ (1);
En = 0;
}
Void lcdcmd (unsigned char ch)
{
lcdport = ch & 0xf0;
Cmden ();
Lcdport = (ch << 4) & 0xf0;
Cmden ();
}



Void lcdprint (char * str)
{
while (* str)
{
lcddata (* str);
Str ++;
}
}
Void lcd_init (void)
{
lcdcmd (0x02);
Lcdcmd (0x28);
Lcdcmd (0x0c);
Lcdcmd (0x01);
}
Void I2CStart () {SDA = 1, SCL = 1, SDA = 0, SCL = 0;} // ch ức n ăng "b ắt đầu" để giao ti ếp v ới
ds1307 RTC
void I2CStop () {SDA = 0, SCL = 1, SDA = 1; } // "stop" ch ức n ăng giao ti ếp wit ds1307 RTC
unsigned char I2CSend (unsigned char Data) // gửi d ữ li ệu đến ds1307
{
char i;
Char ack_bit;
Cho (i = 0; i <8; i ++)
{
if (Dữ liệu & 0x80) SDA = 1;
Khác SDA = 0;
SCL = 1;
Dữ liệu << = 1;
SCL = 0;

}
SDA = 1, SCL = 1;
Ack_bit = SDA;
SCL = 0;
Trở lại ack_bit;
}
Unsigned char I2CRead (char ack) // nhận dữ liệu t ừ ds1307
{
unsigned char i, Data = 0;
SDA = 1;
Cho (i = 0; i <8; i ++)
{
Dữ liệu << = 1;
Do {SCL = 1;}
trong khi (SCL == 0);
Nếu (SDA) Dữ liệu | = 1;
SCL = 0;
}
If (ack) SDA = 0;
Khác SDA = 1;
SCL = 1;
SCL = 0;
SDA = 1;


Dữ liệu trở lại;
}
/ * Void day (char d) // Chức năng hiển thị ngày trên màn hình LCD
{
switch (d)

{
trường hợp 0:
lcdprint ("DAY");
phá vỡ;
Trường hợp 1:
lcdprint ("SUN");
phá vỡ;
Trường hợp 2:
lcdprint ("MON");
phá vỡ;
Trường hợp 3:
lcdprint ("TUE");
phá vỡ;
Trường hợp 4:
lcdprint ("WED");
phá vỡ;
Trường hợp 5:
lcdprint ("THU");
phá vỡ;
Trường hợp 6:
lcdprint ("FRI");
phá vỡ;
Trường hợp 7:
lcdprint ("SAT");
phá vỡ;
}
}*/
Int BCDToDecimal (char bcdByte)
{
char a, b, dec;

A = ((bcdByte & 0xF0) >> 4) * 10);
B = (bcdByte & 0x0F);
Dec = a + b;
Trở lại dec;
}
Char DecimalToBCD (int decimalByte)
{
char a, b, bcd;
A = ((thập phânByte / 10) << 4);
B = (thập phânByte% 10);
Bcd = a | b;
Trả lại bcd;
}
Void show_time () // function để hiển thị thời gian / ngày / ngày trên màn hình LCD
{


char var [5];
Lcdcmd (0x80);
Lcdprint ("Ngày:");
Sprintf (var, "% d", ngày tháng);
Lcdprint (var);
Sprintf (var, "/% d", mon);
Lcdprint (var);
Sprintf (var, "/% d", năm 2000);
Lcdprint (var);
Lcdprint ("");
Lcdcmd (0xc0);
Lcdprint ("Thời gian:");
Sprintf (var, "% d", giờ);

Lcdprint (var);
Sprintf (var, ":% d", min);
Lcdprint (var);
Sprintf (var, ":% d", giây);
Lcdprint (var);
Lcdprint ("");
// ngày (ngày1);
Lcdprint ("");
}
khoảng trống set_time () // thời gian thiết lập chức năng
{
lcdcmd (0x0e);
Trong khi (k <7)
{
trong khi (k == 3) // đặt ngày
{
x = năm% 4;
If (inc == 0)
{date ++, while (inc == 0);
If (x == 1 & & mon == 2 && date == 28) {date = 1;} // ki ểm tra cho 28 ngày febuary
if (x == 0 & & == 2 & ngày == 29) {date = 1;} // ki ểm tra ngày febuary 29 ngày
nếu ((date == 31) && (mon == 4) || (mon == 6) || (mon == 9) || (mon == 17)) {Date = 1;} // ki ểm tra
30 ngày tháng
nếu (ngày == 32) {date = 1;} // kiểm tra ngày tháng 31 tháng
show_time ();
} If (next == 0)
{
k = 5;
Trong khi (tiếp theo == 0);
} // kiểm tra số kế tiếp

lcdcmd (0x85);
}
Trong khi (k == 2) // đặt tháng
{
if (inc == 0)
{mon ++, while (inc == 0); If (
mon ==13) {mon = 1;} // kiểm tra vào cuối năm
show_time (); } If
(next == 0) {k = 3;
Trong khi (tiếp theo == 0);


}
Lcdcmd (0x88);
}
Trong khi (k == 1) // set năm
{
if (inc == 0)
{năm ++, trong khi (inc == 0);
năm == 30) {year = 0;}
show_time (); } If
(next == 0) {k = 2;
While
}

If (

(next == 0);} lcdcmd (0x8d);

While (k == 5) // set hour

{
if (inc == 0)
{hour ++, while (inc == 0);
if == 24) {hour = 0;}
show_time ();}
if (next == 0) {k = 6; While
(next == 0);}
lcdcmd (0xc5);
}

If

Trong khi (k == 6) // set min
{
if (inc == 0)
{min ++, while (inc == 0); If
if (min == 60) {min = 0;}
show_time ();}
if (next == 0) {k = 10;
Trong khi (tiếp theo == 0);}
lcdcmd (0xc8);
}
}
}

(next == 0) {k = 6; if (next == 0) {

(next == 0) {k = 10;

Void main ()

{
lcd_init ();
Lcdprint ("đồng hồ kỹ thuật số");
Lcdcmd (0xc0);
Lcdprint ("Sử dụng 8.051");
Chậm trễ (400);
Lcdcmd (1);
Lcdprint ("Circuit Digest");
Lcdcmd (192);
Lcdprint ("Saddam Khan");
Chậm trễ (400);
Trong khi (1)
{
if (set == 0) // kiểm tra thời gian thiết lập nút nhấn
{
I2CStart ();
I2CSend (0xD0);
I2CSend (0x00);


I2CSend (0x00);
I2CSend (0x00);
I2CSend (0x00);
I2CSend (0x01);
I2CSend (0x01);
I2CSend (0x01);
I2CSend (0x00);
I2CSend (0x80);
I2CStop ();
K = 1;

cài đặt thời gian(); / / Gọi thời gian thiết lập chức năng
min = DecimalToBCD (phút);
Giây = DecimalToBCD (0);
Giờ = DecimalToBCD (giờ);
Năm = DecimalToBCD (năm);
Mon = DecimalToBCD (mon);
Ngày = DecimalToBCD (ngày);
I2CStart ();
I2CSend (0xD0);
I2CSend (0x00);
I2CSend (0x00);
I2CSend (phút);
I2CGiấy phép (giờ);
I2CGửi (ngày1);
I2CGửi (ngày);
I2CGửi (mon);
I2CGửi (năm);
I2CSend (0x80);
I2CStop ();
Lcdcmd (1);
Lcdcmd (0x0c);
} K = 1; cài đặt thời gian(); / / Gọi thời gian thiết lập chức năng min = DecimalToBCD (phút); Giây
= DecimalToBCD (0); Giờ = DecimalToBCD (giờ); Năm = DecimalToBCD (năm); Mon =
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
(0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi (ngày1); I2CGửi
(ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd (1); Lcdcmd (0x0c); } K
= 1; cài đặt thời gian(); / / Gọi thời gian thiết lập chức năng min = DecimalToBCD (phút); Giây =
DecimalToBCD (0); Giờ = DecimalToBCD (giờ); Năm = DecimalToBCD (năm); Mon =
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
(0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi (ngày1); I2CGửi

(ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd (1); Lcdcmd (0x0c); } / /
Gọi thời gian thiết lập chức năng min = DecimalToBCD (phút); Giây = DecimalToBCD (0); Giờ =
DecimalToBCD (giờ); Năm = DecimalToBCD (năm); Mon = DecimalToBCD (mon); Ngày =
DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend (0x00); I2CSend (0x00); I2CSend
(phút); I2CGiấy phép (giờ); I2CGửi (ngày1); I2CGửi (ngày); I2CGửi (mon); I2CGửi (năm); I2CSend
(0x80); I2CStop (); Lcdcmd (1); Lcdcmd (0x0c); } / / Gọi thời gian thiết lập chức năng min =
DecimalToBCD (phút); Giây = DecimalToBCD (0); Giờ = DecimalToBCD (giờ); Năm =
DecimalToBCD (năm); Mon = DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart
(); I2CSend (0xD0); I2CSend (0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi
(ngày1); I2CGửi (ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd
(1); Lcdcmd (0x0c); } Giờ = DecimalToBCD (giờ); Năm = DecimalToBCD (năm); Mon =
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
(0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi (ngày1); I2CGửi
(ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd (1); Lcdcmd
(0x0c); } Giờ = DecimalToBCD (giờ); Năm = DecimalToBCD (năm); Mon = DecimalToBCD
(mon); Ngày = DecimalToBCD (ngày);
I2CStart (); I2CSend (0xD0); I2CSend


(0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi (ngày1); I2CGửi
(ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd (1); Lcdcmd
(0x0c); } I2CSend (0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép (giờ); I2CGửi
(ngày1); I2CGửi (ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop (); Lcdcmd
(1); Lcdcmd (0x0c); } I2CSend (0x00); I2CSend (0x00); I2CSend (phút); I2CGiấy phép
(giờ); I2CGửi (ngày1); I2CGửi (ngày); I2CGửi (mon); I2CGửi (năm); I2CSend (0x80); I2CStop
(); Lcdcmd (1); Lcdcmd (0x0c); }
I2CStart ();
I2CSend (0xD0);
I2CSend (0x00);
I2CStart ();

I2CSend (0xD1);
Giây = BCDToDecimal (I2CRead (1));
Min = BCDToDecimal (I2CRead (1));
Giờ = BCDToDecimal (I2CRead (1));
Day1 = BCDToDecimal (I2CRead (1));
Ngày = BCDToDecimal (I2CRead (1));
Mon = BCDToDecimal (I2CRead (1));
Năm = BCDToDecimal (I2CRead (1));
I2CStop ();
Show_time (); // hiển thị thời gian / ngày / ngày
chậm trễ (1);
}
}

Program Description
In code we have included 8051 family library and a standard input output library. And
defined pins that we have used, and taken some variable for calculations.
#include<reg51.h>
#include<stdio.h>
#define lcdport P1

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

sbit SDA=P2^1;
sbit SCL=P2^0;

sbit next=P2^2;

//increment digit


sbit inc=P2^3;

//increment value

sbit set=P2^4;

//set time


char ack;
unsigned char day1=1;
unsigned char k,x;
unsigned int date=1, mon=1, hour=0, min=0, sec=0;
int year=0;

void delay(int itime)
{
int i,j;
for(i=0;ifor(j=0;j<1275;j++);
}

And the given function is used for driving LCD.
void daten()
{
rs=1;
en=1;
delay(1);
en=0;

}

void lcddata(unsigned char ch)
{
lcdport=ch & 0xf0;
daten();
lcdport=(ch<<4) & 0xf0;
daten();
}

void cmden(void)
{
rs=0;
en=1;


delay(1);
en=0;
}

void lcdcmd(unsigned char ch)

This function is used for initialise RTC and and read time and date from form RTC IC.
I2CStart();
I2CSend(0xD0);
I2CSend(0x00);
I2CStart();
I2CSend(0xD1);
sec=BCDToDecimal(I2CRead(1));
min=BCDToDecimal(I2CRead(1));

hour=BCDToDecimal(I2CRead(1));
day1=BCDToDecimal(I2CRead(1));
date=BCDToDecimal(I2CRead(1));
mon=BCDToDecimal(I2CRead(1));
year=BCDToDecimal(I2CRead(1));
I2CStop();
show_time();

//display time/date/day

delay(1);

These functions is used for converting decimal to BCD and BCD to Decimal.
int BCDToDecimal(char bcdByte)
{
char a,b,dec;
a=(((bcdByte & 0xF0) >> 4) * 10);
b=(bcdByte & 0x0F);
dec=a+b;
return dec;
}


char DecimalToBCD (int decimalByte)
{
char a,b,bcd;
a=((decimalByte / 10) << 4);
b= (decimalByte % 10);
bcd=a|b;
return bcd;

}

The given below functions is used for I2C Communication.
void I2CStart(){SDA=1;SCL=1,SDA=0,SCL=0;}
with ds1307 RTC

//"start" function for communicate

void I2CStop(){SDA=0,SCL=1,SDA=1;}
wit ds1307 RTC

//"stop" function for communicate

unsigned char I2CSend(unsigned char Data)

//send data to ds1307

{
char i;
char ack_bit;
for(i=0;i<8;i++)
{
if(Data & 0x80) SDA=1;
else SDA=0;
SCL=1;
Data<<=1;
SCL=0;
}
SDA=1,SCL=1;
ack_bit=SDA;

SCL=0;
return ack_bit;
}

unsigned char I2CRead(char ack)

//receive data from ds1307


{
unsigned char i, Data=0;
SDA=1;
for(i=0;i<8;i++)
{
Data<<=1;
do{SCL=1;}
while(SCL==0);
if(SDA) Data|=1;
SCL=0;
}
if(ack)SDA=0;
else SDA=1;
SCL=1;
SCL=0;
SDA=1;
return Data;
}

The set_time function is used to set the time in clock and show_time function below is
used to display time on LCD.

void show_time()
{
char var[5];
lcdcmd(0x80);
lcdprint("Date:");
sprintf(var,"%d",date);
lcdprint(var);
sprintf(var,"/%d",mon);
lcdprint(var);
sprintf(var,"/%d",year+2000);
lcdprint(var);
lcdprint(" ");
lcdcmd(0xc0);

//function to display time/date/day on LCD


lcdprint("Time:");
sprintf(var,"%d",hour);
lcdprint(var);
sprintf(var,":%d",min);
lcdprint(var);
sprintf(var,":%d",sec);
lcdprint(var);
lcdprint(" ");
// day(day1);
lcdprint(" ");
}

Code:

#include<reg51.h>
#include<stdio.h>
#define lcdport P1
sbit rs=P1^0;
sbit en=P1^1;
sbit SDA=P2^1;
sbit SCL=P2^0;
sbit next=P2^2;
sbit inc=P2^3;
sbit set=P2^4;

//increment digit
//increment value
//set time

char ack;
unsigned char day1=1;
unsigned char k,x;
unsigned int date=1, mon=1, hour=0, min=0, sec=0;
int year=0;
void delay(int itime)
{
int i,j;
for(i=0;ifor(j=0;j<1275;j++);
}
void daten()
{
rs=1;
en=1;

delay(1);


en=0;
}
void lcddata(unsigned char ch)
{
lcdport=ch & 0xf0;
daten();
lcdport=(ch<<4) & 0xf0;
daten();
}
void cmden(void)
{
rs=0;
en=1;
delay(1);
en=0;
}
void lcdcmd(unsigned char ch)
{
lcdport=ch & 0xf0;
cmden();
lcdport=(ch<<4) & 0xf0;
cmden();
}
void lcdprint(char *str)
{
while(*str)
{

lcddata(*str);
str++;
}
}
void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x28);
lcdcmd(0x0c);
lcdcmd(0x01);
}
void I2CStart(){SDA=1;SCL=1,SDA=0,SCL=0;}
ds1307 RTC
void I2CStop(){SDA=0,SCL=1,SDA=1;}
RTC
unsigned char I2CSend(unsigned char Data)
{
char i;
char ack_bit;
for(i=0;i<8;i++)
{

//"start" function for communicate with
//"stop" function for communicate wit ds1307

//send data to ds1307


if(Data & 0x80) SDA=1;
else SDA=0;

SCL=1;
Data<<=1;
SCL=0;
}
SDA=1,SCL=1;
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char I2CRead(char ack)
{
unsigned char i, Data=0;
SDA=1;
for(i=0;i<8;i++)
{
Data<<=1;
do{SCL=1;}
while(SCL==0);
if(SDA) Data|=1;
SCL=0;
}
if(ack)SDA=0;
else SDA=1;
SCL=1;
SCL=0;
SDA=1;
return Data;
}
/*void day(char d)
{

switch(d)
{
case 0:
lcdprint("DAY");
break;
case 1:
lcdprint("SUN");
break;
case 2:
lcdprint("MON");
break;
case 3:
lcdprint("TUE");
break;
case 4:
lcdprint("WED");
break;

//receive data from ds1307

// Function for display day on LCD


case 5:
lcdprint("THU");
break;
case 6:
lcdprint("FRI");
break;
case 7:

lcdprint("SAT");
break;
}
} */
int BCDToDecimal(char bcdByte)
{
char a,b,dec;
a=(((bcdByte & 0xF0) >> 4) * 10);
b=(bcdByte & 0x0F);
dec=a+b;
return dec;
}
char DecimalToBCD (int decimalByte)
{
char a,b,bcd;
a=((decimalByte / 10) << 4);
b= (decimalByte % 10);
bcd=a|b;
return bcd;
}

void show_time()
{
char var[5];
lcdcmd(0x80);
lcdprint("Date:");
sprintf(var,"%d",date);
lcdprint(var);
sprintf(var,"/%d",mon);
lcdprint(var);

sprintf(var,"/%d",year+2000);
lcdprint(var);
lcdprint(" ");
lcdcmd(0xc0);
lcdprint("Time:");
sprintf(var,"%d",hour);
lcdprint(var);
sprintf(var,":%d",min);
lcdprint(var);
sprintf(var,":%d",sec);
lcdprint(var);
lcdprint(" ");

//function to display time/date/day on LCD


// day(day1);
lcdprint(" ");
}
void set_time()
//time set function
{
lcdcmd(0x0e);
while(k<7)
{
while(k==3)
//set date
{
x=year%4;
if(inc==0)

{date++;while(inc==0);
if(x==1 && mon==2 && date==28){date=1;}
//check for 28 day febuary
if(x==0 && mon==2 && date==29){date=1;}
//check for 29 day febuary
if((date==31) && (mon==4) || (mon==6) || (mon==9) || (mon==17)){date=1;}
// check for 30
day month
if(date==32){date=1;}
// check for 31 day month
show_time();}
if(next==0)
{
k=5;
while(next==0);
}
lcdcmd(0x85);
}
while(k==2)
{
if(inc==0)
{mon++;while(inc==0);
if(mon==13){mon=1;}
show_time(); }
if(next==0){k=3;
while(next==0);

//check for next digit

//set month


//check for end of year

}
lcdcmd(0x88);
}
while(k==1)
{
if(inc==0)
{year++;while(inc==0);
if(year==30){year=0;}
show_time(); }
if(next==0){k=2;
while(next==0);}
lcdcmd(0x8d);
}

//set year


×