JP3接P0,VCC接+5V,矩陣鍵盤的左邊八個引腳接在P1上面。想要的結果是按第0個按鍵,數碼管顯示0,以此類推。注意,這里的數碼管是共陽極的。
#include#defineuintunsignedint#defineucharunsignedcharucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};ucharcodetable_scan[]={0xfe,0xfd,0xfb,0xf7};voidinit();uchartemp;voidmain(){uinti_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){case0xe0:if(i_row==0)P0=table[0];elseif(i_row==1)P0=table[4];elseif(i_row==2)P0=table[8];elseP0=table[12];break;case0xd0:if(i_row==0)P0=table[1];elseif(i_row==1)P0=table[5];elseif(i_row==2)P0=table[9];elseP0=table[13];break;case0xb0:if(i_row==0)P0=table[2];elseif(i_row==1)P0=table[6];elseif(i_row==2)P0=table[10];elseP0=table[14];break;case0x70:if(i_row==0)P0=table[3];elseif(i_row==1)P0=table[7];elseif(i_row==2)P0=table[11];elseP0=table[15];break;}}}}voidinit(){P0=0xff;}
注意,代碼中沒有進行消抖操作,是因為這個不消抖不會影響實驗結果。