EXAMPLE APP: Clock with RTC DS1307, LCD 16x2 [CKCLOCKTI

]

Ứng dụng ví dụ: Giao tiếp C51 với RTC DS1307 (chuẩn I2C) và LCD 16x2, dùng ngắt timer. Đi kèm theo sản phẩm mạch nạp KIT51x (KIT51A, KIT51B, KIT51D, KIT51F,KIT51G,KITROM). Chương trình dịch Keil/C. Sơ đồ mạch điện:
VCC

VCC U2 VCC Y1 C3 + 10uF R1 10K C1 30pF 12M C2 30pF BG RS RW EN 31 19 18 9 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 EA/VPP XTAL1 XTAL2 RST P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 P3.0/RXD P3.1/TXD P3.2/INTO P3.3/INT1 P3.4/TO P3.5/T1 P3.6/WR P3.7/RD AT89C51 VCC VCC LCD1 GND VCC VO RS RW E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 LEDA LEDK 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3 VCC R3 2 RS RW EN 20K P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 PSEN ALE/PROG 39 38 37 36 35 34 33 32 29 30 Y2 32k P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 21 22 23 24 25 26 27 28 1 2 3 8 X1 X2 VBAT VCC DS1307 2 SDA SCLK 1 SQW/OUT 5 6 7 U3 R16 5.6K R17 5.6K

VCC R2 1K

DB4 DB5 DB6 DB7

BT1 3V

D1 LED

DB4 DB5 DB6 DB7

VCC 2 Q1 R21 1 3 10K C1815 BG

LCD1602A

1

VCC D15 1N4007 2 1N4007 1 C4 D13 1 2 2 1N4007 1 D17 1N4007 + C5 2 0.1uF 100uF U1 LM7805 1 GND VI VO 3 LED C6 + 100uF C7 0.1uF R2 2.2K D2

1 2

1

D16 2 3

PHAN IT

http:\\www.songquoc.com

1

