JP3接P0,VCC接+5V,矩阵键盘的左边八个引脚接在P1上面。想要的结果是按第0个按键,数码管显示0,以此类推。注意,这里的数码管是共阳极的。
#include<reg51.h> #define uint unsigned int #define uchar unsigned char uchar code table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e}; uchar code table_scan[] = {0xfe, 0xfd, 0xfb, 0xf7}; void init(); uchar temp; void main() { uint i_row; init(); while(1) { for(i_row=0; i_row<4; i_row++) { P1 = table_scan[i_row]; temp = P1; temp = P1 & 0xf0; switch(temp) { case 0xe0: if(i_row==0) P0 = table[0]; else if(i_row==1) P0 = table[4]; else if(i_row==2) P0 = table[8]; else P0 = table[12]; break; case 0xd0: if(i_row==0) P0 = table[1]; else if(i_row==1) P0 = table[5]; else if(i_row==2) P0 = table[9]; else P0 = table[13]; break; case 0xb0: if(i_row==0) P0 = table[2]; else if(i_row==1) P0 = table[6]; else if(i_row==2) P0 = table[10]; else P0 = table[14]; break; case 0x70: if(i_row==0) P0 = table[3]; else if(i_row==1) P0 = table[7]; else if(i_row==2) P0 = table[11]; else P0 = table[15]; break; } } } } void init() { P0 = 0xff; }
原文地址:http://blog.csdn.net/kotei_88_luluc_66/article/details/40187611