3 common IOs recognize 22 buttons test

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

Learning from the experience of predecessors, the problem of too many diodes was optimized. At present, 6 keys can be connected without diodes, 12 keys can be connected with 2 diodes, 18 keys can be connected with 6 diodes, and 21 keys can be connected with 9 diodes. The 22nd key should occupy 3 diodes alone, which is the least cost-effective.

The experiment uses 89S51 for the test. The circuit wiring is P1.2, P1.3, P1.4 connected to the keyboard, and P1.0 connected to the display.





(Original file name: GIF.gif) 





(Original file name SC00015.JPG) 





(Original file name SC00016.jpg) 








/*==========================================================================*

* 3 IO connection recognition 22 keys test program *

* ------------------------------------------------ *

* MCU: AT89C2051 *

* OSC: 12M cysytel *

* Program design: Cowboy *

* Program version: V1.0 *

*========================================================================================*/



#include



//================== IO port line connection==================

sbit Bus = P1^0;

sbit IO_a = P1^4;

sbit IO_b = P1^3;

sbit IO_c = P1^2;

  

//==================== Variable declaration====================

unsigned char Disp_buf[3];

unsigned char Di g;

unsigned char Key_count;

unsigned char bdata Key_state;    

sbit KB0 = Key_state^0;

sbit KB1 = Key_state^1;

sbit KB2 = Key_state^2;

sbit KB3 = Key_state^3;

sbit KB4 = Key_state^4;

sbit KB5 = Key_state^5;



//================= Table data====================

code unsigned char LED_font[24]=

{

        0x84,0x9f,0xa2,0x8a,0x99,0xc8,0xc0,0x9e,0x80, //012345678

        0x88,0x90,0xc1,0xe4,0x83,0xe0,0xf0,0xff,0xfb, //9abcdef -

};



code unsigned char Key_tab[64]= //Key code mapping table

{// 0 1 2 3 4 5 6 7 8 9   

        22, 0, 2, 0, 0, 0, 0, 0, 0, 0, //4X 0

         , 5, 0, 0, 0, 0, 0, 0, //5X

         16,12

        , 7, 0, 0, 0, //6X 0, 0, 0, 0, 0, 0, 0,

         0, //7X 0, 0, 0, 0, 0, 0, 0, 0,         0

         ,     // 8X 0 ,    ... i;     Key_count --; //Scanning order     Key_count &= 3;     switch (Key_count) //Process in order     {         case 2: //First round of scanning         KB0 = IO_b;          KB1 = IO_c;          IO_a = 1;         IO_b = 0;         break;         case 1: //Every second round of scanning



































    



        KB2 = IO_c;

        KB3 = IO_a;

        IO_b = 1;

        IO_c = 0;

        break;

    

        case 0: //Scan every three rounds

        KB4 = IO_a;

        KB5 = IO_b;

        IO_c = 1;

        break;

    

        default: //Scan every four rounds

        if (!IO_a) KB0 = 0;

        if (!IO_b) KB2 = 0;

        if (!IO_c) KB4 = 0;

        IO_a = 0;



        //======Update display buffer=======

        i = Key_tab[Key_state];

        if (i == 0)

        {

            Disp_buf[2] = 0x11; //Display three horizontal lines

            Disp_buf[1] = 0x11;

            Disp_buf[0] = 0x11;

        }

        else

        {

            Disp_buf[2] = 0x0c; //Character "C"

            Disp_buf[1] = i / 10; //ten digits of key code

            Disp_buf[0] = B; //one digit of key code                         }       

        Key_state     =

        0     ;

    }

}     /



    

* = ...     (!Dat) Bus = 0;     else     {         Bus = 0;         Bus = 1;     }     while(--i); //Delay 8us         Bus = 1; }     //=============== Bus driver ================= void Bus_drive() {     unsigned char i = 0;     unsigned char Sdat;     Send_bit(1); //Bit6 blanking     do Bus = 1; while(--i); //Delay 768us     do Bus = 0; while(--i); //Delay 768us     Bus = 1;     Sdat = LED_font[Disp_buf[Dig++]]; //Get display data     Send_bit(Sdat & 0x01); //Send bit 0             Send_bit(Sdat & 0x02); //Send bit 1             Send_bit(Sdat & 0x04); //Send bit 2             Send_bit(Sdat & 0x08); //Send bit 3             Send_bit(Sdat & 0x10); //Send bit 4             Send_bit(Sdat & 0x20); //Send bit 5             Send_bit(Dig & 0x01); //Send bit select 1             Send_bit(Dig & 0x02); //Send bit select 2     while(--i); //Delay 512us     Send_bit(Sdat & 0x40); //Send bit 6     for (i = 7;i> 0;i--) Send_bit(1); //Move bit 6 to Dout     if (Dig == 3) Dig = 0; }     

















































































        

/ * =        

                    ...       ​        ​        ​        ​    ​​     /                         *        = ...​    ​    ​    ​    ​        ​         ​        ​        ​










































Reference address:3 common IOs recognize 22 buttons test

Previous article:3 IO ports scan 16 keys
Next article:C51 interrupts and using interrupt priorities

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号