#include<stdio.h>
#include<reg52.h>
#include<i2c.h>
#include<ds1307.h>
sbit led1=P0^0;
sbit led2=P0^1;
sbit led3=P0^2;
sbit led4=P0^3;
sbit led5=P0^4;
sbit led6=P0^5;
sbit set=P3^4;
sbit up=P3^5;
sbit down=P3^6;
sbit f=P3^7;
unsigned char hour, minute, second,thu=2,ngay=1,thang=1,nam=0;
unsigned char ma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char kt=0,a=0,b=0,c=0,d=0,i,k,x=0;
unsigned int y=0;
bit set0=1,set1=1,up1=1,up0=1,down0=1,down1=1;
void delays(int ms)
{
int j;
for(j=0;j<ms;j++){}
}
/*void dem() interrupt 3
{
TR1=0;
TH1=-5000/256;
TL1=-5000%256;
c++;
if(c==100)
{
f=~f;
}
TF1=0;
TR1=1;
}
*/
void writeds(){
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
rtc_write(0x03, thu);
rtc_write(0x04, ngay);
rtc_write(0x05, thang);
rtc_write(0x06, nam);
}
void readds()
{
if(kt==0)
{
if(a==1)
{writeds();a=0;
}
second = rtc_read(0x00);
minute = rtc_read(0x01);
hour = rtc_read(0x02);
thu = rtc_read(0x03);
ngay = rtc_read(0x04);
thang = rtc_read(0x05);
nam = rtc_read(0x06);
}
}
void dislay()
{
if(y>=20&&y<260)
{
P2=ma[hour/10];led1=1;delays(90);led1=0;P2=0xff;
P2=ma[hour%10];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[minute/10];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[minute%10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[second/10];led5=1;delays(90);led5=0;P2=0xff;
P2=ma[second%10];led6=1;delays(90);led6=0;P2=0xff;
}
if(y>=260&&y<280)
{
P2=0xce;led1=1;delays(90);led1=0;P2=0xff;
P2=ma[thu];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[ngay/10];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[ngay%10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[thang/10];led5=1;delays(90);led5=0;P2=0xff;
P2=ma[thang%10];led6=1;delays(90);led6=0;P2=0xff;
}
if(y>=280&&y<300)
{
P2=ma[2];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[0];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[nam/10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[nam%10];led5=1;delays(90);led5=0;P2=0xff;
}
if(y<20)
{
led6=0;
P2=0x89;led1=1;delays(90);led1=0;P2=0xff;
P2=0x86;led2=1;delays(90);led2=0;P2=0xff;
P2=0xc7;led3=1;delays(90);led3=0;P2=0xff;
P2=0xc7;led4=1;delays(90);led4=0;P2=0xff;
P2=0xc0;led5=1;delays(90);led5=0;P2=0xff;
}
}
void ktphim()
{
set0=set1;set1=set;
if((set0==1)&&(set1==0))
{
kt++;
if(kt>7)kt=0;
}
switch(kt)
{
case 0:break;
case 1:
{
up0=up1;up1=up; a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
hour++;
if(hour>23)hour=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
hour--;
if(hour==-1)
hour=23;
}
break;
}
case 2:
{
up0=up1;up1=up; a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
minute++;
if(minute>59)
minute=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
minute--;
if(minute==-1)
minute=59;
}
break;
}
case 3:
{
up0=up1;up1=up;a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
second++;
if(second>59)
second=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
second=0;
}
break;
}
case 4:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
thu++;
if(thu>7)thu=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
thu--;
if(thu==0)
thu=7;
}
break;
}
case 5:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
ngay++;
if(ngay>31)ngay=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
ngay--;
if(ngay==0)
ngay=31;
}
break;
}
case 6:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
thang++;
if(thang>12)thang=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
thang--;
if(thang==0)
thang=12;
}
break;
}
case 7:
{
up0=up1;up1=up; a=1;b=3;d=1;
if((up0==1)&&(up1==0))
{
nam++;
if(nam>99)nam=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
nam--;
if(nam==0)
nam=99;
}
break;
}
}
}
void chinhgio(void)
{
if(b==1)
{
if((kt==0)||(kt!=1)||((kt==1)&&(x<40)))
{
P2=ma[hour/10];led1=1;delays(100);led1=0;P2=0xff;
P2=ma[hour%10];led2=1;delays(100);led2=0;P2=0xff;
}
if((kt==0)||(kt!=2)||((kt==2)&&(x<40)))
{
P2=ma[minute/10];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[minute%10];led4=1;delays(100);led4=0;P2=0xff;
}
if((kt==0)||(kt!=3)||((kt==3)&&(x<40)))
{
P2=ma[second/10];led5=1;delays(100);led5=0;P2=0xff;
P2=ma[second%10];led6=1;delays(100);led6=0;P2=0xff;
}
}
if(b==2)
{
if((kt==0)||(kt!=4)||((kt==4)&&(x<40)))
{
P2=0xce;led1=1;delays(100);led1=0;P2=0xff;
P2=ma[thu];led2=1;delays(100);led2=0;P2=0xff;
}
if((kt==0)||(kt!=5)||((kt==5)&&(x<40)))
{
P2=ma[ngay/10];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[ngay%10];led4=1;delays(100);led4=0;P2=0xff;
}
if((kt==0)||(kt!=6)||((kt==6)&&(x<40)))
{
P2=ma[thang/10];led5=1;delays(100);led5=0;P2=0xff;
P2=ma[thang%10];led6=1;delays(100);led6=0;P2=0xff;
}
}
if(b==3)
{
if((kt==0)||(kt!=7)||((kt==7)&&(x<40)))
{
P2=ma[2];led2=1;delays(100);led2=0;P2=0xff;
P2=ma[0];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[nam/10];led4=1;delays(100);led4=0;P2=0xff;
P2=ma[nam%10];led5=1;delays(100);led5=0;P2=0xff;
}
}
}
void bugio(void)
{
if((hour==23 )&& (minute==59)&&(second==56))
{
hour=0;
minute=0;
second=0;
writeds();
}
}
void main()
{
/* TMOD=0x10;
TH1=-10000/256;
TL1=-10000%256;
ET1=1;
ET0=1;
EA=1;
IE=0X88;
IP=0;
TF1=0;
TR1=1; */
while(1)
{
y++;
x++;
ktphim();
readds();
if(d==0)
{
dislay();
}
if(d==1)
{
chinhgio();
d=0;
}
bugio();
if(x==80)
{x=0;}
if(y==300)
{y=0;}
}
}
Các bạn ktra giúp mình cái code này với nhé xem có đúng là nó có vấn đề ko nhé,
#include<reg52.h>
#include<i2c.h>
#include<ds1307.h>
sbit led1=P0^0;
sbit led2=P0^1;
sbit led3=P0^2;
sbit led4=P0^3;
sbit led5=P0^4;
sbit led6=P0^5;
sbit set=P3^4;
sbit up=P3^5;
sbit down=P3^6;
sbit f=P3^7;
unsigned char hour, minute, second,thu=2,ngay=1,thang=1,nam=0;
unsigned char ma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char kt=0,a=0,b=0,c=0,d=0,i,k,x=0;
unsigned int y=0;
bit set0=1,set1=1,up1=1,up0=1,down0=1,down1=1;
void delays(int ms)
{
int j;
for(j=0;j<ms;j++){}
}
/*void dem() interrupt 3
{
TR1=0;
TH1=-5000/256;
TL1=-5000%256;
c++;
if(c==100)
{
f=~f;
}
TF1=0;
TR1=1;
}
*/
void writeds(){
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
rtc_write(0x03, thu);
rtc_write(0x04, ngay);
rtc_write(0x05, thang);
rtc_write(0x06, nam);
}
void readds()
{
if(kt==0)
{
if(a==1)
{writeds();a=0;
}
second = rtc_read(0x00);
minute = rtc_read(0x01);
hour = rtc_read(0x02);
thu = rtc_read(0x03);
ngay = rtc_read(0x04);
thang = rtc_read(0x05);
nam = rtc_read(0x06);
}
}
void dislay()
{
if(y>=20&&y<260)
{
P2=ma[hour/10];led1=1;delays(90);led1=0;P2=0xff;
P2=ma[hour%10];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[minute/10];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[minute%10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[second/10];led5=1;delays(90);led5=0;P2=0xff;
P2=ma[second%10];led6=1;delays(90);led6=0;P2=0xff;
}
if(y>=260&&y<280)
{
P2=0xce;led1=1;delays(90);led1=0;P2=0xff;
P2=ma[thu];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[ngay/10];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[ngay%10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[thang/10];led5=1;delays(90);led5=0;P2=0xff;
P2=ma[thang%10];led6=1;delays(90);led6=0;P2=0xff;
}
if(y>=280&&y<300)
{
P2=ma[2];led2=1;delays(90);led2=0;P2=0xff;
P2=ma[0];led3=1;delays(90);led3=0;P2=0xff;
P2=ma[nam/10];led4=1;delays(90);led4=0;P2=0xff;
P2=ma[nam%10];led5=1;delays(90);led5=0;P2=0xff;
}
if(y<20)
{
led6=0;
P2=0x89;led1=1;delays(90);led1=0;P2=0xff;
P2=0x86;led2=1;delays(90);led2=0;P2=0xff;
P2=0xc7;led3=1;delays(90);led3=0;P2=0xff;
P2=0xc7;led4=1;delays(90);led4=0;P2=0xff;
P2=0xc0;led5=1;delays(90);led5=0;P2=0xff;
}
}
void ktphim()
{
set0=set1;set1=set;
if((set0==1)&&(set1==0))
{
kt++;
if(kt>7)kt=0;
}
switch(kt)
{
case 0:break;
case 1:
{
up0=up1;up1=up; a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
hour++;
if(hour>23)hour=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
hour--;
if(hour==-1)
hour=23;
}
break;
}
case 2:
{
up0=up1;up1=up; a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
minute++;
if(minute>59)
minute=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
minute--;
if(minute==-1)
minute=59;
}
break;
}
case 3:
{
up0=up1;up1=up;a=1;b=1;d=1;
if((up0==1)&&(up1==0))
{
second++;
if(second>59)
second=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
second=0;
}
break;
}
case 4:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
thu++;
if(thu>7)thu=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
thu--;
if(thu==0)
thu=7;
}
break;
}
case 5:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
ngay++;
if(ngay>31)ngay=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
ngay--;
if(ngay==0)
ngay=31;
}
break;
}
case 6:
{
up0=up1;up1=up; a=1;b=2;d=1;
if((up0==1)&&(up1==0))
{
thang++;
if(thang>12)thang=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
thang--;
if(thang==0)
thang=12;
}
break;
}
case 7:
{
up0=up1;up1=up; a=1;b=3;d=1;
if((up0==1)&&(up1==0))
{
nam++;
if(nam>99)nam=0;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
nam--;
if(nam==0)
nam=99;
}
break;
}
}
}
void chinhgio(void)
{
if(b==1)
{
if((kt==0)||(kt!=1)||((kt==1)&&(x<40)))
{
P2=ma[hour/10];led1=1;delays(100);led1=0;P2=0xff;
P2=ma[hour%10];led2=1;delays(100);led2=0;P2=0xff;
}
if((kt==0)||(kt!=2)||((kt==2)&&(x<40)))
{
P2=ma[minute/10];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[minute%10];led4=1;delays(100);led4=0;P2=0xff;
}
if((kt==0)||(kt!=3)||((kt==3)&&(x<40)))
{
P2=ma[second/10];led5=1;delays(100);led5=0;P2=0xff;
P2=ma[second%10];led6=1;delays(100);led6=0;P2=0xff;
}
}
if(b==2)
{
if((kt==0)||(kt!=4)||((kt==4)&&(x<40)))
{
P2=0xce;led1=1;delays(100);led1=0;P2=0xff;
P2=ma[thu];led2=1;delays(100);led2=0;P2=0xff;
}
if((kt==0)||(kt!=5)||((kt==5)&&(x<40)))
{
P2=ma[ngay/10];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[ngay%10];led4=1;delays(100);led4=0;P2=0xff;
}
if((kt==0)||(kt!=6)||((kt==6)&&(x<40)))
{
P2=ma[thang/10];led5=1;delays(100);led5=0;P2=0xff;
P2=ma[thang%10];led6=1;delays(100);led6=0;P2=0xff;
}
}
if(b==3)
{
if((kt==0)||(kt!=7)||((kt==7)&&(x<40)))
{
P2=ma[2];led2=1;delays(100);led2=0;P2=0xff;
P2=ma[0];led3=1;delays(100);led3=0;P2=0xff;
P2=ma[nam/10];led4=1;delays(100);led4=0;P2=0xff;
P2=ma[nam%10];led5=1;delays(100);led5=0;P2=0xff;
}
}
}
void bugio(void)
{
if((hour==23 )&& (minute==59)&&(second==56))
{
hour=0;
minute=0;
second=0;
writeds();
}
}
void main()
{
/* TMOD=0x10;
TH1=-10000/256;
TL1=-10000%256;
ET1=1;
ET0=1;
EA=1;
IE=0X88;
IP=0;
TF1=0;
TR1=1; */
while(1)
{
y++;
x++;
ktphim();
readds();
if(d==0)
{
dislay();
}
if(d==1)
{
chinhgio();
d=0;
}
bugio();
if(x==80)
{x=0;}
if(y==300)
{y=0;}
}
}
Các bạn ktra giúp mình cái code này với nhé xem có đúng là nó có vấn đề ko nhé,
Comment