PIC16F877 matrix keyboard

Publisher:幸福的人生Latest update time:2016-11-04 Source: eefocusKeywords:PIC16F877 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/Purpose of the experiment: Familiarize yourself with the scanning method of the keyboard matrix

//The program does not perform key debounce, nor does it consider the situation where multiple keys are pressed at the same time.
//The lowest two digital tubes display the corresponding keys (e.g., if S10 is pressed, 10 is displayed; if S25 is pressed, 25 is displayed)
//FF is displayed when no key is pressed 
//The key and key scanning results satisfy the following relationship:
// Key Scan Result (result) Key Scan Result  
// K10 0XE7 K18 0XB7
// K11 0XEB K19 0XBB
// K12 0XED K20 0XBD
// K13 0XEE K21 0XBE
// K14 0XD7 K22 0X77
// K15 0XDB K23 0X7B
// K16 0XDD K24 0X7D
// K17 0XDE K25 0X7E

//Hardware requirements: All DIP switches S4 are set to ON
// All DIP switches S6 are set to ON, DIP switch S5 positions 5-6 are ON, and other positions are OFF
// Other DIP switches are set to OFF

#include //Includes the predefined
 __CONFIG(0x1832) internal resources of the MCU;        
//Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off, encryption, 4M crystal HS oscillation

 int result;
 void delay(); //delay function declaration
 void init(); //I/O port initialization function declaration
 void scan(); //Key scanning program declaration
 void display(int x); //Display function declaration
//---------------------------------------------------
                             //Main program  
void main()
{
  while(1) //Loop work
    {
      init(); //Call initialization subroutine
      scan(); //Call key scanning subroutine
      display(result); //Call result display subroutine
    }
 }
 
//---------------------------------------------------
//Initialization function
void init() 
 {
  ADCON1=0X07; //Set port A as a normal I/O port
  TRISA=0X0f; //Set the high 2 bits of port A as output and the low 4 bits as input
  TRISC=0XF0; //Set the high 4 bits of port C as input and the low 4 bits as output
  TRISD=0X00; //Set port D as output PORTA
  =0XFF;              
  PORTD=0XFF; //Clear all displays first
 }

//---------------------------------------------------
//Key scanning program
void scan()
{
 PORTC=0XF7; //C3 outputs low level, and the other three bits output high level
 asm("nop"); //Insert a certain delay to ensure that the level is stable
 result=PORTC; //Read back the high 4 bits of port C
 result=result&0xf0; //Clear the low 4 bits
 if(result!=0xf0) //Judge whether the high 4 bits are all 1 (all 1 means no button is pressed)?
   {
     result=result|0x07; //No, add the lower 4 bits 0x07 as the result of the key scan
   }
 else //Yes, change the lower 4 bits output and re-judge whether a key is pressed
   {
   PORTC=0XFb; //C2 outputs low level, and the other three bits output high levelasm
   ("nop"); //Insert a certain delay to ensure that the level is stableresult
   =PORTC; //Read back the high 4 bits of port
   Cresult=result&0xf0; //Clear the low 4 bitsif
   (result!=0xf0) //Judge whether the high 4 bits are all 1 (all 1 means no key is pressed)
     {
     result=result|0x0b; //No, add the lower 4 bits 0xb as the result of the key scan
     }
   else //Yes, change the lower 4 bits output and re-scan
       {
       PORTC=0XFd; //C1 outputs low level, and the other three bits output high levelasm
       ("nop"); //Insert a certain delay to ensure that the level is stableresult
       =PORTC; //Read back the high 4 bits of port C
       result=result&0xf0; //Clear the low 4 bitsif
       (result!=0xf0) //Judge whether the high 4 bits are all 1 (all 1 means no button is pressed)
        {
        result=result|0x0d; //No, add the low 4 bits 0x0d as the result of the key scan
        }
       else //Yes, change the output of the low 4 bits and rescan
          {
          PORTC=0XFe; //C0 outputs low level, and the other three bits output high levelasm
          ("nop"); //Insert a certain delay to ensure that the level is stableresult
          =PORTC; //Read back the high 4 bits of port C
          result=result&0xf0;//Clear the low 4 bitsif
          (result!=0xf0) //Judge whether the high 4 bits are all 1 (all 1 means no button is pressed)
            {
             result=result|0x0e;//No, add the low 4 bits 0x0e as the result of the key scan
            }
          else //Yes, all key scans are completed, no key is pressed, set the no key pressed flag
            {
             result=0xff; //The scan result is 0xff, which is used as a sign that no key is pressed
            }   
          }      
      }
   }
 }

//------------------------------------------------ ----------
//Display program
void display(int x)
  {
   switch(result)                 
      {
       case 0xe7:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xc0;PORTA=0X1F;delay ();break; //K10
       case 0xeb:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xf9;PORTA=0X1F;delay();break; //K11
       case 0xed:
                 PORTD=0xf9;PORTA=0X2F ;delay();PORTD=0xa4;PORTA=0X1F;delay();break; //K12
       case 0xee:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xb0;PORTA=0X1F;delay();break ; //K13
       case 0xd7:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0x99;PORTA=0X1F;delay();break; //K14
       case 0xdb:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0x92;PORTA= 0X1F;delay();break; //K15
       case 0xdd:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X82;PORTA=0X1F;delay();break; //K16
       case 0xde:
                 PORTD=0xf9; PORTA=0X2F;delay();PORTD=0XF8;PORTA=0X1F;delay();break; //K17
       case 0xb7:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X80;PORTA=0X1F;delay();break; //K18
       case 0xbb:
                 PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X90;PORTA= 0X1F;delay();break; //K19
       case 0xbd:
                 PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xc0;PORTA=0X1F;delay();break; //K20
       case 0xbe:
                 PORTD=0xa4; PORTA=0X2F;delay();PORTD=0xf9;PORTA=0X1F;delay();break; //K21
       case 0x77:
                 PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xa4;PORTA=0X1F;delay();break; //K22
       case 0x7b:
                 PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xb0;PORTA= 0X1F;delay();break; //K23
       case 0x7d:
                 PORTD=0xa4;PORTA=0X2F;delay();PORTD=0x99;PORTA=0X1F;delay();break; //K24
       case 0x7e:
                 PORTD=0xa4; PORTA=0X2F;delay();PORTD=0x92;PORTA=0X1F;delay();break; //K25
       case 0xff:
                 PORTD=0x8e;PORTA=0X2F;delay(); PORTD=0x8e; PORTA=0X1F; delay(); //No button pressed
      }
   }

//------------------------------------------------------------------
//Delay program
void delay() //Delay program
    {
     int i; //Define integer variables
     for(i=0x100;i--;); //Delay
    }

Keywords:PIC16F877 Reference address:PIC16F877 matrix keyboard

Previous article:PIC16F877A CCP Usage
Next article:PIC16F877A sleep mode

Recommended ReadingLatest update time:2024-11-16 15:37

Design of wireless data transmission system based on nRF24L01 and PIC16F877
introduction In industrial control sites, it is often necessary to collect a large amount of field data, such as temperature, humidity, air pressure, etc., and transmit these data to the host for processing. The host transmits the control signal to the field execution module for various operations based on the
[Microcontroller]
Design of wireless data transmission system based on nRF24L01 and PIC16F877
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号