sao luc dau led7 quet dung ma sao luc sau quet sai
#include<AT89X52.h>
#define dataled7 P0
#define d1 P1_0
#define d2 P1_1
#define xc P3_0
#define vc P3_1
#define dc P3_2
#define xp P3_3
#define vp P3_4
#define dp P3_5
#define xt P2_0
#define vt P2_1
#define dt P2_2
#define ct1 P2_3
#define ct2 P2_4
#define ct3 P2_5
unsigned int over=0;
unsigned char xanh1,vang1,do1,vang2,xanh2,do2,xanh3,vang3,do3;
unsigned char ht,mode,led1,led2,led3,led4,led5,led6,led7,led8,le d9,led10,led11,led12,led13,led14,led15,led16,led17 ,led18;
unsigned char code maled7[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 }; //
//chuong trinh con tao thoi gian tre cho quet led
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<13;y++);
}
}
//chuong trinh phuc vu ngat timer0
void time0() interrupt 1
{ //cu 200 us thi tran 1 lan
over++;
if(over==4950) //gia tri tinh toan la 5000, tinh chinh lai 4950
{over=0;
switch(mode)
{ case 1: {xanh1=xanh1-1;do2=do2-1;do3=do3-1;}break;
case 2: {vang1=vang1-1;do2=do2-1;do3=do3-1;}break;
case 3: {do1=do1-1;xanh2=xanh2-1;do3=do3-1;}break;
case 4: {do1=do1-1;vang2=vang2-1;do3=do3-1;}break;
case 5:{do1=do1-1;do2=do2-1;xanh3=xanh3-1;}break;
case 6:{do1=do1-1;do2=do2-1;vang3=vang3-1;}break;
}
}
}
void hex_bcd(void)
{
unsigned char x,y,z,t,u,v,k,h,g;
x=xanh1;
led1=x/10;
led2=x%10;
y=vang1;
led3=y/10;
led4=y%10;
z=do1;
led5=z/10;
led6=z%10;
t=xanh2;
led7=t/10;
led8=t%10;
u=vang2;
led9=u/10;
led10=u%10;
v=do2;
led11=v/10;
led12=v%10;
k=xanh3;
led13=k/10;
led14=k%10;
h=vang3;
led15=h/10;
led16=h%10;
g=do3;
led17=g/10;
led18=g%10;
}
void kiem_tra(void)
{
if(xanh1==0X00)
{ ht=2;mode=2;}
if(vang1==0XFF)
{ ht=3;mode=3;}
if(xanh2==0X00)
{ ht=4;mode=4;}
if(vang2==0XFF)
{ ht=5;mode=5;}
if(xanh3==0x00)
{ht=6;mode=6;}
if(vang3==0xff)
{ht=7;mode=7;}
}
void hienthi(void)
{
switch(ht)
{ case 1: {dataled7=maled7[led1],d1=1,delay(5),d1=0,
dataled7=maled7[led2],d2=1,delay(5),d2=0,
xc=0,vc=1,dc=1,xp=1,vp=1,dp=0,xt=1,vt=1,dt=0;}brea k;
case 2: {dataled7=maled7[led3],d1=1,delay(5),d1=0,
dataled7=maled7[led4],d2=1,delay(5),d2=0,
xc=1,vc=0,dc=1,xp=1,vp=1,dp=0,xt=1,vt=1,dt=0;}brea k;
case 3: {dataled7=maled7[led7],d1=1,delay(5),d1=0,
dataled7=maled7[led8],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=0,vp=1,dp=1,xt=1,vt=1,dt=0;}brea k;
case 4: {dataled7=maled7[led9],d1=1,delay(5),d1=0,
dataled7=maled7[led10],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=0,dp=1,xt=1,vt=1,dt=0;}brea k;
case 5: {dataled7=maled7[led13],d1=1,delay(5),d1=0,
dataled7=maled7[led14],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=1,dp=0,xt=0,vt=1,dt=1;}brea k;
case 6: {dataled7=maled7[led15],d1=1,delay(5),d1=0,
dataled7=maled7[led16],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=1,dp=0,xt=1,vt=0,dt=1;}brea k;
}
}
//
void cong_tac()
{ if(P1_5==0)
{ht=1;mode=1;}
else if(P1_6==0)
{ht=3;mode=3;}
else if(P1_7==0)
{ht=5;mode=5;}
}
//chuong trinh chinh
void main()
{ ht=1,mode=1, xanh1=57,vang1=3,do1=127,xanh2=62,vang2=3,do2=122, xanh3=59,vang3=3;do3=125;
xc=0,vc=0,dc=0,xp=0,vp=0,dp=0,xt=0,vt=0,dt=0;
P0=255;P1=255;
P2=255; cong_tac();
TMOD=2; //timer0 o che do 2 tu nap lai
TH0=54; //gia tri nap lai
TR0=1; //cho phep timer0 chay
ET0=1; //cho phep ngat timer0
EA=1; //cho phep ngat toan cuc
while(1)
{
hex_bcd();
kiem_tra();
if(mode==7)
{ht=1,mode=1, xanh1=57,vang1=3,do1=127,xanh2=62,vang2=3,do2=122, xanh3=59,vang3=3;do3=125;}
hienthi();
}
}
#include<AT89X52.h>
#define dataled7 P0
#define d1 P1_0
#define d2 P1_1
#define xc P3_0
#define vc P3_1
#define dc P3_2
#define xp P3_3
#define vp P3_4
#define dp P3_5
#define xt P2_0
#define vt P2_1
#define dt P2_2
#define ct1 P2_3
#define ct2 P2_4
#define ct3 P2_5
unsigned int over=0;
unsigned char xanh1,vang1,do1,vang2,xanh2,do2,xanh3,vang3,do3;
unsigned char ht,mode,led1,led2,led3,led4,led5,led6,led7,led8,le d9,led10,led11,led12,led13,led14,led15,led16,led17 ,led18;
unsigned char code maled7[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 }; //
//chuong trinh con tao thoi gian tre cho quet led
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<13;y++);
}
}
//chuong trinh phuc vu ngat timer0
void time0() interrupt 1
{ //cu 200 us thi tran 1 lan
over++;
if(over==4950) //gia tri tinh toan la 5000, tinh chinh lai 4950
{over=0;
switch(mode)
{ case 1: {xanh1=xanh1-1;do2=do2-1;do3=do3-1;}break;
case 2: {vang1=vang1-1;do2=do2-1;do3=do3-1;}break;
case 3: {do1=do1-1;xanh2=xanh2-1;do3=do3-1;}break;
case 4: {do1=do1-1;vang2=vang2-1;do3=do3-1;}break;
case 5:{do1=do1-1;do2=do2-1;xanh3=xanh3-1;}break;
case 6:{do1=do1-1;do2=do2-1;vang3=vang3-1;}break;
}
}
}
void hex_bcd(void)
{
unsigned char x,y,z,t,u,v,k,h,g;
x=xanh1;
led1=x/10;
led2=x%10;
y=vang1;
led3=y/10;
led4=y%10;
z=do1;
led5=z/10;
led6=z%10;
t=xanh2;
led7=t/10;
led8=t%10;
u=vang2;
led9=u/10;
led10=u%10;
v=do2;
led11=v/10;
led12=v%10;
k=xanh3;
led13=k/10;
led14=k%10;
h=vang3;
led15=h/10;
led16=h%10;
g=do3;
led17=g/10;
led18=g%10;
}
void kiem_tra(void)
{
if(xanh1==0X00)
{ ht=2;mode=2;}
if(vang1==0XFF)
{ ht=3;mode=3;}
if(xanh2==0X00)
{ ht=4;mode=4;}
if(vang2==0XFF)
{ ht=5;mode=5;}
if(xanh3==0x00)
{ht=6;mode=6;}
if(vang3==0xff)
{ht=7;mode=7;}
}
void hienthi(void)
{
switch(ht)
{ case 1: {dataled7=maled7[led1],d1=1,delay(5),d1=0,
dataled7=maled7[led2],d2=1,delay(5),d2=0,
xc=0,vc=1,dc=1,xp=1,vp=1,dp=0,xt=1,vt=1,dt=0;}brea k;
case 2: {dataled7=maled7[led3],d1=1,delay(5),d1=0,
dataled7=maled7[led4],d2=1,delay(5),d2=0,
xc=1,vc=0,dc=1,xp=1,vp=1,dp=0,xt=1,vt=1,dt=0;}brea k;
case 3: {dataled7=maled7[led7],d1=1,delay(5),d1=0,
dataled7=maled7[led8],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=0,vp=1,dp=1,xt=1,vt=1,dt=0;}brea k;
case 4: {dataled7=maled7[led9],d1=1,delay(5),d1=0,
dataled7=maled7[led10],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=0,dp=1,xt=1,vt=1,dt=0;}brea k;
case 5: {dataled7=maled7[led13],d1=1,delay(5),d1=0,
dataled7=maled7[led14],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=1,dp=0,xt=0,vt=1,dt=1;}brea k;
case 6: {dataled7=maled7[led15],d1=1,delay(5),d1=0,
dataled7=maled7[led16],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,xp=1,vp=1,dp=0,xt=1,vt=0,dt=1;}brea k;
}
}
//
void cong_tac()
{ if(P1_5==0)
{ht=1;mode=1;}
else if(P1_6==0)
{ht=3;mode=3;}
else if(P1_7==0)
{ht=5;mode=5;}
}
//chuong trinh chinh
void main()
{ ht=1,mode=1, xanh1=57,vang1=3,do1=127,xanh2=62,vang2=3,do2=122, xanh3=59,vang3=3;do3=125;
xc=0,vc=0,dc=0,xp=0,vp=0,dp=0,xt=0,vt=0,dt=0;
P0=255;P1=255;
P2=255; cong_tac();
TMOD=2; //timer0 o che do 2 tu nap lai
TH0=54; //gia tri nap lai
TR0=1; //cho phep timer0 chay
ET0=1; //cho phep ngat timer0
EA=1; //cho phep ngat toan cuc
while(1)
{
hex_bcd();
kiem_tra();
if(mode==7)
{ht=1,mode=1, xanh1=57,vang1=3,do1=127,xanh2=62,vang2=3,do2=122, xanh3=59,vang3=3;do3=125;}
hienthi();
}
}