chào các kaka.em là thanh viên mới toanh.hi.em có vấn đề cần các kaka giúp đỡ:bọn em mới học vdk 8051 và phải làm hệ thống đèn gt.em viết đoạn code nay cho chạy mô phỏng thì ban đầu con led 7 quét đúng nhưng sau khoảng thời gian thì lại quét sai.mong các kaka sửa jup em lỗi này gấp.
#include<AT89X51.h>
#define dataled7 P2
#define d1 P3_0
#define d2 P3_1
#define xc P1_0
#define vc P1_1
#define dc P1_2
#define dp P1_3
#define ct1 P3_4
#define ct2 P3_5
#define ct3 P3_6
unsigned int over=0;
unsigned char xanh1,vang1,do1,do2;
unsigned char nhay,dem,led1,led2,led3,led4,led5,led6,led7,led8;
unsigned char maled7[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 }; //
//
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<13;y++);
}
}
//
void time0() interrupt 1
{
over++;
if(over==5000)
{over=0;
switch(nhay)
{ case 1: {xanh1=xanh1-1;}break;
case 2: {vang1=vang1-1;}break;
case 3: {do1=do1-1;}break;
case 4: {do2=do2-1;}break;
}
}
}
//
void kiem_tra(void)
{
if(xanh1==0X00)
{ nhay=2;dem=2;}
if(vang1==0Xff)
{ nhay=3;dem=3;}
if(do1==0X00)
{ nhay=4;dem=4;}
if(do2==0Xff)
{ nhay=5;dem=5;}
}
//
void cong_tac(void)
{if(P3_4==0)
{nhay=1;dem=1;}
if(P3_5==0)
{nhay=3;dem=3;}
else if(P3_6==0)
{nhay=4;dem=4;}
else if((P3_4!=0)&&(P3_5!=0))
{nhay=1;dem=1;}
}
void hex_bcd(void)
{
unsigned char x,y,z,t;
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=do2;
led7=t/10;
led8=t%10;
}
//
void hien_thi(void)
{
switch(dem)
{ 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,dp=1;}break;
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,dp=1;}break;
case 3: {dataled7=maled7[led5],d1=1,delay(5),d1=0,
dataled7=maled7[led6],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,dp=1;}break;
case 4: {dataled7=maled7[led7],d1=1,delay(5),d1=0,
dataled7=maled7[led8],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,dp=1;}break;
}
}
//
//
void main()
{ nhay=1,dem=1, xanh1=57,vang1=3,do1=65,do2=62;
xc=0,vc=0,dc=0,dp=0;
P3_0=P3_1=255;
P2=255;
TMOD=2;
TH0=45;TL0=5;
TR0=1;
ET0=1;
EA=1;
while(1)
{ hex_bcd();
kiem_tra();
if(nhay==5)
{nhay=1,dem=1, xanh1=57,vang1=3,do1=65,do2=62;}
hien_thi();
cong_tac();
}
}
#include<AT89X51.h>
#define dataled7 P2
#define d1 P3_0
#define d2 P3_1
#define xc P1_0
#define vc P1_1
#define dc P1_2
#define dp P1_3
#define ct1 P3_4
#define ct2 P3_5
#define ct3 P3_6
unsigned int over=0;
unsigned char xanh1,vang1,do1,do2;
unsigned char nhay,dem,led1,led2,led3,led4,led5,led6,led7,led8;
unsigned char maled7[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 }; //
//
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<13;y++);
}
}
//
void time0() interrupt 1
{
over++;
if(over==5000)
{over=0;
switch(nhay)
{ case 1: {xanh1=xanh1-1;}break;
case 2: {vang1=vang1-1;}break;
case 3: {do1=do1-1;}break;
case 4: {do2=do2-1;}break;
}
}
}
//
void kiem_tra(void)
{
if(xanh1==0X00)
{ nhay=2;dem=2;}
if(vang1==0Xff)
{ nhay=3;dem=3;}
if(do1==0X00)
{ nhay=4;dem=4;}
if(do2==0Xff)
{ nhay=5;dem=5;}
}
//
void cong_tac(void)
{if(P3_4==0)
{nhay=1;dem=1;}
if(P3_5==0)
{nhay=3;dem=3;}
else if(P3_6==0)
{nhay=4;dem=4;}
else if((P3_4!=0)&&(P3_5!=0))
{nhay=1;dem=1;}
}
void hex_bcd(void)
{
unsigned char x,y,z,t;
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=do2;
led7=t/10;
led8=t%10;
}
//
void hien_thi(void)
{
switch(dem)
{ 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,dp=1;}break;
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,dp=1;}break;
case 3: {dataled7=maled7[led5],d1=1,delay(5),d1=0,
dataled7=maled7[led6],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,dp=1;}break;
case 4: {dataled7=maled7[led7],d1=1,delay(5),d1=0,
dataled7=maled7[led8],d2=1,delay(5),d2=0,
xc=1,vc=1,dc=0,dp=1;}break;
}
}
//
//
void main()
{ nhay=1,dem=1, xanh1=57,vang1=3,do1=65,do2=62;
xc=0,vc=0,dc=0,dp=0;
P3_0=P3_1=255;
P2=255;
TMOD=2;
TH0=45;TL0=5;
TR0=1;
ET0=1;
EA=1;
while(1)
{ hex_bcd();
kiem_tra();
if(nhay==5)
{nhay=1,dem=1, xanh1=57,vang1=3,do1=65,do2=62;}
hien_thi();
cong_tac();
}
}