專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機教程網(wǎng) >> MCU設(shè)計實例 >> 瀏覽文章

矩陣鍵盤反轉(zhuǎn)掃描(中斷觸發(fā))數(shù)碼管新認(rèn)識

作者:胡永明   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年12月10日   【字體:


 1、電 路 圖
 2、效 果 圖
 3、代 碼
 4、小 結(jié)( 數(shù)碼管)




                           
                                        代 碼
 

#include <reg52.h>
sbit dm = P2^2; //段碼
sbit wm = P2^3; //位碼
sbit led =P3^7; 
unsigned char hc[8];
unsigned char jj=0;
unsigned char DM[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 顯示段碼值0~9
unsigned char WM[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位碼。顯示的位置
void DelayUs2x(unsigned char t)
{   
 while(--t);
}
void DelayMs(unsigned char t)
{
     
 while(t--)
 {
     //大致延時1mS
     DelayUs2x(245);
DelayUs2x(245);
 }
}
void sm()
{
unsigned char ii=0;
while(8!=ii && hc[ii]!=0)
{
P1 = 0; //消影
dm = 1;
dm = 0;

P1 = WM[ii]; //寫入位碼
wm = 1;
wm = 0;

P1 = hc[ii]; //寫入段碼
dm = 1;
dm = 0;
DelayUs2x(10);
ii++;
}
}

/*以下函數(shù)就是反轉(zhuǎn)掃描的精華*/
unsigned char jpsm() //矩陣鍵盤反轉(zhuǎn)掃描
{
unsigned char i=0; //用于接收按鍵數(shù)值 
P0 = 0x0f; //檢測低4位
if(0x0f != P0) //檢測按鍵是否按下
{
DelayMs(10); //去抖
if(0x0f != P0) //在次判斷按鍵是否真的按下而不是其他干擾
{
i = P0; //把低4位賦值給i
P0 = 0xf0; //檢測高4位
DelayUs2x(5); //稍微延時。。這條語句可以去掉
i = i | P0; //把低4位和高4位組合成完整的按鍵數(shù)值
while( 0xf0 != P0 ) //檢測按鍵是否松開
{
sm();
}
return i; //返回按鍵數(shù)值
}
}
return 0; //按鍵沒有按下返回0
}

unsigned char hcxr(unsigned char i) //按鍵解碼函數(shù)
{
switch(i)
{
case 0x77:hc[jj]=DM[0];break; //0
case 0x7e:hc[jj]=DM[1];break; //1
case 0xbe:hc[jj]=DM[2];break; //2
case 0xde:hc[jj]=DM[3];break; //3
case 0x7d:hc[jj]=DM[4];break; //4
case 0xbd:hc[jj]=DM[5];break; //5
case 0xdd:hc[jj]=DM[6];break; //6
case 0x7b:hc[jj]=DM[7];break; //7
case 0xbb:hc[jj]=DM[8];break; //8
case 0xdb:hc[jj]=DM[9];break; //9
/*case 0xee: //+
case 0xed: //-
case 0xeb: //*
case 0xe7: ///
case 0xd7: //=
case 0xb7: //.*/
default :return 0;
}
if(jj<7)
{
jj++;
hc[jj]=0;
}
else
jj=0;

}
void csh_wbzd_0() //初始化外部中斷0
{
IT0=0; //外部中斷0下降沿觸發(fā)
EX0=1; //啟用外部中斷0
EA=1; //打開總中斷

}
void wbzd_0() interrupt 0 //外部中斷服務(wù)函數(shù)
{
unsigned char i;
i=jpsm(); //接收按鍵值
hcxr(i); //按鍵值解碼
}
void main()
{
 
csh_wbzd_0(); //初始化外部中斷0
P0=0xf0; //這里很重要用于中斷檢測
while(1)
{
sm(); //循環(huán)掃描數(shù)碼管
}

} 

小結(jié)
1、數(shù)碼管的緩存數(shù)據(jù)一般有兩個地方 。
    1)是在單片機里我們自己聲明的。
    2)是在鎖存器里也有數(shù)據(jù)。 

關(guān)閉窗口

相關(guān)文章