ClearScreen().day. extern void WriteChar(unsigned char ch). extern void GoHome().EXAMPLE APP: Clock with RTC DS1307.date. extern void SetupTimer0(void). unsigned char ch). unsigned char nTick0. extern void WriteNum(unsigned char num). void DispDate(void). extern void SetupI2C(void). extern void GoToXY(unsigned char row. PHAN IT http:\\www.year. extern unsigned char ReadRtc(unsigned char addr). DBLED = 1. void SetTime(void). SetBgOn().c #include <AT89X52. LCD 16x2 [CKCLOCKTI] Chương trìn điều khiển: //main. extern void Delay(unsigned int n). void GetDate(void).unsigned char col).H> #define DBLED P3_2 extern void SetupLcd(void).minute.com 2 . extern void WriteHex(unsigned char byte). Delay(2). SetupI2C(). /* second = 56. void GetTime(void). SetupTimer0(). extern void SetBgOn(void). hour = 23. nTick0 = 0.songquoc. void main (void) { Delay(10). extern void WriteRtc(unsigned char addr. void DispTime(void).month.hour. void WriteNum2(unsigned char num). SetupLcd(). extern void SetBgOff(void). extern void ClearScreen(void). minute = 59. unsigned char second. void SetDate(void).

month = 2.hour).day). } void SetDate(void) { WriteRtc(0x03. GetDate(). WriteRtc(0x04.date). WriteRtc(0x02. year = 7. Delay(20000). while(1) { DispTime(). LCD 16x2 [CKCLOCKTI] SetTime().0). hour = ReadRtc(0x02). } void DispTime(void) { GoToXY(1. WriteRtc(0x05. WriteNum2(minute). } void GetTime(void) { second = ReadRtc(0x00). WriteRtc(0x01. PHAN IT http:\\www.month). */ GetTime(). SetDate(). WriteNum2(hour).com 3 . minute = ReadRtc(0x01).second). WriteNum2(second).EXAMPLE APP: Clock with RTC DS1307. } } void SetTime(void) { WriteRtc(0x00. day = 1.songquoc. date = 28. DispDate().minute). WriteChar(':'). DBLED = 0. WriteChar(':').

WriteNum2(month). month = ReadRtc(0x05). WriteNum2(year). WriteChar('/'). } void DispDate(void) { GoToXY(0. WriteNum2(date).c #include <AT89X52. year = ReadRtc(0x06).EXAMPLE APP: Clock with RTC DS1307.com 4 . LCD 16x2 [CKCLOCKTI] WriteRtc(0x06.0). WriteChar('/').H> //#define FUNCTION 0x38 #define FUNCTION0 0x20 #define FUNCTION1 0x20 #define FUNCTION2 0x80 //#define CONTROL 0x0c #define CONTROL0 0x00 #define CONTROL1 0xC0 //#define CLRSCR #define CLRSCR0 #define CLRSCR1 0x01 0x00 0x10 //#define ENTRYMODE 0x06 #define ENTRYMODE0 0x00 #define ENTRYMODE1 0x60 //#define RETHOME 0x03 #define RETHOME0 0x00 #define RETHOME1 0x30 #define PortData P2 #define BG P1_0 #define RS P1_1 PHAN IT http:\\www. date = ReadRtc(0x04). } void GetDate(void) { day = ReadRtc(0x03).year).songquoc. } //lcdm4.

EN = 0. PHAN IT http:\\www. void GoToXY(unsigned char row. WaitBusy(). PortData = (PortData | 0x80). void SetBgOff(void).j++). Delay(0). void WriteNum2(unsigned char num). void WriteNum(unsigned char num).com 5 . for(i=0. Delay(0). void SetBgOff(void).j<n.songquoc. } void WaitBusy(void) { unsigned char i. void GoHome(). nLcdBusy = (PortData & 0x80). RW = 1.i++) { while(1) { EN = 1. } } } void WriteCmd(unsigned char byte) { unsigned char tmp.EXAMPLE APP: Clock with RTC DS1307. if(nLcdBusy == 0) break.nLcdBusy. void WriteHex(unsigned char byte).i<50. void Delay(unsigned int n) { unsigned int j. void SetBgOn(void). RS = 0.unsigned char col). for(j=0. RS = 0. LCD 16x2 [CKCLOCKTI] #define RW #define EN P1_2 P1_3 void WaitBusy(void). void WriteCmd(unsigned char byte). RW = 0.

WriteCmd(0x10). } void SetupLcd(void) { Delay(150). EN = 0. WriteCmd(ENTRYMODE1). Delay(0). Delay(0). WaitBusy(). tmp = tmp | (byte & 0xf0). WriteCmd(0x10). WriteCmd(0xe0). WriteCmd(FUNCTION0). WriteCmd(ENTRYMODE0).songquoc. LCD 16x2 [CKCLOCKTI] tmp = PortData & 0x0f.com 6 . WriteCmd(0xe0). WriteCmd(FUNCTION2). WriteCmd(FUNCTION1). EN = 1. Delay(0). PortData = tmp. Delay(5). PHAN IT //store low nible http:\\www.EXAMPLE APP: Clock with RTC DS1307. Delay(0). Delay(5). EN = 1. RW = 0. WriteCmd(CONTROL1). WaitBusy(). RS = 1. Delay(5). tmp = PortData & 0x0f. Delay(0). EN = 0. WriteCmd(CONTROL0). WriteCmd(0xe0). WriteCmd(0x10). Delay(0). } void WriteChar(unsigned char ch) { unsigned char tmp. WriteCmd(CLRSCR1). WriteCmd(CLRSCR0).

EN = 1. digit = digit % 10. } void WriteNum(unsigned char num) { unsigned char digit. //write dvi } void WriteNum2(unsigned char num) { unsigned char digit. PortData = tmp. digit = digit % 10. WriteChar(digit + '0'). WriteChar(byte). tmp = tmp | ((ch & 0x0f) << 4). //write chuc digit = num % 100. LCD 16x2 [CKCLOCKTI] tmp = tmp | (ch & 0xf0). //write dvi } void WriteNible(unsigned char byte) { if(byte < 10) { byte = byte + 48.EXAMPLE APP: Clock with RTC DS1307. //write tram digit = num/10. WriteChar(digit + '0'). digit = num/100. digit = digit % 10. WriteChar(digit + '0'). //write tram digit = num/10. digit = digit % 10. } PHAN IT //store low nible //write low nible //write high nible http:\\www. EN = 1. // WriteChar(digit + '0'). WriteChar(digit + '0'). PortData = tmp.com 7 . WriteChar(digit + '0'). Delay(0). EN = 0.songquoc. EN = 0. tmp = PortData & 0x0f. digit = num/100. //write chuc digit = num % 100. Delay(0).

LCD 16x2 [CKCLOCKTI] else { byte= byte + 55. tmp = tmp | (ac & 0xf0).unsigned char col) { unsigned char ac. //low nible } void ClearScreen() { WriteCmd(CLRSCR0).tmp. ac = ac | ((row & 1) << 6). WriteCmd(tmp).songquoc. } } void WriteHex(unsigned char byte) { unsigned char tbyte. tmp = tmp | ((ac & 0x0f) << 4). Delay(100). WriteNible(tbyte). WriteCmd(tmp). WriteCmd(CLRSCR1). tmp = PortData & 0x0f. ac = ac | (col & 15). WriteChar(byte). WriteCmd(RETHOME1). Delay(100). } void SetBgOn(void) { BG = 1. tmp = PortData & 0x0f. } void GoToXY(unsigned char row.com 8 . tbyte = (byte & 0xf0) >> 4. WriteNible(tbyte). } void GoHome() { WriteCmd(RETHOME0). //hi nible tbyte = byte & 0x0f. ac = 0x80.EXAMPLE APP: Clock with RTC DS1307. } PHAN IT //protect low nible //write high nible //protect low nible //write low nible http:\\www.

ClockI2C(4). sbit WP = P0^5. SCL = 0. PHAN IT http:\\www. } void BaseClock(void) { ClockI2C(4). SDA = 0. unsigned char ReadRtc(unsigned char addr).H> sbit SCL = P0^4. void ShiftOut(unsigned char bytout). void WriteRtc(unsigned char addr. } void StartAccess(void) { SCL = 1. sbit A1 = P0^1. unsigned char ch). sbit SDA = P0^3. void ClockI2C(unsigned int loop) { while(loop--). SCL = 0. SCL = 1. } //ds1307.EXAMPLE APP: Clock with RTC DS1307. ClockI2C(4). SDA = 1. LCD 16x2 [CKCLOCKTI] void SetBgOff(void) { BG = 0. void SetupI2C(void). void ClockI2C(unsigned int loop). sbit A2 = P0^2. ClockI2C(4).com 9 . sbit A0 = P0^0.songquoc. #define SIG24C 0xA0 void TxChar(unsigned char ch).c #include <REG52.

EXAMPLE APP: Clock with RTC DS1307. A0 = 0. SCL = 1.com 10 .i++) BaseClock(). A1 = 0.songquoc. PHAN IT http:\\www. } void Ack(void) { SDA = 0. BaseClock(). BaseClock(). SDA = 1. SCL = 1. ClockI2C(4). StartAccess(). WP = 0. for(i=0.i<9. LCD 16x2 [CKCLOCKTI] } void StopAccess(void) { SDA = 0. A2 = 0. } void NoAck(void) { SDA = 1. } void SetupI2C(void) { unsigned char i. SCL = 0. SCL = 1. SDA = 1. } bit GetAck(void) { unsigned int i. ClockI2C(2). ClockI2C(4). SDA = 1. ClockI2C(2). bit bSda.

i++) PHAN IT http:\\www.i<1000.i++) { ClockI2C(100). bytin = bytin | SDA.i<8. } void ShiftOut(unsigned char bytout) { unsigned char i. else SDA = 1.i<50000. bSda = SDA. bytin = 0. } return(0). } } unsigned char ShiftIn(void) { unsigned char i. } bit AckPoll(void) { unsigned char i. if(bSda == 0) break.i<7. for(i=0. return(!bSda). ShiftOut(SIG24C).bytin.com 11 .EXAMPLE APP: Clock with RTC DS1307. BaseClock().i++) { StartAccess(). LCD 16x2 [CKCLOCKTI] for(i=0.songquoc. bytout = bytout << 1. if(GetAck()) return(1). } void WaitTwr(void) { ClockI2C(4000). } SCL = 0. for(i=0.i++) { if((bytout & 0x80) == 0) SDA = 0. for(i=0.

songquoc. GetAck(). GetAck(). } void WriteRtc(unsigned char addr. StopAccess(). } return(bytin). bytin = bytin << 1. StartAccess(). StartAccess(). GetAck().c #include <AT89X52. } unsigned char ReadRtc(unsigned char addr) { unsigned char ch. LCD 16x2 [CKCLOCKTI] { BaseClock().EXAMPLE APP: Clock with RTC DS1307. ShiftOut(0xD0). ShiftOut(addr). } //isrtimir. extern void GetDate(void). return((ch & 0x0f) + (ch>>4)*10). StopAccess(). GetAck(). ShiftOut(addr).H> #define TICKS0_PER_SEC 15 extern void GetTime(void). ShiftOut(0xD1). GetAck(). bytin = bytin | SDA. GetAck(). NoAck(). ShiftOut(0xD0). PHAN IT http:\\www. ch = ShiftIn().com 12 . ShiftOut(((ch/10)<<4)|(ch%10)). unsigned char ch) { StartAccess(). ch = 0xff.

Website: http:\\www.Điện thoại: (04) 7731744 – 0912666017 .date.com .phường Thành Công . } } } } } Liên h ệ: . TL0 = 1. PHAN IT http:\\www. } void T0Isr(void) interrupt 1 { TH0 = 0.Email: phan_it@yahoo.hour. LCD 16x2 [CKCLOCKTI] extern unsigned char nTick0.songquoc. if(hour++ >= 23) { hour = 0. TR0 = 1.songquoc. ET0 = 1.Địa chỉ: phòng 206B . .year.minute. EA = 1. if(nTick0++ >= TICKS0_PER_SEC) { nTick0 = 0.EXAMPLE APP: Clock with RTC DS1307.nhà G3A . P3_2 = !P3_2.quận Ba Đình – Hà nội. TF0 = 0.day.com . if(minute++ >= 59) { minute = 0. GetTime(). void SetupTimer0(void) { TMOD = (TMOD & 0xf1) | 0x01. if(second++ >= 59) { second = 0.month. GetDate(). extern unsigned char second.com 13 .Mr Phan Khắc Quang.

Sign up to vote on this title
UsefulNot useful