微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PIC16F877矩阵键盘的识别

PIC16F877矩阵键盘的识别

时间:11-24 来源:互联网 点击:
关于矩阵键盘识别方法在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];
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top