您的位置 首页 数字

PIC16F877矩阵键盘的辨认

关于矩阵键盘的识别方法在51矩阵键盘识别中已经说过,现在要说的是PIC单片机与51单片机的区别,主要是PIC单片机的口子的输入输出需要TRISn

关于矩阵键盘辨认办法在51矩阵键盘辨认中现已说过,现在要说的是PIC单片机51单片机的差异,主要是PIC单片机的口儿的输入输出需求TRISn寄存器设置,详细如下:

扫描法:

#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];
}
}

线反转法:

#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)//松手检测
{
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];
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/shuzi/260284.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部