8051 based Real time clock is a digital clock to display real time using a RTC DS1307, which works on I2C protocol.. In this project we have used a 16x2 LCD module to display the time in
Trang 1đ ề ể 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 24- hour 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.
Trang 28051 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.
Trang 3Steps 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.
Trang 4K 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
Trang 5châ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 ồ ờ
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
Trang 6void lcdcmd(unsigned char ch)
This function is used for initialise RTC and and read time and date from form RTC IC.
I2CStart();
I2CSend(0xD0);
Trang 8Void I2CStart () {SDA = 1; SCL = 1, SDA = 0, SCL = 0;} // chức năng "bắt đầu" để giaotiếp với ds1307 RTC
Void I2CStop () {SDA = 0, SCL = 1, SDA = 1;} // "chức năng" dừng để truyền thông vớids1307 RTC
Unsigned char I2CSend (unsigned char Data) // gửi dữ liệu đến ds1307
Trang 9Nếu (ack) SDA = 0;
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{
Lcdprint ("Thời gian:");
Sprintf (var, "% d", giờ);
Trang 11Void lcdprint (char * str)
Trang 13{date ++, while (inc == 0);
{mon ++, while (inc == 0); If (
show_time (); } If
(next == 0) {k = 3;
Trang 15Mon = DecimalToBCD (mon);
Ngày = DecimalToBCD (ngày);
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend (0x00); I2CSend (0x00); I2CSend
DecimalToBCD (mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
(mon); Ngày = DecimalToBCD (ngày); I2CStart (); I2CSend (0xD0); I2CSend
Trang 16(0x00); I2CSend (0x00); I2CSend (phút); I2CGi y phép (gi );ấ ờ I2CG i (ngày1);ử I2CG i ử
Giây = BCDToDecimal (I2CRead (1));
Min = BCDToDecimal (I2CRead (1));
Day1 = BCDToDecimal (I2CRead (1));
Ngày = BCDToDecimal (I2CRead (1));
Mon = BCDToDecimal (I2CRead (1));
sbit next=P2^2; //increment digit
sbit inc=P2^3; //increment value
sbit set=P2^4; //set time
Trang 18delay(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.
These functions is used for converting decimal to BCD and BCD to Decimal.
int BCDToDecimal(char bcdByte)
Trang 19char DecimalToBCD (int decimalByte)
The given below functions is used for I2C Communication.
void I2CStart(){SDA=1;SCL=1,SDA=0,SCL=0;} //"start" function for communicatewith ds1307 RTC
void I2CStop(){SDA=0,SCL=1,SDA=1;} //"stop" function for communicatewit ds1307 RTC
unsigned char I2CSend(unsigned char Data) //send data to ds1307
Trang 21sbit next=P2^2; //increment digit
sbit inc=P2^3; //increment value
sbit set=P2^4; //set time
Trang 23if(Data & 0x80) SDA=1;
Trang 25if(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
Trang 26while(k==5) //set hour