Matrix keyboard experiment

Publisher:Changfeng520Latest update time:2016-12-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Code:

#include
typedef unsigned char  u8;
typedef unsigned int   u16;
sbit seg_sel = P1^4;
sbit bit_sel = P1^5;
#define DATA P0
u8 code seg_tab[]={
              0xc0,0xf9,0xa4,0xb0,
              0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,
              0xc6,0xa1,0x86,0x8e
             };
u8 code bit_tab[]={
              0x10,0x20,0x40,0x80,
              0x01,0x02,0x04,0x08
             };
void delay(u16 num)
{
    u16 x,y;
    for(x=num; x>0; x--)
        for(y=110; y>0; y--)
        {
            ;//延时1ms
        }
}
void display_led(u8 which_bit, u8 which_number)
{
        bit_sel = 1;//Q[7..0]=D[7..0]
        DATA = bit_tab[which_bit];
        bit_sel = 0;//BIT[7..0]=0x80
        seg_sel = 1;//Q[7..0]=D[7..0]
        DATA = seg_tab[which_number];
        seg_sel = 0;
        delay(2);    
}
u8 key_scan (void)
{
    u8 temp,temp2;
    P2 = 0xf0;//让P2输出0xf0
    temp = P2;//读入P2的值
    if (temp != 0xf0) //当P2读入的值不等于0xf0
    {
        delay(5);//延迟5ms
        temp = P2;//再读入P2口的值
        if (temp != 0xf0)//如果P2读入的值不等于
        {                 //0xf0,就说明有按键按下
            temp2 = temp & 0xf0;//保留P2读入值得高四位             
            P2 = 0x0f;//再让P2口输出0x0f
            delay(1);
            temp = P2;//再读入P2口的值
            temp2 |= temp;//保留此读入值的低四位
            return temp2;
        }
        //while(P2!=0x0f);
    }
}
u8 encode(u8 cod)
{
    u8 num;
    switch(cod)
    {
        case 0xee: num = 0;break;
        case 0xde: num = 1;break;
        case 0xbe: num = 2;break;
        case 0x7e: num = 3;break;
        case 0xed: num = 4;break;
        case 0xdd: num = 5;break;
        case 0xbd: num = 6;break;
        case 0x7d: num = 7;break;
        case 0xeb: num = 8;break;
        case 0xdb: num = 9;break;
        case 0xbb: num = 10;break;
        case 0x7b: num = 11;break;
        case 0xe7: num = 12;break;
        case 0xd7: num = 13;break;
        case 0xb7: num = 14;break;
        case 0x77: num = 15;break;
        default : break;
    }
    return num;
}
void main (void)
{
    u8 cod,num;
    P0 = 0xff;
    P1 = 0xff; //Port initialization
    seg_sel = 0; //LE disable latch
    bit_sel = 0;
    while (1)
    {
        cod = key_scan(); //Judgement and encoding
        num = encode(cod); //Decoding program. Get the number
        display_led(1, num); //Display program
    }
}

2. Circuit diagram:

 

3. Effect


Reference address:Matrix keyboard experiment

Previous article:External interrupt experiment
Next article:LED digital tube display experiment

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号