99网
您的当前位置:首页51单片机程序

51单片机程序

来源:99网


//该程序用数码管显示按键的编码及编码的平方

#include

#define uint unsigned int

#define uchar unsigned char

sbit dula=P2^6;

sbit wela=P3^7;

uint numone,numtwo;

uchar temp,one,two,three,four,five,six;

uchar code list[]={

0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};

void delay(uint z)

{

uint x,y;

for(x=100;x>0;x--)

for(y=z;y>0;y--);

}

void shuma(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)

{

if(a==0)

P0=0xff;

else

{

dula=1;

P0=list[a];

dula=0;

P0=0xff;

wela=1;

P0=0xfe;

wela=0;

delay(1);

}

if(b==0)

P0=0xff;

else

{

dula=1;

P0=list[b];

dula=0;

P0=0xff;

wela=1;

P0=0xfd;

wela=0;

delay(1);

}

if((c==0)&&(b==0))

P0=0xff;

else

{

dula=1;

P0=list[c];

dula=0;

P0=0xff;

wela=1;

P0=0xfb;

wela=0;

delay(1);

}

if(d==0)

P0=0xff;

else

{

dula=1;

P0=list[d];

dula=0;

P0=0xff;

wela=1;

P0=0xf7;

wela=0;

delay(1);

}

if((e==0)&&(d==0))

P0=0xff;

else

{

dula=1;

P0=list[e];

dula=0;

P0=0xff;

wela=1;

P0=0xef;

wela=0;

delay(1);

}

if((f==0)&&(e==0)&&(d==0))

P0=0xff; //注意此处是为了解决第十个按键按下时,十位和个位不显示的问题

else

{

dula=1;

P0=list[f];

dula=0;

P0=0xff;

wela=1;

P0=0xdf;

wela=0;

delay(1);

}

}

void init()

{

one=numone%10; two=numone%100/10;

three=numone/100;

four=numtwo%10; five=numtwo%100/10;

six=numtwo/100; }

void main()

{

while(1)

{

//numone表示第几个按下

//numtwo表示按下的平方

P3=0xfe;

temp=P3;

temp=temp&0xf0;

if(temp!=0xf0)

{

delay(5);

if(temp!=0xf0)

{

switch(temp)

{

case 0xe0: numone=1;numtwo=1;P1=0xfe;break;

case 0xd0: numone=2;numtwo=4;P1=0xfd;break;

case 0xb0: numone=3;numtwo=9;P1=0xfc;break;

case 0x70: numone=4;numtwo=16;P1=0xfb;break;

} //下面两行放于此处数码管只有在键盘按下时才会显示

}

}

init();

shuma(three,two,one,six,five,four);

P3=0xfd;

temp=P3;

temp=temp&0xf0;

if(temp!=0xf0)

{

delay(5);

if(temp!=0xf0)

{

switch(temp)

{

case 0xe0: numone=5;numtwo=25;P1=0xfa;break;

case 0xd0: numone=6;numtwo=36;P1=0xf9;break;

case 0xb0: numone=7;numtwo=49;P1=0xf8;break;

case 0x70: numone=8;numtwo=;P1=0xf7;break;

} //下面两行放于此处数码管只有在键盘按下时才会显示

}

}

init();

shuma(three,two,one,six,five,four);

P3=0xfb;

temp=P3;

temp=temp&0xf0;

if(temp!=0xf0)

{

delay(5);

if(temp!=0xf0)

{

switch(temp)

{

case 0xe0: numone=9;numtwo=81;P1=0xf6;break;

case 0xd0: numone=10;numtwo=100;P1=0xf5;break;

case 0xb0: numone=11;numtwo=121;P1=0xf4;break;

case 0x70: numone=12;numtwo=144;P1=0xf3;break;

} //下面两行放于此处数码管只有在键盘按下时才会显示

}

}

init();

shuma(three,two,one,six,five,four);

P3=0xf7;

temp=P3;

temp=temp&0xf0;

if(temp!=0xf0)

{

delay(5);

if(temp!=0xf0)

{

switch(temp) //用二极管指示按下第几个按键

{

case 0xe0: numone=13;numtwo=169;P1=0xf2;break;

case 0xd0: numone=14;numtwo=196;P1=0xf1;break;

case 0xb0: numone=15;numtwo=225;P1=0xf0;break;

case 0x70: numone=16;numtwo=256;P1=0xef;break;

} //下面两行放于此处数码管只有在键盘按下时才会显示

}

}

init();

shuma(three,two,one,six,five,four);

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容