AVR microcontroller matrix keyboard program (2 methods)

Publisher:科技律动Latest update time:2015-07-20 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
AVR matrix keyboard 2 methods to achieve!

Normal method:

#include//
#include//
#define uchar unsigned char//
#define uint unsigned int//
#define setb(val,bitn) (val|=(1<<(bitn)))//Set a bit of an IO port to 1
#define clr(val,bitn) (val&=~(1<<(bitn)))//Set a bit of an IO port to 0
#define get(val,bitn) (val&(1<<(bitn)))//Read a bit status such as if(get(PORTC,7)) determines whether PC7 port is 1
#pragma data:code//Array storage location
const table[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,
0x8e
};//Common anode digital tube 0--9 code
uchar num,yin,temp;//Key value code and identification code
void delay(uchar);//Delay function
void disy();//Display function
void skk()
{
  for(yin=0;yin<2;yin++)
  {
   PORTD=0xfe;//Set the first line to 0, that is, detect the first line
  temp=PIND;//Give the value to tem
  temp=temp&0xf0;//AND gate with 0xf0,
  while(temp!=0xf0)//Judge whether it is 0xf0
  {
   delay(10);//Delay
   temp=PIND;//Give P1 value to tem
   temp=temp&0xf0;//AND gate with the value again
   while(temp!=0xf0)//Judge whether it is 0xf0 again
   {
    temp=PIND;//It is indeed pressed, and the tem value is restored so that the switch statement can be judged next time. Otherwise, there is no corresponding value. For example, if it was ee before, if the second key is pressed, it will not be de
    switch(temp)//switch statement
     {
      case 0xee:num=0;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0xde:num=1;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0xbe:num=2;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0x7e:num=3;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
     }
    while(temp!=0xf0)//Release the judgment to exit the above two while
    {
     temp=PIND;//assign value to temp
     temp=temp&0xf0;//AND gate
    }
   }
  }
 }
 
  for(yin=0;yin<2;yin++)
      { 
   PORTD=0xfd;//set the first line to 0, that is, detect the first line
  temp=PIND;//assign value to tem
  temp=temp&0xf0;//AND gate with 0xf0,
  while(temp!=0xf0)//judge whether it is 0xf0
  {
   delay(10);//delay
   temp=PIND;//assign P1 value to tem again
   temp=temp&0xf0;//AND gate with 0xf0
   while(temp!=0xf0)//judge whether it is 0xf0 again
   {
    temp=PIND;//it is indeed pressed, put the tem value back, so as to judge the switch statement next time, otherwise there is no corresponding value, such as ee originally, if the second key is pressed, it will not be de
    switch(temp)//switch statement
     {
      case 0xed:num=4;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0xdd:num=5;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0xbd:num=6;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0x7d:num=7;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
     }
    while(temp!=0xf0)//Release judgment to exit the above two while
    {
     temp=PIND;//Assign value to temp
     temp=temp&0xf0;//AND gate
    }
   }
  }
 }
 for(yin=0;yin<2;yin++)
      { 
   PORTD=0xfb;//Set the first line to 0, that is, check the first line
  temp=PIND;//Assign value to tem
  temp=temp&0xf0;//AND gate with 0xf0,
  while(temp!=0xf0)//Judge whether it is 0xf0
  {
   delay(10);//Delay
   temp=PIND;//Assign P1 value to tem
   temp=temp&0xf0;//AND gate with value again
   while(temp!=0xf0)//Judge whether it is 0xf0 again
   {
    temp=PIND;//It is indeed pressed, and the tem value is restored so that the switch statement can be judged next time. Otherwise, there is no corresponding value. For example, if it was ee before, if the second key is pressed, it will not be de
    switch(temp)//switch statement
     {
      case 0xeb:num=8;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0xdb:num=9;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0xbb:num=10;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
      case 0x7b:num=11;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
      break;//Exit the switch statement
     }
    while(temp!=0xf0)//Release the judgment to exit the above two while
    {
     temp=PIND;//Assign value to temp
     temp=temp&0xf0;//AND gate
    }
   }
  }
 }
  for(yin=0;yin<2;yin++)
       { 
   PORTD=0xf7;//Set the first line to 0, i.e., check the first line
  temp=PIND;//Assign value to tem
  temp=temp&0xf0;//AND gate with 0xf0,
  while(temp!=0xf0)//Judge whether it is 0xf0
  {
   delay(10);//Delay
   temp=PIND;//Assign P1 value to tem again
   temp=temp&0xf0;//AND gate with 0xf0
   while(temp!=0xf0)//Judge whether it is 0xf0 again
   {
    temp=PIND;//It is indeed pressed, put the tem value back, so as to judge the switch statement next time, otherwise there is no corresponding value, such as ee originally, if the second key is pressed, it will not be de
    switch(temp)//Switch statement
     {
      case 0xe7:num=12;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0xd7:num=13;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0xb7:num=14;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
      case 0x77:num=15;//Compare with num value to see if they are equal, if they are equal, exit switch statement
      break;//Exit switch statement
     }
    while(temp!=0xf0)//Release judgment to exit the above two while
    {
     temp=PIND;//Assign value to temp
     temp=temp&0xf0;//AND gate
    }
   }
  }

 }
}[page]
void main()
{
     DDRD|=0xff;//Set PB port as output
     PORTD|=0xff;//PB output is high potential
     DDRD&=0x0f;//Set high
     while(1)//Large loop
     {
      skk();
 disy();//Call display subroutine 
     }
}
void delay(uchar z)//Delay subfunction
{
 uchar x,y;
 for(x=100;x>0;x--)
 for(y=z;y>0;y--); 
}
void disy()//Display subfunction
{    
     DDRC=0xff;
     DDRB=0xff;
     PORTB=table[num];
     PORTC=0x00;
}

