em có bài toán về ma trận phím viết bằng 89c51, đã viết rồi nhưng không sửa được mong các coa thủ trên 4rum giúp em với!
//cho ma trận phím 4x4. khi ấn phím nào thì bật sáng phím đó trên led 7thanh.
//đây là code em viết
#include <REGX51.H>
void tre(unsigned int t)
{
unsigned int i,j;
for (i=0;i<t;i++)
for(j=0;j<201;j++);
}
void quetphim()
{
P2=0xff; //set cac hang len muc cao
if (P2_7==0)
hang1();
if (P2_6==0)
hang2();
if (P2_5==0)
hang3();
if (P2_4==0)
hang4();
}
void khongbam()//cac led 7 thanh hien so 0
{
P2=0x0f;
P1=0x40;
P3=0x40;
}
void hang1()
{
P2=0xef;
if (P2_3==0)
nut1();
if(P2_2==0)
nut2();
if(P2_1==0)
nut3();
if(P2_0=0)
nut4();
}
void hang2()
{
P2=oxbf;
if(P2_3==0)
nut5();
if(P2_2==0)
nut6();
if(P2_1==0)
nut7();
if(P2_0==0)
nut8();
}
void hang3()
{
P2=0xdf;
if(P2_3==0)
nut9();
if(P2_2==0)
nut10();
if(P2_1==0)
nut11();
if(P2_0==0)
nut12();
}
void hang4()
{
P2=0x1f;
if(P2_3==0)
nut13();
if(P2_2==0)
nut14();
if(P2_1==0)
nut15();
if(P2_0==0)
nut16();
}
void nut1()
{
chongrung();
P1=0x79; //bat so 1 tren led 7 thanh noi voi port1
P3=0x40; //bat so 0 tren led 7 seg noi voi port3
}
void nut2()
{
chongrung();
P1=0x24;
P3=0x40;
}
void nut3()
{
chongrung();
P1=0x30;
P3=0x40;
}
void nut4()
{
chongrung();
P1=0x19;
P3=0x40;
}
void nut5()
{
chongrung();
P1=0x12;
P3=0x40;
}
void nut6()
{
chongrung();
P1=0x03
P3=0x40;
}
void nut7()
{
chongrung();
P1=0x78;
P3=0x40;
}
void nut8()
{
chongrung();
P1=0x00;
P3=0x40;
}
void nut9()
{
chongrung();
P1=0x10;
P3=0x40;
}
void nut10()
{
chongrung();
P3=0x79;
P1=0x40;
}
void nut11()
{
chongrung();
P3=0x79;
P1=0x79;
}
void nut12()
{
chongrung();
P3=0x79;
P1=0x24;
}
void nut13()
{
chongrung();
P3=0x79;
P1=0x30;
}
void nut14()
{
chongrung();
P3=0x79;
P1=0x19;
}
void nut15()
{
chongrung();
P3=0x79;
P1=0x12;
}
void nut16()
{
chongrung();
P3=0x79;
P1=0x03;
}
void chongrung()
tre(50);
void main()
{
while (1)
{
quetphim();
}
}
//cho ma trận phím 4x4. khi ấn phím nào thì bật sáng phím đó trên led 7thanh.
//đây là code em viết
#include <REGX51.H>
void tre(unsigned int t)
{
unsigned int i,j;
for (i=0;i<t;i++)
for(j=0;j<201;j++);
}
void quetphim()
{
P2=0xff; //set cac hang len muc cao
if (P2_7==0)
hang1();
if (P2_6==0)
hang2();
if (P2_5==0)
hang3();
if (P2_4==0)
hang4();
}
void khongbam()//cac led 7 thanh hien so 0
{
P2=0x0f;
P1=0x40;
P3=0x40;
}
void hang1()
{
P2=0xef;
if (P2_3==0)
nut1();
if(P2_2==0)
nut2();
if(P2_1==0)
nut3();
if(P2_0=0)
nut4();
}
void hang2()
{
P2=oxbf;
if(P2_3==0)
nut5();
if(P2_2==0)
nut6();
if(P2_1==0)
nut7();
if(P2_0==0)
nut8();
}
void hang3()
{
P2=0xdf;
if(P2_3==0)
nut9();
if(P2_2==0)
nut10();
if(P2_1==0)
nut11();
if(P2_0==0)
nut12();
}
void hang4()
{
P2=0x1f;
if(P2_3==0)
nut13();
if(P2_2==0)
nut14();
if(P2_1==0)
nut15();
if(P2_0==0)
nut16();
}
void nut1()
{
chongrung();
P1=0x79; //bat so 1 tren led 7 thanh noi voi port1
P3=0x40; //bat so 0 tren led 7 seg noi voi port3
}
void nut2()
{
chongrung();
P1=0x24;
P3=0x40;
}
void nut3()
{
chongrung();
P1=0x30;
P3=0x40;
}
void nut4()
{
chongrung();
P1=0x19;
P3=0x40;
}
void nut5()
{
chongrung();
P1=0x12;
P3=0x40;
}
void nut6()
{
chongrung();
P1=0x03
P3=0x40;
}
void nut7()
{
chongrung();
P1=0x78;
P3=0x40;
}
void nut8()
{
chongrung();
P1=0x00;
P3=0x40;
}
void nut9()
{
chongrung();
P1=0x10;
P3=0x40;
}
void nut10()
{
chongrung();
P3=0x79;
P1=0x40;
}
void nut11()
{
chongrung();
P3=0x79;
P1=0x79;
}
void nut12()
{
chongrung();
P3=0x79;
P1=0x24;
}
void nut13()
{
chongrung();
P3=0x79;
P1=0x30;
}
void nut14()
{
chongrung();
P3=0x79;
P1=0x19;
}
void nut15()
{
chongrung();
P3=0x79;
P1=0x12;
}
void nut16()
{
chongrung();
P3=0x79;
P1=0x03;
}
void chongrung()
tre(50);
void main()
{
while (1)
{
quetphim();
}
}
Comment