Professional Documents
Culture Documents
Ứng dụng ví dụ: Giao tiếp C51 với RTC DS1307 và LCD 16x2 (chuẩn I2C).
Đi kèm theo sản phẩm mạch nạp KIT51x (KIT51A, KIT51B, KIT51D,
KIT51F,KIT51G,KITROM).
VCC
VCC
R16 R17
U2 5.6K 5.6K
VCC 31 39
19 EA/VPP P0.0/AD0 38
Y1 18 XTAL1 P0.1/AD1 37
C3 + 9 XTAL2 P0.2/AD2 36
RST P0.3/AD3 35
10uF 12M 1 P0.4/AD4 34
BG P1.0 P0.5/AD5
2 33
RS P1.1 P0.6/AD6
R1 C1 C2 3 32
RW P1.2 P0.7/AD7
4
EN P1.3
10K 30pF 30pF 5 29
6 P1.4 PSEN 30 U3
7 P1.5 ALE/PROG Y2
8 P1.6 1 5
P1.7 32k 2 X1 SDA
21 X2 6
VCC 10 P2.0/A8 22 SCLK
11 P3.0/RXD P2.1/A9 23 3
P3.1/TXD P2.2/A10 VBAT
1
12 24 8 7
R2 13 P3.2/INTO P2.3/A11 25 BT1 VCC SQW/OUT
P3.3/INT1 P2.4/A12 DB4
1K 14 26
P3.4/TO P2.5/A13 DB5
15 27 3V DS1307
P3.5/T1 P2.6/A14 DB6
16 28
2
P3.6/WR P2.7/A15 DB7
17
D1 P3.7/RD
LED AT89C51
VCC
VCC
VCC
3
LCD1
1 R3
GND 2
VCC 3 2 20K
VO 4
RS RS
5
RW RW
6
E EN
7
1
DB0 8
DB1 9
DB2 10
DB3 11
DB4 DB4
12
DB5 DB5
13
DB6 DB6
14
DB7 DB7
15 VCC
LEDA 16
LEDK
2 Q1 R21
LCD1602A 1
BG
3
10K
C1815
VCC
U1
D15 1N4007 LM7805 D2
1 1 2 1 3
2 VI VO
GND
D13
2
1N4007
2 1
D17
1N4007
PHAN IT http:\\www.songquoc.com 1
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
#include <AT89X52.H>
void SetTime(void);
void GetTime(void);
void SetDate(void);
void GetDate(void);
void DispTime(void);
void DispDate(void);
SetBgOn();
second = 56;
minute = 59;
hour = 23;
SetTime();
PHAN IT http:\\www.songquoc.com 2
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
day = 1;
date = 28;
month = 2;
year = 7;
SetDate();
DBLED = 0;
GoToXY(0,0);
WriteNum2(year);
WriteChar('/');
WriteNum2(month);
WriteChar('/');
WriteNum2(date);
WriteNum2(hour);
WriteChar(':');
WriteNum2(minute);
WriteChar(':');
WriteNum2(second);
while(1)
{
//ClearScreen();
GetTime();
GetDate();
DispTime();
DispDate();
Delay(20000);
}
}
void SetTime(void)
{
WriteRtc(0x00,second);
WriteRtc(0x01,minute);
WriteRtc(0x02,hour);
}
void GetTime(void)
{
second = ReadRtc(0x00);
minute = ReadRtc(0x01);
hour = ReadRtc(0x02);
}
void DispTime(void)
{
GoToXY(1,8);
WriteNum2(hour);
PHAN IT http:\\www.songquoc.com 3
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
WriteChar(':');
WriteNum2(minute);
WriteChar(':');
WriteNum2(second);
}
void SetDate(void)
{
WriteRtc(0x03,day);
WriteRtc(0x04,date);
WriteRtc(0x05,month);
WriteRtc(0x06,year);
void GetDate(void)
{
day = ReadRtc(0x03);
date = ReadRtc(0x04);
month = ReadRtc(0x05);
year = ReadRtc(0x06);
}
void DispDate(void)
{
GoToXY(1,0);
WriteNum2(year);
WriteChar('/');
WriteNum2(month);
WriteChar('/');
WriteNum2(date);
}
;ds1307.c
// QuangPK 08/2006
#include <REG52.H>
sbit A0 = P0^0;
sbit A1 = P0^1;
sbit A2 = P0^2;
sbit WP = P0^5;
PHAN IT http:\\www.songquoc.com 4
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
void SetupI2C(void);
void ClockI2C(unsigned int loop);
unsigned char ReadRtc(unsigned char addr);
void WriteRtc(unsigned char addr, unsigned char ch);
void ShiftOut(unsigned char bytout);
void BaseClock(void)
{
ClockI2C(4);
SCL = 1;
ClockI2C(4);
SCL = 0;
}
void StartAccess(void)
{
SCL = 1;
SDA = 1;
ClockI2C(4);
SDA = 0;
ClockI2C(4);
SCL = 0;
}
void StopAccess(void)
{
SDA = 0;
SCL = 1;
ClockI2C(4);
SDA = 1;
ClockI2C(4);
SCL = 0;
void SetupI2C(void)
{
unsigned char i;
A0 = 0;
A1 = 0;
PHAN IT http:\\www.songquoc.com 5
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
A2 = 0;
WP = 0;
SCL = 1;
SDA = 1;
ClockI2C(2);
for(i=0;i<9;i++)
BaseClock();
StartAccess();
}
void NoAck(void)
{
SDA = 1;
BaseClock();
}
void Ack(void)
{
SDA = 0;
BaseClock();
}
bit GetAck(void)
{
unsigned int i;
bit bSda;
SDA = 1;
ClockI2C(2);
SCL = 1;
for(i=0;i<50000;i++)
{
ClockI2C(100);
bSda = SDA;
if(bSda == 0)
break;
}
SCL = 0;
return(!bSda);
}
bit AckPoll(void)
{
unsigned char i;
for(i=0;i<1000;i++)
{
StartAccess();
ShiftOut(SIG24C);
if(GetAck())
PHAN IT http:\\www.songquoc.com 6
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
return(1);
}
return(0);
}
void WaitTwr(void)
{
ClockI2C(4000);
}
bytin = 0;
bytin = bytin | SDA;
for(i=0;i<7;i++)
{
BaseClock();
bytin = bytin << 1;
bytin = bytin | SDA;
}
return(bytin);
}
ShiftOut(((ch/10)<<4)|(ch%10));
GetAck();
StopAccess();
PHAN IT http:\\www.songquoc.com 7
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
StartAccess();
ShiftOut(0xD0);
GetAck();
ShiftOut(addr);
GetAck();
StartAccess();
ShiftOut(0xD1);
GetAck();
ch = ShiftIn();
NoAck();
StopAccess();
return((ch & 0x0f) + (ch>>4)*10);
}
;lcdm4.c
//lcdm4.c
#include <AT89X52.H>
PHAN IT http:\\www.songquoc.com 8
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
#define PortData P2
#define BG P1_0
#define RS P1_1
#define RW P1_2
#define EN P1_3
void WaitBusy(void);
void WriteCmd(unsigned char byte);
void SetBgOff(void);
void GoToXY(unsigned char row,unsigned char col);
void SetBgOn(void);
void SetBgOff(void);
void GoHome();
void WriteHex(unsigned char byte);
void WriteNum(unsigned char num);
void WriteNum2(unsigned char num);
void WaitBusy(void)
{
unsigned char i,nLcdBusy;
PortData = (PortData | 0x80);
RS = 0;
RW = 1;
for(i=0;i<50;i++)
{
while(1)
{
EN = 1;
Delay(0);
nLcdBusy = (PortData & 0x80);
EN = 0;
Delay(0);
if(nLcdBusy == 0)
break;
}
}
PHAN IT http:\\www.songquoc.com 9
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
RS = 0;
RW = 0;
EN = 1;
Delay(0);
EN = 0;
}
void SetupLcd(void)
{
Delay(150);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
EN = 1;
Delay(0);
EN = 0;
WaitBusy();
WriteCmd(FUNCTION0);
WriteCmd(FUNCTION1);
WriteCmd(FUNCTION2);
Delay(0);
WriteCmd(CONTROL0);
WriteCmd(CONTROL1);
Delay(0);
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(0);
WriteCmd(ENTRYMODE0);
WriteCmd(ENTRYMODE1);
}
PHAN IT http:\\www.songquoc.com 10
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
Delay(0);
EN = 1;
Delay(0);
EN = 0;
EN = 1;
Delay(0);
EN = 0;
}
PHAN IT http:\\www.songquoc.com 11
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
void ClearScreen()
{
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(100);
}
void GoHome()
{
WriteCmd(RETHOME0);
WriteCmd(RETHOME1);
Delay(100);
}
void SetBgOn(void)
PHAN IT http:\\www.songquoc.com 12
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]
{
BG = 1;
}
void SetBgOff(void)
{
BG = 0;
}
Liên h ệ:
- Mr Phan Khắc Quang.
- Điện thoại: (04) 7731744 – 0912666017
- Email: phan_it@yahoo.com
- Website: http:\\www.songquoc.com
- Địa chỉ: phòng 206B - nhà G3A - phường Thành Công - quận Ba Đình – Hà
nội.
PHAN IT http:\\www.songquoc.com 13