các bác xem giùm em code xem có vấn đề gì không mà sao em gắn pin rồi, mà mỗi lần cắm nguồn thì đồng hồ cứ chạy đúng 9h59' tắt đi bật lại cũng vậy. em cũng đã thử thay con ds1307 nhưng vẫn vậy.
Code:
#include<reg52.h> #include<string.h> unsigned char bcd_dec(unsigned char bcd); // Chuyen doi so BCD-thap phan unsigned char dec_bcd(unsigned char dec); // Chuyen doi so thap phan-BCD 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(); void hienngay(); void hiengio(); unsigned char mode=0; 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 // 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; // 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(50); En=1; delay_us(50); En=0; } // Ham ghi du lieu void kitu(unsigned char x) { ban(); P2=x; Rs=1; Rw=0; delay_us(50); En=1; delay_us(50); 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 GIANH CHO DS1307 // Ham start I2C void start() { SCL=1; SDA=1; delay(1); SDA=0; delay(1); SCL=0; SDA=1; } // Ham stop I2C void stop() { SDA=0;SCL=1; delay(1); SDA=1; delay(1); } // Ham 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 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 void ghi(unsigned char add,unsigned char gt) { start(); truyen(0xd0); truyen(add); truyen(gt); stop(); } // Ham doc gia tri tu 1 dia chi 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 DS1307 void khoitao_DS() { unsigned char check; check=doc(0x08); if(check!='c') { ghi(0x08,'c'); ghi(0x00,(dec_bcd(30))&0x7F); ghi(0x01,(dec_bcd(35))&0x7F); ghi(0x02,(dec_bcd(14))&0x3F); ghi(0x03,dec_bcd(4)); ghi(0x04,dec_bcd(1)); ghi(0x05,dec_bcd(5)); ghi(0x06,dec_bcd(13)); } } //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)); } //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; } //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(' '); } //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; } } } } } } } } 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)); 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(); } } } } } } } } // HAM CHINH void main() { khoitao(); khoitao_DS(); lenh(0x80); chuoi_cham("REAL TIME CLOCK "); lenh(0xc0); chuoi_cham(" DO AN DIEN TU "); delay(2000); lenh(0x01); lenh(0x80); chuoi_cham("By DanhPro"); lenh(0xc0); chuoi_cham("GMAIL:vdanhp"); delay(2000); lenh(0x01); while(1) { mode0=mode1;mode1=menu; if(mode0==0 && mode1==1) { mode=mode+1; if(mode==8) mode=0; } if(mode!=0) caidat(); else hienthi(); } }
Comment