打印
[其他产品]

PIC16F877矩阵键盘的识别

[复制链接]
495|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
phosphate|  楼主 | 2020-2-10 17:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原文地址:http://blog.sina.com.cn/s/blog_4a3946360100mwqh.html
关于矩阵键盘的识别方法在51矩阵键盘识别中已经说过,现在要说的是PIC单片机与51单片机的区别,主要是PIC单片机的口子的输入输出需要TRISn寄存器设置,

使用特权

评论回复
沙发
phosphate|  楼主 | 2020-2-10 17:53 | 只看该作者
扫描法:

#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
uchar num;
const ucharSSEG[]={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;
    case0x7d: 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;
    case0xbd: 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;
    case0xdd: 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;
    case0xed: 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];
}
}

使用特权

评论回复
板凳
phosphate|  楼主 | 2020-2-10 17:53 | 只看该作者
线反转法:

#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
uchar num;
const ucharSSEG[]={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)
   {
    case0xee: 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];
}
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

32

主题

393

帖子

1

粉丝