新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > PIC16F877矩陣鍵盤的識(shí)別

PIC16F877矩陣鍵盤的識(shí)別

作者: 時(shí)間:2016-11-24 來(lái)源:網(wǎng)絡(luò) 收藏
關(guān)于矩陣鍵盤識(shí)別方法在51矩陣鍵盤識(shí)別中已經(jīng)說(shuō)過(guò),現(xiàn)在要說(shuō)的是PIC單片機(jī)51單片機(jī)的區(qū)別,主要是PIC單片機(jī)的口子的輸入輸出需要TRISn寄存器設(shè)置,具體如下:

本文引用地址:http://m.butianyuan.cn/article/201611/320853.htm



掃描法:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num;
const uchar SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
uint x;
uchar y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void keyscan()
{
uchar temp;
TRISB=0x0f;
PORTB=0x7f;
temp=PORTB&0x0f;
if(temp!=0x0f)
{
delay1ms(10);
temp=PORTB&0x0f;
if(temp!=0x0f)
{
num=temp|0x70;
}
temp=PORTB&0x0f;
while(temp!=0x0f)
{
temp=PORTB&0x0f;
}
switch(num)
{
case 0x7e: num =12;break;
case 0x7d: num =13;break;
case 0x7b: num =14;break;
case 0x77: num =15;break;
}
}
PORTB=0xbf;
temp=PORTB&0x0f;
if(temp!=0x0f)
{
delay1ms(10);
temp=PORTB&0x0f;
if(temp!=0x0f)
{
num=temp|0xb0;
}
temp=PORTB&0x0f;
while(temp!=0x0f)
{
temp=PORTB&0x0f;
}
switch(num)
{
case 0xbe: num =8;break;
case 0xbd: num =9;break;
case 0xbb: num =10;break;
case 0xb7: num =11;break;
}
}
PORTB=0xdf;
temp=PORTB&0x0f;
if(temp!=0x0f)
{
delay1ms(10);
temp=PORTB&0x0f;
if(temp!=0x0f)
{
num=temp|0xd0;
}
temp=PORTB&0x0f;
while(temp!=0x0f)
{
temp=PORTB&0x0f;
}
switch(num)
{
case 0xde: num =4;break;
case 0xdd: num =5;break;
case 0xdb: num =6;break;
case 0xd7: num =7;break;
}
}
PORTB=0xef;
temp=PORTB&0x0f;
if(temp!=0x0f)
{
delay1ms(10);
temp=PORTB&0x0f;
if(temp!=0x0f)
{
num=temp|0xe0;
}
temp=PORTB&0x0f;
while(temp!=0x0f)
{
temp=PORTB&0x0f;
}
switch(num)
{
case 0xee: num =0;break;
case 0xed: num =1;break;
case 0xeb: num =2;break;
case 0xe7: num =3;break;
}
}
}

void main()
{
num=0xff;
TRISD=0;
PORTD=0;
while(1)
{
keyscan();
PORTD=~SSEG[num];
}
}


線反轉(zhuǎn)法:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num;
const uchar SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
uint x;
uchar y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void keyscan()
{
uchar temp,z,x;
TRISB=0x0f;
PORTB=0x0f;
x=PORTB&0x0f;
if(x!=0x0f)
{
delay1ms(10);
x=PORTB&0x0f;
if(x!=0x0f)
{
temp=PORTB&0x0f;
TRISB=0xf0;
PORTB=0xf0;
z=temp|PORTB;
x=PORTB&0xf0;
while(x!=0xf0)//松手檢測(cè)
{
x=PORTB&0xf0;
}
switch(z)
{
case 0xee: num =0; break;
case 0xde: num =4; break;
case 0xbe: num =8; break;
case 0x7e: num =12; break;
case 0xed: num =1; break;
case 0xdd: num =5; break;
case 0xbd: num =9; break;
case 0x7d: num =13; break;
case 0xeb: num =2; break;
case 0xdb: num =6; break;
case 0xbb: num =10;break;
case 0x7b: num =14;break;
case 0xe7: num =3;break;
case 0xd7: num =7;break;
case 0xb7: num =11;break;
case 0x77: num =15;break;
}
}
}
}

void main()
{
num=0xff;
TRISD=0;
PORTD=0;
while(1)
{
keyscan();
PORTD=~SSEG[num];
}
}



關(guān)鍵詞: PIC16F877矩陣鍵盤識(shí)

評(píng)論


技術(shù)專區(qū)

關(guān)閉