首页 > 代码库 > 51单片机实现按键查询并灯闪烁时间实验

51单片机实现按键查询并灯闪烁时间实验

————————————————————————————————————————————

实现功能:

技术分享

功能模块:

  • 初始化变量
  • 主体功能(循环)
    • 按键查询
    • 对应LED状态及延时参数
    • 改变LED状态

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

思路分析:

技术分享

流程分析:

技术分享

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

伪代码分析:

  • 初始化变量:

技术分享

  • 按键查询

技术分享

  • LED初始化

技术分享

  • LED改变

技术分享

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

完整流程:

技术分享

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

代码实现:

  1 #include "reg52.h"
  2 typedef unsigned char uchar;
  3 typedef unsigned int uint;
  4 int KEY_Detect(void);
  5 void Delay(uint m);
  6 void main(void)
  7 {
  8     uchar ledStatus;
  9     uchar curDelay = 0;
 10     uchar maxDelay = 20;
 11     uchar keyStatus = 0xff;
 12     uchar keyData = http://www.mamicode.com/0;
 13     P3 = 0xff;
 14     while(1)
 15     {
 16         keyData =http://www.mamicode.com/ KEY_Detect();
 17         switch (keyData)
 18         {
 19         case 1:
 20             P1 = 0x7f;
 21             ledStatus = 0;
 22             maxDelay = 10;
 23             curDelay = 0;
 24             keyStatus = keyData;
 25             break;
 26         case 2:
 27             P1 = 0xff;
 28             keyStatus = keyData;
 29             if (maxDelay < 250)
 30                 maxDelay += 5;
 31             curDelay = maxDelay;
 32             break;
 33         case 3:
 34             P1 = 0xff;
 35             keyStatus = keyData;
 36             if (maxDelay > 5)
 37                 maxDelay -= 5;
 38             curDelay = maxDelay;
 39             break;
 40         default:
 41             Delay(2000);
 42             break;
 43         }
 44         if(curDelay == maxDelay)
 45         {
 46             switch(keyStatus)
 47             {
 48             case 1:
 49                 P1 >>= 1;
 50                 ledStatus++;
 51                 if (ledStatus == 8)
 52                 {
 53                     ledStatus = 0;
 54                     P1 = 0x7f;
 55                 }
 56                 break;
 57             case 2:
 58             case 3:
 59                 P1 = ~P1;
 60                 break;
 61             default:
 62                 break;
 63             }
 64             curDelay = 0;
 65         }
 66         curDelay ++;
 67     }
 68 }
 69 void Delay(unsigned int m)
 70 {
 71     while(m--);
 72 }
 73 int KEY_Detect(void)
 74 {
 75     uchar ucTemp = P3;
 76     uchar key = 0xff;
 77     if (ucTemp != 0xff)
 78     {
 79         Delay(2000);
 80         ucTemp = P3;
 81         if(ucTemp != 0xff)
 82         {
 83             switch(ucTemp)
 84             {
 85             case 0xFE:
 86                 key = 1;
 87                 break;
 88             case 0xFD:
 89                 key = 2;
 90                 break;
 91             case 0xFB:
 92                 key = 3;
 93                 break;
 94             case 0xF7:
 95                 key = 4;
 96                 break;
 97             case 0xEF:
 98                 key = 5;
 99                 break;
100             case 0xDF:
101                 key = 6;
102                 break;
103             case 0xBF:
104                 key = 7;
105                 break;
106             case 0x7F:
107                 key = 8;
108                 break;
109             default:
110                 break;
111             }
112         }
113     }
114     while(ucTemp != 0xff)
115         ucTemp = P3;
116     return key;
117 }

51单片机实现按键查询并灯闪烁时间实验