找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5161|回復: 0
打印 上一主題 下一主題
收起左側

PIC16F877矩陣鍵盤的識別

[復制鏈接]
跳轉到指定樓層
樓主
ID:105323 發(fā)表于 2016-2-13 02:19 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
關于矩陣鍵盤的識別方法在51矩陣鍵盤識別中已經(jīng)說過,現(xiàn)在要說的是PIC單片機與51單片機的區(qū)別,主要是PIC單片機的口子的輸入輸出需要TRISn寄存器設置,具體如下:



掃描法:
#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];
}
}


線反轉法:
#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];
}
}



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表