Simple method:

#include
#include
#define uchar unsigned char
#define uint unsigned int
#pragma data:code//Define the program to be stored in ROM
const table[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,
0x8e,0xff
};
/***************************************************/
/*******************Delay subroutine******************/
/***************************************************/
void delay(uchar z)
{
   uchar x,y;
  for(x=0;x<50;x++)
  for(y=0;y }
/***************************************************/
/*******************Display subroutine******************/
/***************************************************/
void xian(uchar num)
{
  uchar i;
 PORTB=table[num];//ten digits
 PORTC=0x00;
}
/***************************************************/
/***************Key press processing procedure***************/
/***************************************************/
uchar key_press()
{
     uchar i;
     DDRD=0XFF;//Set PD port to output
  PORTD=0XF0;//Set the upper four bits of PD to high level for judgment
  DDRD=0X0F;//Set the upper 4 bits to input and the lower 4 bits to output
  i=PIND;//Read the IO port status
  if(i==0XF0)//If it is equal to 0XF0, return 0
  {
    DDRD=0XFF;//Read the IO port value return
    0;
  }
  else//Otherwise it will return 1
  {
    DDRD=0XFF;
    return 1;
  }
}
/***********************************************/
/***************Key value processing program***************/
/***************************************************/
uchar key_scan()
{
    uchar key,i=0X7F,j;//Set variable I to 01111111B
 if(key_press())//Judge whether the key is pressed
 {
      delay(10);//If it is pressed, eliminate the bounce
   if(key_press())//Judge again whether the key is pressed
   {
     do//Execute once first
  {
   i=(i<<1|i>>7);/*Shift the content of variable I left by 1 bit, 11111110
   and then shift I right by 7 bits, 11111110, that is, 11111111 
   11111101 11111011 11110111*/
   PORTD=i;//Send the shifted value to the PD port
   DDRD=0X0F;//Set the high bit of the PD port as input and the low bit as output
   key=PIND;//Read the high bit value of the IO port
   j=key&0XF0;//Only need to read the high 4 bits
    }
  while(j==0XF0);//If the key is not pressed, execute the above do statement
  while(key_press());//If it is pressed, release it
  switch(key)//If a key is pressed, determine which value it is
  {
          case 0xEE:
      key=0x0;
   break;
      case 0xDE:
      key=0x1;
   break; 
      case 0xBE:
      key=0x2;
   break;
      case 0x7E:
      key=0x3;
   break;
      case 0xED:
      key=0x4;
   break;
      case 0xDD:
      key=0x5;
   break;
      case 0xBD:
      key=0x6;
   break;
     case 0x7D:
      key=0x7;
   break;
      case 0xEB:
      key=8;
   break ;
      case 0xDB:
      key=9;
   break;
      case 0xBB:
      key=0xA;
   break;
      case 0x7B:
      key=0xB;
   break;
      case 0xE7:
      key=0xC;
   break;
      case 0xD7:
      key=0xD;
   break;
        case 0xB7:
      key =0xE;
   break;
      case 0x77:
      key=0xF;
   break;
  }
  }
  }
  return key;//return key value
}
/**************************** ************************/
/************************Main program*** ******************/
/****************************** *******************/
void main()
{
   uchar ik=16;//define local variable
   DDRB=0xff;//set PB port as output
  DDRC=0xff;//set PC port as output
  while(1)
  {
     ik=key_scan();//set the key return value to the variable IK
     if(ik<16)//If the return value is less than 16, it will be displayed on the digital tube
   xian(ik); //Call the display subroutine
   else//If the return value is not less than 16, a black screen will be displayed
   xian(16);
  }
}

Reference address:AVR microcontroller matrix keyboard program (2 methods)

Previous article:AVR MCU timer count
Next article:AVR microcontroller password lock program (original)

Latest Microcontroller Articles
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号