You are on page 1of 13

EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

Ứ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).

Chương trình dịch Keil.


Sơ đồ mạch điện:

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

D16 1N4007 LED


2 1 C6
C4 + C5 + C7 R2
3

D13
2

1 2 100uF 0.1uF 100uF 0.1uF 2.2K

1N4007
2 1
D17

1N4007

PHAN IT http:\\www.songquoc.com 1
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

Chương trìn điều khiển:


//main.c

#include <AT89X52.H>

#define DBLED P3_2

extern void SetupLcd(void);


extern void Delay(unsigned int n);
extern void WriteChar(unsigned char ch);
extern void ClearScreen(void);
extern void WriteNum(unsigned char num);
void WriteNum2(unsigned char num);
extern void GoToXY(unsigned char row,unsigned char col);
extern void SetBgOn(void);
extern void SetBgOff(void);
extern void GoHome();
extern void WriteHex(unsigned char byte);

extern void SetupI2C(void);


extern unsigned char ReadRtc(unsigned char addr);
extern void WriteRtc(unsigned char addr, unsigned char ch);

void SetTime(void);
void GetTime(void);
void SetDate(void);
void GetDate(void);
void DispTime(void);
void DispDate(void);

unsigned char second,minute,hour,day,date,month,year;

void main (void)


{
Delay(10);
SetupI2C();
SetupLcd();
Delay(2);
DBLED = 1;
ClearScreen();

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 SCL = P0^4;


sbit SDA = P0^3;

sbit A0 = P0^0;
sbit A1 = P0^1;
sbit A2 = P0^2;
sbit WP = P0^5;

#define SIG24C 0xA0

PHAN IT http:\\www.songquoc.com 4
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

void TxChar(unsigned char ch);

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 ClockI2C(unsigned int loop)


{
while(loop--);
}

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);
}

void ShiftOut(unsigned char bytout)


{
unsigned char i;
for(i=0;i<8;i++)
{
if((bytout & 0x80) == 0)
SDA = 0;
else
SDA = 1;
BaseClock();
bytout = bytout << 1;
}
}

unsigned char ShiftIn(void)


{
unsigned char i,bytin;

bytin = 0;
bytin = bytin | SDA;
for(i=0;i<7;i++)
{
BaseClock();
bytin = bytin << 1;
bytin = bytin | SDA;
}
return(bytin);
}

void WriteRtc(unsigned char addr, unsigned char ch)


{
StartAccess();
ShiftOut(0xD0);
GetAck();
ShiftOut(addr);
GetAck();

ShiftOut(((ch/10)<<4)|(ch%10));
GetAck();
StopAccess();

PHAN IT http:\\www.songquoc.com 7
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

unsigned char ReadRtc(unsigned char addr)


{
unsigned char ch;
ch = 0xff;

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>

//#define FUNCTION 0x38


#define FUNCTION0 0x20
#define FUNCTION1 0x20
#define FUNCTION2 0x80

//#define CONTROL 0x0c


#define CONTROL0 0x00
#define CONTROL1 0xC0

//#define CLRSCR 0x01


#define CLRSCR0 0x00
#define CLRSCR1 0x10

//#define ENTRYMODE 0x06


#define ENTRYMODE0 0x00
#define ENTRYMODE1 0x60

//#define RETHOME 0x03


#define RETHOME0 0x00
#define RETHOME1 0x30

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 Delay(unsigned int n)


{
unsigned int j;
for(j=0;j<n;j++);
}

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;
}
}

void WriteCmd(unsigned char byte)


{
unsigned char tmp;
WaitBusy();

PHAN IT http:\\www.songquoc.com 9
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

RS = 0;
RW = 0;

tmp = PortData & 0x0f;


tmp = tmp | (byte & 0xf0);
PortData = tmp;

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);
}

void WriteChar(unsigned char ch)


{
unsigned char tmp;
WaitBusy();
RS = 1;
RW = 0;

PHAN IT http:\\www.songquoc.com 10
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

Delay(0);

tmp = PortData & 0x0f; //store low nible


tmp = tmp | (ch & 0xf0); //write high nible
PortData = tmp;

EN = 1;
Delay(0);
EN = 0;

tmp = PortData & 0x0f; //store low nible


tmp = tmp | ((ch & 0x0f) << 4); //write low nible
PortData = tmp;

EN = 1;
Delay(0);
EN = 0;
}

void WriteNum(unsigned char num)


{
unsigned char digit;
digit = num/100;
WriteChar(digit + '0'); //write tram
digit = num/10;
digit = digit % 10;
WriteChar(digit + '0'); //write chuc
digit = num % 100;
digit = digit % 10;
WriteChar(digit + '0'); //write dvi
}

void WriteNum2(unsigned char num)


{
unsigned char digit;
digit = num/100;
// WriteChar(digit + '0'); //write tram
digit = num/10;
digit = digit % 10;
WriteChar(digit + '0'); //write chuc
digit = num % 100;
digit = digit % 10;
WriteChar(digit + '0'); //write dvi
}

void WriteNible(unsigned char byte)


{
if(byte < 10)
{

PHAN IT http:\\www.songquoc.com 11
EXAMPLE APP: C51 Interface with RTC DS1307 [CKDS1307]

byte = byte + 48;


WriteChar(byte);
}
else
{
byte= byte + 55;
WriteChar(byte);
}
}

void WriteHex(unsigned char byte)


{
unsigned char tbyte;
tbyte = (byte & 0xf0) >> 4;
WriteNible(tbyte); //hi nible
tbyte = byte & 0x0f;
WriteNible(tbyte); //low nible
}

void ClearScreen()
{
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(100);
}

void GoToXY(unsigned char row,unsigned char col)


{
unsigned char ac,tmp;
ac = 0x80;
ac = ac | ((row & 1) << 6);
ac = ac | (col & 15);

tmp = PortData & 0x0f; //protect low nible


tmp = tmp | (ac & 0xf0);
WriteCmd(tmp); //write high nible

tmp = PortData & 0x0f; //protect low nible


tmp = tmp | ((ac & 0x0f) << 4);
WriteCmd(tmp); //write low nible
}

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

You might also like