Identification of PIC16F877 Matrix Keyboard

Publisher:古泉痴迷者Latest update time:2015-12-21 Source: eefocusKeywords:PIC16F877 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The identification method of the matrix keyboard has been discussed in the 51 matrix keyboard identification. Now let's talk about the difference between the PIC microcontroller and the 51 microcontroller. The main difference is that the input and output of the PIC microcontroller port requires the TRISn register setting, as follows:

 

Identification of PIC16F877 Matrix Keyboard

 

Scanning method:

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


 

Line reversal method:

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

Keywords:PIC16F877 Reference address:Identification of PIC16F877 Matrix Keyboard

Previous article:Timer0 of PIC16F877
Next article:PIC microcontroller entry timer query flashing light program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号