• <li id="00i08"><input id="00i08"></input></li>
  • <sup id="00i08"><tbody id="00i08"></tbody></sup>
    <abbr id="00i08"></abbr>
  • 新聞中心

    EEPW首頁 > 嵌入式系統 > 設計應用 > 3個普通IO識別22個按鍵試驗

    3個普通IO識別22個按鍵試驗

    作者: 時間:2016-12-14 來源:網絡 收藏

      吸取各位前輩的經驗,將之前二極管用量多的問題優化一下,目前不用二極管能接6鍵,2只二極管能接12鍵,6只二極管能接18鍵,9只二極管能接21鍵,第22鍵要單獨占用3只二極管最不化算。

    本文引用地址:http://www.czjhyjcfj.com/article/201612/341598.htm

      實驗用89S51作試驗,電路接線就是P1.2,P1.3,P1.4接鍵盤,P1.0接顯示器。

        

     

      (原文件名:GIF.gif)

     

      (原文件名SC00015.JPG)  

     

     

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

      * 3個接識別22鍵測試程序 *

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

      * MCU: AT89C2051 *

      * OSC: 12M cysytel *

      * 程序設計:Cowboy *

      * 程序版本:V1.0 *

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

      #include

      //================== 口線連接 ==================

      sbit Bus = P1^0;

      sbit _a = P1^4;

      sbit IO_b = P1^3;

      sbit IO_c = P1^2;

      //================== 變量聲明 ====================

      unsigned char Disp_buf[3];

      unsigned char Dig;

      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;

      //================== 表格數據 ====================

      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]= //鍵碼映射表

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

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

      0, 0, 0, 0, 0,18, 0, 0, 0, 0, //1X

      0, 0, 0, 0, 0, 0, 3,14, 0, 0, //2X

      20,10, 6, 0, 0, 0, 0, 0, 1,19, //3X

      0, 5, 0, 0, 0,15, 0,11, 0, 0, //4X

      0,17, 0, 0,13, 8, 0,21, 0, 9, //5X

      16,12, 7, 0 //6X

      };

      //=============== 檢測 =================

      void Key_scan()

      {

      unsigned char i;

      Key_count --; //掃描次序

      Key_count &= 3;

      switch (Key_count) //按次序處理

      {

      case 2: //第一輪掃描

      KB0 = IO_b;

      KB1 = IO_c;

      IO_a = 1;

      IO_b = 0;

      break;

      case 1: //每二輪掃描

      KB2 = IO_c;

      KB3 = IO_a;

      IO_b = 1;

      IO_c = 0;

      break;

      case 0: //每三輪掃描

      KB4 = IO_a;

      KB5 = IO_b;

      IO_c = 1;

      break;

      default: //每四輪掃描

      if (!IO_a) KB0 = 0;

      if (!IO_b) KB2 = 0;

      if (!IO_c) KB4 = 0;

      IO_a = 0;

      //======更新顯示緩沖區=======

      i = Key_tab[Key_state];

      if (i == 0)

      {

      Disp_buf[2] = 0x11; //顯示三橫

      Disp_buf[1] = 0x11;

      Disp_buf[0] = 0x11;

      }

      else

      {

      Disp_buf[2] = 0x0c; //字符"C"

      Disp_buf[1] = i / 10; //鍵碼十位

      Disp_buf[0] = B;于 //鍵碼個位

      }

      Key_state = 0;

      }

      }

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

      ONE WIRE 顯示總線驅動程序

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

      //=============== 發送一位 =================

      void Send_bit(bit Dat)

      {

      unsigned char i = 3;

      if (!Dat) Bus = 0;

      else

      {

      Bus = 0;

      Bus = 1;

      }

      while(--i); //延時8us

      Bus = 1;

      }

      //=============== 總線驅動 =================

      void Bus_drive()

      {

      unsigned char i = 0;

      unsigned char Sdat;

      Send_bit(1); //Bit6消隱

      do Bus = 1; while(--i); //延時768us

      do Bus = 0; while(--i); //延時768us

      Bus = 1;

      Sdat = LED_font[Disp_buf[Dig++]]; //獲取顯示數據

      Send_bit(Sdat & 0x01); //發送位0

      Send_bit(Sdat & 0x02); //發送位1

      Send_bit(Sdat & 0x04); //發送位2

      Send_bit(Sdat & 0x08); //發送位3

      Send_bit(Sdat & 0x10); //發送位4

      Send_bit(Sdat & 0x20); //發送位5

      Send_bit(Dig & 0x01); //發送位選1

      Send_bit(Dig & 0x02); //發送位選2

      while(--i); //延時512us

      Send_bit(Sdat & 0x40); //發送位6

      for (i = 7;i> 0;i--) Send_bit(1); //位6移至Dout

      if (Dig == 3) Dig = 0;

      }

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

      延時 5ms 程序

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

      void Delay_5ms()

      {

      while(!TF1);

      TF1 = 0;

      TH1 = (- 5000) / 256;

      TL1 = (- 5000) % 256;

      }

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

      主程序

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

      void main()

      {

      TMOD = 0x10; //定時器1,16位模式

      TCON = 0xc0; //TR1=1;TF1=1;

      while(1) //主循環

      {

      Bus_drive(); //顯示總線驅動

      Key_scan(); //檢測

      Delay_5ms(); //延時5MS

      }



    關鍵詞: IO 按鍵

    評論


    相關推薦

    技術專區

    關閉
    主站蜘蛛池模板: 龙岩市| 屯昌县| 芮城县| 乐昌市| 临沧市| 大足县| 连江县| 夏河县| 故城县| 上栗县| 昌宁县| 长乐市| 宝应县| 新源县| 周至县| 桑植县| 泗阳县| 海晏县| 儋州市| 石台县| 九龙县| 临泽县| 平谷区| 尚义县| 凉城县| 海淀区| 新郑市| 福鼎市| 太康县| 方正县| 微山县| 河曲县| 喀什市| 民权县| 平乡县| 荥阳市| 靖西县| 八宿县| 皮山县| 安西县| 通城县|