AVR's simple and easy-to-use 4x4 matrix keyboard

Publisher:朝霞暮雨Latest update time:2016-07-18 Source: eefocusKeywords:AVR  4x4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
// Target : ATMEGA16 

// Crystal: 8.0000Mhz

//key.h

#ifndef _4X4_H_
#define _4X4_H_

#define No_key  255

#define key_port      PORTB 
#define key_port_ddr  DDRB 
#define key_port_pin  PINB


unsigned char keyboard_Scan(void);

#endif

//key.c

#include  
#include

#include "key.h"

const unsigned char key_table[16] =  
      { 
         1, 2, 3,12, 
         4, 5, 6,13, 
         7, 8, 9,14,                
         10,0,11,15, 
     };

void delay_1ms(void)                 //1ms延时函数
  {
    unsigned int i;
    for (i=0;i<1140;i++);
  }
  
void delay_nms(unsigned int n)       //N ms延时函数
  {
    unsigned int i=0;
    for (i=0;i    delay_1ms();
  }

//4x4 matrix keyboard scanning function

unsigned char keyboard_Scan(void) 

    unsigned char temp,key,row,Column;                         

    key_port_ddr = 0b00001111; // high four bits input row line / low four bits output column line 
    key_port = 0b11110000; // high four bits open pull-up resistor / low four bits output low level 
    delay_nus(5); // delay 5us 
    if((key_port_pin & 0xF0)!= 0xF0) // make an initial check to see if any key is pressed, if not, return 
   { // if not all row lines are 1, a key may be pressed 
     delay_nms(5); // delay debounce 

      //Set the initial value of the column line 3~0=1110        
     for(Column=0,key_port=0b11111110;Column<4;Column++) 
     {                 
         for(row=0,temp=0b11101111;row<4;row++)//Set the initial value of the row line 7~4=1110 
       {   
           while((key_port_pin & 0xF0)==(temp & 0xF0))//Enter the row line and check whether there is a key pressed in this row 
           { 
           key=4*row+Column;//Key code = 4*row input value + column scan valuekey 
           =key_table[key];//Keyboard code conversion key valuereturn 
            (key); 
            } 
          temp<<=1;//Shift the row line left by 1 bit 
        } 
      key_port=((key_port<<1)|0x01);//Shift the column line scan value left by 1 bit and scan the next row 
     } 
   } 
   return (No_key); 
}

//main.c

#include  
#include

#include "key.h"

void main(void)
{
  unsigned char Key;
  while(1)
   {
     Key = keyboard_Scan();            // 键盘扫描 
     switch(Key)
      {
        case 0:
        break;

        ......
  
       default:
       break;
     }
   }
}

I personally think it is better to use a timer for the delay in the main function.

The key_table array can be modified if the circuit connection is different

Keywords:AVR  4x4 Reference address:AVR's simple and easy-to-use 4x4 matrix keyboard

Previous article:AVR six IO ports drive LCD1602
Next article:AVR Learning Experience (IV)

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号