标题: 按键数码显示实现 [打印本页]

作者: 去也66    时间: 2020-7-1 19:42
标题: 按键数码显示实现
  1. #include <reg52.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char

  4. //共阳数码管编码表
  5. uchar code table[] =
  6. {0xc0,0xf9,0xa4,0xb0,
  7. 0x99,0x92,0x82,0xf8,
  8. 0x80,0x90,0x88,0x83,
  9. 0xc6,0xa1,0x86,0x8e};

  10. void delay(uint x)
  11. {
  12.         uint i,j;
  13.         for(i = 0; i < x; i++)
  14.         {
  15.                 for(j = 0; j < 110; j++);
  16.         }
  17. }

  18. uchar getKey()
  19. {
  20.         uchar x = 0;
  21.         uchar y = 0;
  22.         uchar result = 0;

  23.         P2 = 0x0f;
  24.         if (P2 != 0x0f)
  25.         {
  26.                 delay(20);
  27.                 switch(P2)
  28.                 {
  29.                         case 0x0e: x = 0; break;
  30.                         case 0x0d: x = 1; break;
  31.                         case 0x0b: x = 2; break;
  32.                         case 0x07: x = 3; break;
  33.                         default: break;
  34.                 }
  35.                 delay(10);

  36.                 P2 = 0xf0;
  37.                 if (P2 != 0xf0)
  38.                 {
  39.                         delay(20);
  40.                         switch(P2)
  41.                         {
  42.                                 case 0xe0: y = 0; break;
  43.                                 case 0xd0: y = 1; break;
  44.                                 case 0xb0: y = 2; break;
  45.                                 case 0x70: y = 3; break;
  46.                                 default: break;
  47.                         }
  48.                 }
  49.         }

  50.         return x*4 + y;
  51. }

  52. void display(uchar num)
  53. {
  54.         P0 = table[num];
  55. }

  56. void main()
  57. {                                               
  58.         uchar keyValue;
  59.         while(1)
  60.         {
  61.                 keyValue = getKey();
  62.                 display(keyValue);
  63.         }
  64. }
  65.        
复制代码


{B66523C3-535F-41E3-A5B8-8798D7981DBA}.png.jpg (219.68 KB, 下载次数: 36)

{B66523C3-535F-41E3-A5B8-8798D7981DBA}.png.jpg





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1