Dong Ho

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Dong Ho as PDF for free.

More details

  • Words: 1,599
  • Pages: 11
EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA]

Ứng dụng ví dụ: Giải pháp đồng hồ dùng C51 với RTC BQ3285 (chuẩn song song) và LCD 16x2 (mode 4bit), dùng ngắt ngoài. Đ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: 1

VCC C

VCC

R8 VCC

8x10K

U2

U4

C3 + 10uF

12M

R1

C1

10K

30pF

1 2 3 4 5 6 7 8

BG RS RW EN

C2 30pF

VCC R2 1K

P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7

10 11 12 13 14 15 16 17

IRQ R/W DS

39 38 37 36 35 34 33 32

P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7

29 30

PSEN ALE/PROG

P3.0/RXD P3.1/TXD P3.2/INTO P3.3/INT1 P3.4/TO P3.5/T1 P3.6/WR P3.7/RD

4 5 6 7 8 9 10 11 1 13 14 15 17 18 21

R/W DS

21 22 23 24 25 26 27 28

P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15

AD0 AD1 AD2 AD3 AD4 AD5 AD6 AD7 MOT CS AS R/W DS RST RCLR

R5 30K

DB4 DB5 DB6 DB7

R4 10K

IRQ SQW X1 X2

19 23

IRQ

2 3 Y2 32k

VBAT

20 BT1

BQ3285 3V 2

D1

EA/VPP XTAL1 XTAL2 RST

1

31 19 18 9

Y1

9 8 7 6 5 4 3 2

VCC

LED

AT89C51 VCC 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

R3 20K

2 RS RW EN

DB4 DB5 DB6 DB7

1

GND VCC VO RS RW E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 LEDA LEDK

VCC

3

VCC

LCD1

VCC 2 Q1

R21 1

LCD1602A

BG

3 10K C1815

VCC

D16 2

LM7805 1

1N4007 1

3

C4

2

+

C5

D2 VO

3 LED C6 +

C7

100uF

0.1uF

R2

2

D13 1

VI

GND

1 2

U1

D15 1N4007 1 2

2

100uF

0.1uF

2.2K

1N4007 1 D17 1N4007

PHAN IT

http:\\www.songquoc.com

1

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA]

Chương trìn điều khiển: //main.c #include #include "BQ3285.H" #include "LCDM4.H" #define DBLED P3_2 extern void SetupEx1(void); void DispTime(struct CTime ti); void DispDate(struct CDate dt); struct CTime ti,ta; struct CDate dt; void main (void) { Delay(60000); SetupEx1(); SetupLcd(); SetupRtc(); Delay(2); DBLED = 1; ClearScreen(); SetBgOn(); ti.second = 57; ti.minute = 59; ti.hour = 23; SetTime(ti); dt.day = 4; dt.date = 28; dt.month = 2; dt.year = 7; SetDate(dt);

//first time use chip

//for set time

//for set date

ta.second = 128; ta.minute = 128; ta.hour = 128; SetAlarm(ta); ti = GetTime(); dt = GetDate(); while(1) { DispTime(ti); DispDate(dt); PHAN IT

http:\\www.songquoc.com

2

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] Delay(20000); } } void DispTime(struct CTime ti) { GoToXY(1,0); WriteNum2(ti.hour); WriteChar(':'); WriteNum2(ti.minute); WriteChar(':'); WriteNum2(ti.second); } void DispDate(struct CDate dt) { GoToXY(0,0); WriteNum2(dt.year); WriteChar('/'); WriteNum2(dt.month); WriteChar('/'); WriteNum2(dt.date); } //lcdm4.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 #define RW P1_2 PHAN IT

http:\\www.songquoc.com

3

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] #define EN

P1_3

void Delay(unsigned int n); extern void SetupLcd(void); 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 WriteChar(unsigned char ch); void ClearScreen(void); //lcdm4.c #include #include "LCDM4.H" void Delay(unsigned int n) { unsigned int j; for(j=0;j
http:\\www.songquoc.com

4

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] void WriteCmd(unsigned char byte) { unsigned char tmp; WaitBusy(); 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) PHAN IT

http:\\www.songquoc.com

5

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] { unsigned char tmp; WaitBusy(); RS = 1; RW = 0; Delay(0); tmp = PortData & 0x0f; tmp = tmp | (ch & 0xf0); PortData = tmp;

//store low nible //write high nible

EN = 1; Delay(0); EN = 0; tmp = PortData & 0x0f; tmp = tmp | ((ch & 0x0f) << 4); PortData = tmp;

//store low nible //write low nible

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 } PHAN IT

http:\\www.songquoc.com

6

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA]

