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

Posted Hugh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 0;
 13     P3 = 0xff;
 14     while(1)
 15     {
 16         keyData = 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单片机实现按键查询并灯闪烁时间实验的主要内容,如果未能解决你的问题,请参考以下文章

单片机原理及其应用——单片机控制8只发光二极管交替闪烁

单片机原理及其应用——单片机控制8只发光二极管交替闪烁

江科大51单片机入门学习笔记合集

STM32怎么实现单片机控制LED灯常亮10S后熄灭?

基于51单片机的按键计数设计,4位,0到9999

基于51单片机的电子秒表