Code này có 1 bạn chia sẻ trên DTVN nhưng không có Mô Phỏng Chip vẽ lại và Thêm phần Hẹn Giờ cho các bạn phát triển ứng dụng ^^ Mong các bạn sẽ thích ghé thăm Website Echipkool.com..
#include<reg52.h>
#include<string.h>
#define Bat 1
// Dat lai ten cho cac chan dac biet duoc dung
sbit SCL=P1^0;
sbit SDA=P1^1;
sbit Rs=P1^5;
sbit Rw=P1^6;
sbit En=P1^7;
sbit tang=P3^5;
sbit giam=P3^4;
sbit menu=P3^2;
sbit Bf =P2^7;
sbit Led =P3^0;
unsigned char bcd_dec(unsigned char bcd); // Chuyen doi so BCD sang Thap phan hien thi len LCD
unsigned char dec_bcd(unsigned char dec); // Chuyen doi so thap phan sang BCD dung de nap vao DS1307
unsigned char bcd_dec(unsigned char bcd)
{
return((bcd/16)*10+(bcd%16));
}
unsigned char dec_bcd(unsigned char dec)
{
return((dec/10)*16+(dec%10));
}
void caidat(); // Ham phim bam cai dat ngay gio
void hienngay(); // Ham hien thi ngay thang nam
void hiengio(); // Ham Hien thi Gio Phut Giay
void hien_hengio();// Ham Hien thi Hen Gio
unsigned char mode=0;
unsigned char a_min,a_hour,h_phut1,h_phut2,h_gio1,h_gio2;// Cac bien phuc vu cho hen gio
bit tang0=1,tang1=1,giam0=1,giam1=1,mode0=1,mode1=1;
char giay,phut,gio,h1,h2,m1,m2,s1,s2,ngay,thang,nam,ng1,ng2,th1,th2,n1,n2,thu,day; // cac gia tri
// giay,phut,gio,thu,ngay,thang,nam duoc tach thanh 2 so de hien thi
// Ham tre t(ms)
void delay(int t)
{
int a,b;
for(a=0;a<t;a++)
{
for(b=0;b<125;b++)
{}
}
}
// Ham tre t(us)
void delay_us(int t)
{
int a;
for(a=0;a<t;a++)
{}
}
// CAC CHUONG TRINH GIANH CHO LCD
// Ham kiem tra ban
void ban()
{
P2=0xFF;
Rs=0;
Rw=1;
while(Bf)
{
En=0;
delay_us(50);
En=1;
}
}
// Ham ghi lenh
void lenh(unsigned char x)
{
ban();
P2=x;
Rs=0;
Rw=0;
delay_us(20);
En=1;
delay_us(20);
En=0;
}
// Ham ghi du lieu
void kitu(unsigned char x)
{
ban();
P2=x;
Rs=1;
Rw=0;
delay_us(20);
En=1;
delay_us(20);
En=0;
}
// Ham ghi chuoi
void chuoi(unsigned char *s)
{
char i;
for(i=0;i<strlen(s);i++)
{
kitu(s[i]);
}
}
// Ham ghi cham 1 chuoi
void chuoi_cham(unsigned char *s)
{
char i;
for(i=0;i<strlen(s);i++)
{
kitu(s[i]);
delay(75);
}
}
// Ham khoi tao LCD
void khoitao()
{
lenh(0x38);
lenh(0x01);
lenh(0x0c);
lenh(0x80);
}
// CAC CHUONG TRINH DANH CHO DS1307
// Ham start I2C // Khoi tao Giao Tiep I2C
void start()
{
SCL=1;
SDA=1;
delay(1);
SDA=0;
delay(1);
SCL=0;
SDA=1;
}
// Ham stop I2C // Ket thuc Giao Tiep I2C
void stop()
{
SDA=0;SCL=1;
delay(1);
SDA=1;
delay(1);
}
// Ham ghi 1 byte vao DS1307 // Mater 89xx Ghi 1 Byte vao DS1307
void truyen(unsigned char g)
{
unsigned char tem,i;
for(i=0;i<8;i++)
{
tem=g&0x80;
if(tem==0x80)
SDA=1;
else SDA=0;
SCL=1;
delay(1);
SCL=0;
g=g*2;
}
SCL=1;
delay(1);
SCL=0;
}
// Ham nhan 1 byte // Ham 89xx nhan 1 byte tu DS1307
unsigned char nhan()
{
unsigned char gt=0,i;
for(i=0;i<8;i++)
{
SCL=1;
gt=gt*2;
delay(1);
gt=gt|SDA;
SCL=0;
delay(1);
}
SCL=1;
SDA=1;
delay(1);
SCL=0;
return gt;
}
//Ham ghi gia tri vao 1 dia chi len DS1307
void ghi(unsigned char add,unsigned char gt)
{
start();
truyen(0xd0);
truyen(add);
truyen(gt);
stop();
}
// Ham doc gia tri tu 1 dia chi tu DS1307 xuong
unsigned char doc(unsigned char add)
{
unsigned char value;
start();
truyen(0xd0);
truyen(add);
start();
truyen(0xd1);
value=nhan();
stop();
return value;
}
// CAC CHUONG TRINH GIAO TIEP 89S52-DS1307
//Ham khoi tao gia tri ban dau cho DS1307
void khoitao_DS()
{
unsigned char check;
check=doc(0x10);
if(check!='c')
{
ghi(0x10,'c');
ghi(0x00,(dec_bcd(30))&0x7F);// Giay
ghi(0x01,(dec_bcd(35))&0x7F);// Phut
ghi(0x02,(dec_bcd(14))&0x3F);// Gio
ghi(0x03,dec_bcd(4)); // Thu
ghi(0x04,dec_bcd(1)); // Ngay
ghi(0x05,dec_bcd(5)); // Thang
ghi(0x06,dec_bcd(13)); // Nam
ghi(0x08,dec_bcd(0)); // Hen phut
ghi(0x09,dec_bcd(0)); // Hen Gio
}
}
//Ham doc gia tri thoi gian tu DS1307
void docgio()
{
giay =bcd_dec(doc(0x00));
phut =bcd_dec(doc(0x01));
gio =bcd_dec(doc(0x02));
thu=bcd_dec(doc(0x03));
ngay =bcd_dec(doc(0x04));
thang=bcd_dec(doc(0x05));
nam =bcd_dec(doc(0x06));
a_min=bcd_dec(doc(0x08));
a_hour=bcd_dec(doc(0x09));
}
//Tach gia tri de hien thi
void tach()
{
s1=giay/10+48;s2=giay%10+48;
m1=phut/10+48;m2=phut%10+48;
h1=gio/10+48;h2=gio%10+48;
day=thu+48;
ng1=ngay/10+48;ng2=ngay%10+48;
th1=thang/10+48;th2=thang%10+48;
n1=nam/10+48;n2=nam%10+48;
h_phut1=a_min/10+48;h_phut2=a_min%10+48;
h_gio1=a_hour/10+48;h_gio2=a_hour%10+48;
}
//Ham hien thi thoi gian
void hienthi()
{
docgio(); ///////
tach();
lenh(0x80);
if(thu!=7)
{
chuoi("Thu ");
kitu(day+1);
kitu(' ');
}
else chuoi("CNHAT ");
kitu(ng1);kitu(ng2);
kitu('/');
kitu(th1);kitu(th2);
kitu('/');
chuoi("20");
kitu(n1);kitu(n2);
lenh(0xc0);
chuoi("Time: ");
kitu(h1);kitu(h2);
kitu(':');
kitu(m1);kitu(m2);
kitu(':');
kitu(s1);kitu(s2);
chuoi(" ");
}
//Hien thi thu ngay thang nam
void hienngay()
{
tach();
lenh(0xc0);
if(thu!=7)
{
chuoi("Thu ");
kitu(day+1);
kitu(' ');
}
else chuoi("CNHAT ");
kitu(ng1);kitu(ng2);
kitu('/');
kitu(th1);kitu(th2);
kitu('/');
chuoi("20");
kitu(n1);kitu(n2);
}
//Hien giay phut gio
void hiengio()
{
tach();
lenh(0xc0);
chuoi("Time: ");
kitu(h1);kitu(h2);
kitu(':');
kitu(m1);kitu(m2);
kitu(':');
kitu(s1);kitu(s2);
kitu(' ');
}
// hien hen gio
void hien_hengio()
{
tach();
lenh(0xc0);
chuoi("Alarm: ");
kitu(h_gio1);kitu(h_gio2);
kitu(':');
kitu(h_phut1);kitu(h_phut2);
kitu(':');
}
//------------------------------------------------
void Out_1Hz()
{
start(); /* Start i2c bus */
truyen(0xD0); /* Connect to DS1307 */
truyen(0x07); //vi tri con tro RTC
truyen(0x10);
stop();
}
//----------------------------------------------------------------------
//Ham cai dat gia tri thoi gian
void caidat()
{
if(mode==1) // Gio
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
gio=gio+1;
if(gio==24)
gio=0;
}
if(giam0==0 && giam1==1)
{
gio=gio-1;
if(gio<0)
gio=23;
}
}
else
{
if(mode==2) // Phut
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
phut=phut+1;
if(phut==60)
phut=0;
}
if(giam0==0 && giam1==1)
{
phut=phut-1;
if(phut<0)
phut=59;
}
}
else
{
if(mode==3) // Giay
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
giay=giay+1;
if(giay==60)
giay=0;
}
if(giam0==0 && giam1==1)
{
giay=giay-1;
if(giay<0)
giay=59;
}
}
else
{
if(mode==4) // Thu
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
thu=thu+1;
if(thu==8)
thu=1;
}
if(giam0==0 && giam1==1)
{
thu=thu-1;
if(thu==0)
thu=7;
}
}
else
{
if(mode==5) // Ngay
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
ngay=ngay+1;
if(ngay==32)
ngay=1;
}
if(giam0==0 && giam1==1)
{
ngay=ngay-1;
if(ngay==0)
ngay=31;
}
}
else
{
if(mode==6) // Thang
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
thang=thang+1;
if(thang==13)
thang=1;
}
if(giam0==0 && giam1==1)
{
thang=thang-1;
if(thang==0)
thang=12;
}
}
else
{
if(mode==7) // Nam
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
nam=nam+1;
if(nam==100)
nam=0;
}
if(giam0==0 && giam1==1)
{
nam=nam-1;
if(nam<0)
nam=99;
}
}
if(mode==8) // Gio Alarm
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
a_hour=a_hour+1;
if(a_hour==24)
a_hour=0;
}
if(giam0==0 && giam1==1)
{
a_hour=a_hour-1;
if(a_hour<0)
a_hour=24;
}
}
if(mode==9) // Phut Alarm
{
tang0=tang1;tang1=tang;
giam0=giam1;giam1=giam;
if(tang0==0 && tang1==1)
{
a_min=a_min+1;
if(a_min==60)
a_min=0;
}
if(giam0==0 && giam1==1)
{
a_min=a_min-1;
if(a_min<0)
a_min=60;
}
}
}
}
}
}
}
}
ghi(0x00,(dec_bcd(giay))&0x7F);
ghi(0x01,(dec_bcd(phut))&0x7F);
ghi(0x02,(dec_bcd(gio))&0x3F);
ghi(0x03,dec_bcd(thu));
ghi(0x04,dec_bcd(ngay));
ghi(0x05,dec_bcd(thang));
ghi(0x06,dec_bcd(nam));
ghi(0x08,dec_bcd(a_min));
ghi(0x09,dec_bcd(a_hour));
if(mode==1)
{
lenh(0x80);
chuoi(" CHINH GIO: ");
lenh(0xc0);
hiengio();
}
else
{
if(mode==2)
{
lenh(0x80);
chuoi(" CHINH PHUT: ");
lenh(0xc0);
hiengio();
}
else
{
if(mode==3)
{
lenh(0x80);
chuoi(" CHINH GIAY: ");
lenh(0xc0);
hiengio();
}
else
{
if(mode==4)
{
lenh(0x80);
chuoi(" CHINH THU: ");
lenh(0xc0);
hienngay();
}
else
{
if(mode==5)
{
lenh(0x80);
chuoi(" CHINH NGAY: ");
lenh(0xc0);
hienngay();
}
else
{
if(mode==6)
{
lenh(0x80);
chuoi(" CHINH THANG: ");
lenh(0xc0);
hienngay();
}
else
{
if(mode==7)
{
lenh(0x80);
chuoi(" CHINH NAM: ");
lenh(0xc0);
hienngay();
}
else
{
if(mode==8)
{
lenh(0x80);
chuoi(" CHINH HGIO: ");
lenh(0xc0);
hien_hengio();
}
else
{
if(mode==9)
{
lenh(0x80);
chuoi(" CHINH HPHUT: ");
lenh(0xc0);
hien_hengio();
}
}
}
}
}
}
}
}
}
}
// HAM CHINH
void main()
{
khoitao();
//khoitao_DS();
lenh(0x80);
chuoi_cham("REAL TIME CLOCK ");
lenh(0xc0);
chuoi_cham(" ECHIPKOOL.COM ");
delay(2000);
lenh(0x01);
lenh(0x80);
chuoi_cham("Edit ChipKool");
lenh(0xc0);
chuoi_cham("GMAIL:new.moon9xone");
delay(2000);
lenh(0x01);
while(1)
{
Out_1Hz();//
mode0=mode1;mode1=menu;
if(mode0==0 && mode1==1)
{
mode=mode+1;
if(mode==10)
mode=0;
}
if(mode!=0)
caidat();
else
hienthi();
/*if((gio==a_hour)&&(phut==a_min))
{
Led=Bat;
delay(100);
} */
}
}
Nguồn :EchipKool - Chia sẻ kiến thức - Kết nối đam mê điện tử
Đăng nhận xét