void WriteNible(unsigned char byte) { if(byte < 10) { 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; tmp = tmp | (ac & 0xf0); WriteCmd(tmp);

//protect low nible //write high nible

tmp = PortData & 0x0f; tmp = tmp | ((ac & 0x0f) << 4); WriteCmd(tmp);

//protect low nible //write low nible

} void GoHome() { WriteCmd(RETHOME0); PHAN IT

http:\\www.songquoc.com

7

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] WriteCmd(RETHOME1); Delay(100); } void SetBgOn(void) { BG = 1; } void SetBgOff(void) { BG = 0; } //bq3285.h struct CTime { unsigned char hour; unsigned char minute; unsigned char second; }; struct CDate { unsigned char day; unsigned char date; unsigned char month; unsigned char year; }; void SetupRtc(void); unsigned char ReadRtc(unsigned char addr); void WriteRtc(unsigned char addr, unsigned char ch); struct CTime GetTime(void); struct CDate GetDate(void); struct CTime GetAlarm(void); void SetTime(struct CTime ti); void SetDate(struct CDate dt); void SetAlarm(struct CTime ti); //bq3285.c #include #include "BQ3285.H" unsigned char xdata bt[128]; unsigned char DecToBcd(unsigned char dec); unsigned char BcdToDec(unsigned char bcd); void DelayRtc(unsigned int loop); void SetupRtc(void) PHAN IT

http:\\www.songquoc.com

8

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] { unsigned char t; bt[0x0B] = 0x80;

//UTI=1

bt[0x0A] = 0x2F; bt[0x0B] = 0x82; bt[0x0B] = 0xA2;

//UTI=1,DF=0,HF=1 mode date 24h //UTI=1,DF=0,HF=1 mode date 24h

bt[0x0B] = 0x02; bt[0x0B] = 0x22;

//UTI=0,DF=0,HF=1 enable tranfer //UTI=0,DF=0,HF=1 enable tranfer

disable

tranfer // //

t = bt[0x0C]; } unsigned char ReadRtc(unsigned char addr) { return(bt[addr]); } void WriteRtc(unsigned char addr, unsigned char ch) { bt[addr] = ch; } void SetTime(struct CTime ti) { bt[0x0B] = 0xA2; bt[0x00] = DecToBcd(ti.second); bt[0x02] = DecToBcd(ti.minute); bt[0x04] = DecToBcd(ti.hour); bt[0x0B] = 0x22; } struct CTime GetTime(void) { struct CTime ti; ti.second = BcdToDec(bt[0x00]); ti.minute = BcdToDec(bt[0x02]); ti.hour = BcdToDec(bt[0x04]); return(ti); } // void SetAlarm(struct CTime ti) { bt[0x0B] = 0xA2; bt[0x01] = DecToBcd(ti.second); bt[0x03] = DecToBcd(ti.minute); bt[0x05] = DecToBcd(ti.hour); bt[0x0B] = 0x22; PHAN IT

//UTI=1,DF=0,HF=1 mode date 24h

//UTI=0,DF=0,HF=1 enable tranfer

//UTI=1,DF=0,HF=1 mode date 24h

//UTI=0,DF=0,HF=1 enable tranfer

http:\\www.songquoc.com

9

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] } struct CTime GetAlarm(void) { struct CTime ti; ti.second = BcdToDec(bt[0x01]); ti.minute = BcdToDec(bt[0x03]); ti.hour = BcdToDec(bt[0x05]); return(ti); } // void SetDate(struct CDate dt) { bt[0x0B] = 0xA2; bt[0x06] = DecToBcd(dt.day); bt[0x07] = DecToBcd(dt.date); bt[0x08] = DecToBcd(dt.month); bt[0x09] = DecToBcd(dt.year); bt[0x0B] = 0x22;

//UTI=1,DF=0,HF=1 mode date 24h

//UTI=0,DF=0,HF=1 enable tranfer

} struct CDate GetDate(void) { struct CDate dt; dt.day = BcdToDec(bt[0x06]); dt.date = BcdToDec(bt[0x07]); dt.month = BcdToDec(bt[0x08]); dt.year = BcdToDec(bt[0x09]); return(dt); } unsigned char DecToBcd(unsigned char dec) { return(((dec/10)<<4)|(dec%10)); } unsigned char BcdToDec(unsigned char bcd) { return((bcd & 0x0f) + (bcd>>4)*10); } void DelayRtc(unsigned int loop) { while(loop--); } //isrex.c #include PHAN IT

http:\\www.songquoc.com

10

EXAMPLE APP: Clock with C51, RTC BQ3285, LCD 16x2 [CKCLOCKEXA] #include "BQ3285.H" #define DBLED P3_2 extern struct CTime ti; extern struct CDate dt; extern unsigned char xdata bt[128]; unsigned char t; void SetupEx1(void) { EX1 = 1; IT1 = 1; EA = 1; } void Ex1Isr(void) interrupt 2 { DBLED = !DBLED; t = bt[0x0C]; if(ti.second++ >= 59) { ti.second = 0; if(ti.minute++ >= 59) { ti.minute = 0; if(ti.hour++ >= 23) { ti.hour = 0; ti = GetTime(); dt = GetDate(); } } } }

Liên h ệ: - Mr Phan Khắc Quang. - Điện thoại: (04) 7731744 – 0912666017 - Email: [email protected] - 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

11

Related Documents

Dong Ho
October 2019 16
Dong Ho
June 2020 19
Dong Ho Qua Lac
May 2020 7
Ho So Quyet Toan Hop Dong
October 2019 15