矩阵键盘扫描实验报告

/**************************************************

实验名称:矩阵键盘实验一

实验目的:按下16个矩阵键盘依次在数码管上显示1-16的

 平方。如按下第一个显示1,第二个显示4...

实验备注:

**************************************************/

#include<reg52.h>


#define uchar unsigned char

#define uint unsigned int


sbit DUAN = P2^6;

sbit WEI = P2^7;


uchar code duan_table[]=

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

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

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

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


uchar code wei_table[]=

{0xfe,0xfd,0xfb,

 0xf7,0xef,0xdf};


void delay_1ms(uchar);

void display(uchar,uchar,uchar);

uchar scan_mkey(uchar);


void main()

{

uchar bai, ge, shi,num_tem;

uint num;

num_tem = 0;

num = 0;

while(1)

{

num_tem = scan_mkey(num_tem);

num = num_tem * num_tem;

bai = num/100;

shi = num%100/10;

ge = num%10;

display(bai,shi,ge);

}

}


void delay_1ms(uchar x)

{

uchar i, j;

for(i = 0; i < x; i++)

for(j = 0; j < 110; j++);

}


void display(uchar bai,uchar shi, uchar ge)

{

DUAN = 1;

P0 = duan_table[bai];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table[0];

WEI = 0;

delay_1ms(1);


DUAN = 1;

P0 = duan_table[shi];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table[1];

WEI = 0;

delay_1ms(1);


DUAN = 1;

P0 = duan_table[ge];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table[2];

WEI = 1;

delay_1ms(1);

}


uchar scan_mkey(uchar num_tem)

{

uchar temp, key;

P3 = 0xfe;

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

delay_1ms(10);

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

temp = P3;

switch(temp)

{

case 0xee:

key = 1;

break;

case 0xde:

key = 2;

break;

case 0xbe:

key = 3;

break;

case 0x7e:

key = 4;

break;

}

}

/* while(temp != 0xf0)

{

temp = P3;

temp = temp & 0xf0;

}

*/ }


P3 = 0xfd;

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

delay_1ms(5);

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

temp = P3;

switch(temp)

{

case 0xed:

key = 5;

break;

case 0xdd:

key = 6;

break;

case 0xbd:

key = 7;

break;

case 0x7d:

key = 8;

break;

}

}

/* while(temp != 0xf0)

{

temp = P3;

temp = temp & 0xf0;

}

*/ }


P3 = 0xfb;

temp = P3;

temp = temp &0xf0;

if(temp != 0xf0)

{

delay_1ms(5);

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

temp = P3;

switch(temp)

{

case 0xeb:

key = 9;

break;

case 0xdb:

key = 10;

break;

case 0xbb:

key = 11;

break;

case 0x7b:

key = 12;

break;

}

}

/* while(temp != 0xf0)

{

temp = P3;

temp = temp & 0xf0;

}

*/ }


P3 = 0xf7;

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

delay_1ms(5);

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

temp = P3;

switch(temp)

{

case 0xe7:

key = 13;

break;

case 0xd7:

key = 14;

break;

case 0xb7:

key = 15;

break;

case 0x77:

key = 16;

break;

}

}

/* while(temp != 0xf0)

{

temp = P3;

temp = temp & 0xf0;

}

*/ }

if(key != num_tem)

return key;

else 

return num_tem;

}


/**************************************************

实验名称:矩阵键盘实验二

实验目的:按下16个矩阵键盘依次在数码管上显示1-16

实验备注:

**************************************************/

#include<reg52.h>


#define uchar unsigned char

#define uint unsigned int


sbit DUAN = P2^6;

sbit WEI = P2^7;


uchar code duan_table[]=

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

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

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

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


uchar code wei_table[]=

{0xfe,0xfd,0xfb,

 0xf7,0xef,0xdf};


void delay_1ms(uchar);

void display(uchar);

void scan_mkey();


void main()

{

while(1)

{

scan_mkey();


}

}


void scan_mkey()

{

uchar key, temp;


P3 = 0xfe;

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

delay_1ms(5);

temp = P3;

temp = temp & 0xf0;

if(temp != 0xf0)

{

temp = P3;    //重新赋值必不可少!

switch(temp)

{

case 0xee:

key = 0;

break;

case 0xde:

key = 1;

break;

case 0xbe:

key = 2;

break;

case 0x7e:

key = 3;

break;

}

}

while(temp != 0xf0)

{

display(key);

temp = P3;

temp = temp & 0xf0;

}

}

P0 = 0xff;//这三句用来控制

WEI = 1;  //按着的时候点亮

WEI = 0;  //松手就熄灭



}


void delay_1ms(uchar x)

{

uchar i, j;

for(i = 0; i < x; i++)

for(j = 0; j <110; j++);

}


void display(uchar i)

{

DUAN = 1;

P0 = duan_table[i];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table[0];

WEI = 0;

delay_1ms(1);

}


你可能感兴趣的:(单片机,实验报告,矩阵键盘扫描)