Mình đang học 8051(Tự học theo cuốn 8051 cơ bản). viết chương trình quét led matrix cho 89s51:
#include <REGX51.H>
/*cot tu P0.0 den P0.7
Hang tu P2.0 den P2.7
De quet dua ra muc logic 0 lan luot ra cong 0*/
/*Ham tre*/
void delay(long time)
{
long n;
for(n=0;n<time;n++)
{
;
}
}
unsigned char kytu1[9];//Mang 9 phan tu chua gia tri cac hang day ra cong 1
unsigned char k=0;//Bien xac dinh cac ki tu
/* ham nap gia tri hien thi cac ki tu vao mang kytu1
co 8 dua ra va 1 gia tri khong bat den nao de cac ki tu cach nhau 1 cot*/
void mahoa(unsigned char x)
{
switch(x)
{
//dau trang
case 0:{kytu1[0]=0x00;kytu1[1]=0x00;kytu1[2]=0x00;kytu1[3]=0x00;kytu1[4]=0x00;kytu1[5]=0x00;kytu1[6]=0x00;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
// chu M
case 1:{kytu1[0]=0x00;kytu1[1]=0xFF;kytu1[2]=0x02;kytu1[3]=0x04;kytu1[4]=0x08;kytu1[5]=0x04;kytu1[6]=0x02;kytu1[7]=0xFF;kytu1[8]=0x00;break;}
//chu T
case 2:{kytu1[0]=0x00;kytu1[1]=0x01;kytu1[2]=0x01;kytu1[3]=0x01;kytu1[4]=0xFF;kytu1[5]=0x01;kytu1[6]=0x01;kytu1[7]=0x01;kytu1[8]=0x00;
break;}
// chu C
case 3:{kytu1[0]=0x00;kytu1[1]=0x7E;kytu1[2]=0x81;kytu1[3]=0x81;kytu1[4]=0x81;kytu1[5]=0x81;kytu1[6]=0x42;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
//dau trang
case 4:{kytu1[0]=0x00;kytu1[1]=0x00;kytu1[2]=0x00;kytu1[3]=0x00;kytu1[4]=0x00;kytu1[5]=0x00;kytu1[6]=0x00;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
}
}
/* ham quet led ma tran vua hien thi vua dich ki tu dan sang trai*/
void hienthi(void)
{
unsigned char n,m,lap;
unsigned char cot[8]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//cac phan tu quet cot
for(m=0;m<20;m++)//dich hien thi
{
for(lap=0;lap<12;lap++)//lap hien thi
{
for(n=0;n<8;n++)//quet cot
{
if((n+m)<9)//neu n+m<9 hien thi ky tu 1
{
mahoa(k);//nap cac gia tri ma hoa ki tu dua ra cac hang(cong 2)
P0=cot[n];//day cac gia tri cac hang (ma hoa ki tu) ra cong 2(cac hang)
delay(100);//tre du de led sang
}
if((n+m)>7)//neu n+m>7 hien thi ki tu 2
{
mahoa(k+1);//nap gia tri ma hoa ki tu tiep de dua ra cac hang(cong 2)
P0=cot[n];//day gia tri lo gic 0V ra cong 0 (cac cot)
P2=kytu1[n+m-8];//day cac gia tri cac hang (ma hoa ki tu) ra cong 2 (cac hang)
delay(45);//tre du de led sang
}
P0=0xFF;//day cac cot len cao
P2=0x00;//dua cac hang xuong thap de tat toan bo led
}
}
}
}
void main(void)
{
while(1)//vong lap vo han
{
hienthi();//hien thi 2 ki tu dau trang va chu M
k=k+1;//tang k de hien thi chu M va chu T tiep theo
if(k==4)k=0;//quay vong hien thi
}
}
Khi mô phỏng bằng proteus7 thấy ngoài các chữ M, T, C hiển thị còn có một số đèn Led sáng không theo lập trình. vậy có phải do nhiễu ko? sửa nhiễu đó bằng cách nào? giúp mình với!
#include <REGX51.H>
/*cot tu P0.0 den P0.7
Hang tu P2.0 den P2.7
De quet dua ra muc logic 0 lan luot ra cong 0*/
/*Ham tre*/
void delay(long time)
{
long n;
for(n=0;n<time;n++)
{
;
}
}
unsigned char kytu1[9];//Mang 9 phan tu chua gia tri cac hang day ra cong 1
unsigned char k=0;//Bien xac dinh cac ki tu
/* ham nap gia tri hien thi cac ki tu vao mang kytu1
co 8 dua ra va 1 gia tri khong bat den nao de cac ki tu cach nhau 1 cot*/
void mahoa(unsigned char x)
{
switch(x)
{
//dau trang
case 0:{kytu1[0]=0x00;kytu1[1]=0x00;kytu1[2]=0x00;kytu1[3]=0x00;kytu1[4]=0x00;kytu1[5]=0x00;kytu1[6]=0x00;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
// chu M
case 1:{kytu1[0]=0x00;kytu1[1]=0xFF;kytu1[2]=0x02;kytu1[3]=0x04;kytu1[4]=0x08;kytu1[5]=0x04;kytu1[6]=0x02;kytu1[7]=0xFF;kytu1[8]=0x00;break;}
//chu T
case 2:{kytu1[0]=0x00;kytu1[1]=0x01;kytu1[2]=0x01;kytu1[3]=0x01;kytu1[4]=0xFF;kytu1[5]=0x01;kytu1[6]=0x01;kytu1[7]=0x01;kytu1[8]=0x00;
break;}
// chu C
case 3:{kytu1[0]=0x00;kytu1[1]=0x7E;kytu1[2]=0x81;kytu1[3]=0x81;kytu1[4]=0x81;kytu1[5]=0x81;kytu1[6]=0x42;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
//dau trang
case 4:{kytu1[0]=0x00;kytu1[1]=0x00;kytu1[2]=0x00;kytu1[3]=0x00;kytu1[4]=0x00;kytu1[5]=0x00;kytu1[6]=0x00;kytu1[7]=0x00;kytu1[8]=0x00;
break;}
}
}
/* ham quet led ma tran vua hien thi vua dich ki tu dan sang trai*/
void hienthi(void)
{
unsigned char n,m,lap;
unsigned char cot[8]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//cac phan tu quet cot
for(m=0;m<20;m++)//dich hien thi
{
for(lap=0;lap<12;lap++)//lap hien thi
{
for(n=0;n<8;n++)//quet cot
{
if((n+m)<9)//neu n+m<9 hien thi ky tu 1
{
mahoa(k);//nap cac gia tri ma hoa ki tu dua ra cac hang(cong 2)
P0=cot[n];//day cac gia tri cac hang (ma hoa ki tu) ra cong 2(cac hang)
delay(100);//tre du de led sang
}
if((n+m)>7)//neu n+m>7 hien thi ki tu 2
{
mahoa(k+1);//nap gia tri ma hoa ki tu tiep de dua ra cac hang(cong 2)
P0=cot[n];//day gia tri lo gic 0V ra cong 0 (cac cot)
P2=kytu1[n+m-8];//day cac gia tri cac hang (ma hoa ki tu) ra cong 2 (cac hang)
delay(45);//tre du de led sang
}
P0=0xFF;//day cac cot len cao
P2=0x00;//dua cac hang xuong thap de tat toan bo led
}
}
}
}
void main(void)
{
while(1)//vong lap vo han
{
hienthi();//hien thi 2 ki tu dau trang va chu M
k=k+1;//tang k de hien thi chu M va chu T tiep theo
if(k==4)k=0;//quay vong hien thi
}
}
Khi mô phỏng bằng proteus7 thấy ngoài các chữ M, T, C hiển thị còn có một số đèn Led sáng không theo lập trình. vậy có phải do nhiễu ko? sửa nhiễu đó bằng cách nào? giúp mình với!
Comment