Đồng hồ số với vi điều khiển 89C51 và RTC DS12C887

Một ứng dụng khá phổ biến của vi điều khiển là tạo một đồng hồ số thời gian thực. Sau đây mình giới thiệu bạn một project ứng dụng 89C51 và RTC DS12C887.

Đồng hồ hiển thị bằng LCD 16X2

Sơ đồ nguyên lý

Video demo !

code :

//Program for Digital clock using RTC DS12C887 and 8051 microcontroller (AT89C51) with time set
/*24 hr clock
set p3^3=0,then start=0 then set time by dig_hr1 & dig_min1, then remove p3^3 & start */
#include"reg51.h"
#include"absacc.h"
#define dataport P2
#define port P1
#define lcdport P3
sbit reset = port^0;
sbit rs =port^1;
sbit rw =port^2;
sbit e = port^3;
sbit dig_hr1=port^4;
sbit dig_min1=port^5;
sbit start=port^6;
int min1=0,hr1=0;
int min0=60,hr0=25;
unsigned char temp=60,hr,min,sec,num[60]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0X0 9,0X10,0X11,0X12,0X13,0X14,0X15,0X16,0X17,0X18,0X1 9,0X20,0X21,0X22,0X23,0X24,0X25,0X26,0X27,0X28,0X2 9,0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X3 9,0X40,0X41,0X42,0X43,0X44,0X45,0X46,0X47,0X48,0X4 9,0X50,0X51,0X52,0X53,0X54,0X55,0X56,0X57,0X58,0X5 9};
void delay(unsigned int msec )
{
int i ,j ;
for(i=0;i23)
hr1=0;
lcd_cmd(0xc3);
lcd_data_int(hr1);
lcd_data(':');
hr0=hr1;
}
void set_min1()
{
min1++;
if(min1>59)
min1=0;
lcd_cmd(0xc6);
lcd_data_int(min1);
min0=min1;
}
void set_time() interrupt 2
{
lcd_cmd(0x01);
if(start==0)
{
lcd_data_string("SET TIMING");
lcd_cmd(0xc3);
lcd_data_int(hr1);
lcd_data(':');
lcd_data_int(min1);
while(start==0)
{
delay(10);
if(dig_hr1==0)
set_hr1();
if(dig_min1==0)
set_min1();
}
}
lcd_cmd(0x01);
hr=num[hr1];
min=num[min1];
set_rtc_time();
lcd_cmd(0x80);
lcd_data_string("TIME:");
hr0=25;
min0=60;
}
bcdconv(unsigned char mybyte)
{
unsigned char x,y;
x= mybyte & 0x0F;
x=x | 0x30;
y= mybyte & 0xF0;
y=y>>4;
y=y | 0x30;
lcd_data(y);
lcd_data(x);
}
void read_rtc_display()
{
reset=0;
reset=1;
XBYTE[11]=0x02;
hr=XBYTE[4];
lcd_cmd(0x87);
min=XBYTE[2];
sec=XBYTE[0];
if(hr!=hr0)
{
lcd_cmd(0X87);
bcdconv(hr);
lcd_data(':');
hr0=hr;
}
if(min!=min0)
{
lcd_cmd(0X8A);
bcdconv(min);
lcd_data(':');
min0=min;
}
if(sec!=temp)
{
lcd_cmd(0x8D);
bcdconv(sec);
temp=sec;
}
}
void main()
{
reset=1;
lcd();
XBYTE[10]=0x20;
XBYTE[1]=0xFF;
XBYTE[3]=0xFF;
XBYTE[5]=0xFF;
XBYTE[11]=0x02;
lcd_cmd(0x01);
IE=0x84;
lcd_cmd(0x80);
lcd_data_string("TIME:");
while(1)
{
read_rtc_display();
}

Từ khóa:
digg delicious stumbleupon technorati Google live facebook Sphinn Mixx newsvine reddit yahoomyweb