单片机-4个独立按键的控制程序

时间:2024-03-04 08:46:47
  1 #include "8051.h"
  2 typedef unsigned char u8;
  3 typedef unsigned int u16;
  4 u8 smgduan[]= {
  5 /*0        1      2        3      4        5      6        7 */
  6 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
  7 /*8        9     A        B     C        D     E        F */
  8 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
  9 
 10 // P0口为数码管的位选的8位输入引脚
 11 // P0 = 0x00;
 12 
 13 
 14 void Delayms(u16 ms);
 15 void shumaguan(u8 n);
 16 void DigDisplay();
 17 
 18 void main()
 19 {
 20     while (1)
 21     {
 22         /*
 23         *   不推荐此做法
 24         *   此做法占用内存高
 25         */
 26         if (!K1)
 27         {
 28             // 软件延时消除抖动
 29             Delayms(10);
 30             if (!K1)
 31             {
 32                 LED_1 = !LED_1;
 33             }
 34             // 确认按键已经按下
 35             while(!K1);
 36         }
 37         if (!K2)
 38         {
 39             // 软件延时消除抖动
 40             Delayms(10);
 41             if (!K2)
 42             {
 43                 LED_2 = !LED_2;
 44             }
 45             while(!K2);
 46         }
 47         if (!K3)
 48         {
 49             // 软件延时消除抖动
 50             Delayms(10);
 51             if (!K3)
 52             {
 53                 LED_3 = !LED_3;
 54             }
 55             while(!K3);
 56         }
 57         if (!K4)
 58         {
 59             // 软件延时消除抖动
 60             Delayms(10);
 61             if (!K4)
 62             {
 63                 LED_4 = !LED_4;
 64             }
 65             while(!K4);
 66         }
 67     }
 68 }
 69 
 70 
 71 void Delayms(u16 ms)
 72 {
 73     unsigned int i, j;
 74     for(i = ms; i > 0; i--)
 75         for(j = 110; j > 0; j--);
 76     return;
 77 }
 78 
 79 void shumaguan(u8 n)
 80 {
 81     switch(n)
 82     {
 83         case 0:
 84             LSA = 0;LSB = 0;LSC = 0;break;
 85         case 1:
 86             LSA = 1;LSB = 0;LSC = 0;break;
 87         case 2:
 88             LSA = 0;LSB = 1;LSC = 0;break;
 89         case 3:
 90             LSA = 1;LSB = 1;LSC = 0;break;
 91         case 4:
 92             LSA = 0;LSB = 0;LSC = 1;break;
 93         case 5:
 94             LSA = 1;LSB = 0;LSC = 1;break;
 95         case 6:
 96             LSA = 0;LSB = 1;LSC = 1;break;
 97         case 7:
 98             LSA = 1;LSB = 1;LSC = 1;break;
 99     }
100 }
101 
102 void DigDisplay()
103 {
104     u8 i1 = 0;
105     u8 i2 = 0;
106     u8 i3 = 0;
107     u8 i4 = 0;
108     u8 i5 = 0;
109     u8 i6 = 0;
110     u8 i7 = 0;
111     u8 i8 = 0;
112     
113 
114 
115 
116     for (i8 = 0; i8 < 10; i8++)
117         for (i7 = 0; i7 < 10; i7++)
118             for (i6 = 0; i6 < 10; i7++)
119                 for (i5 = 0; i5 < 10; i5++)
120                     for (i4 = 0; i4 < 10; i4++)
121                         for (i3 = 0; i3 < 10; i3++)
122                             for (i2 = 0; i2 < 10; i2++)
123                                 for (i1 = 0; i1 < 10; i1++)
124                                 {
125                                     u16 cnt = 10;
126                                     while (cnt--)
127                                     {
128                                         shumaguan(0);       //选中第一个数码管
129                                         P0 = smgduan[i1];   //给他送一个数字
130                                         Delayms(1);         //稍微延时一下下
131                                         shumaguan(1);       //然后切换到第二个数码管。。。如此进行一直到最后一个数码管。。。最后再改变送入数码管的数值。。。再来一遍
132                                         P0 = smgduan[i2];
133                                         Delayms(1);
134                                         shumaguan(2);
135                                         P0 = smgduan[i3];
136                                         Delayms(1);
137                                         shumaguan(3);
138                                         P0 = smgduan[i4];
139                                         Delayms(1);
140                                         shumaguan(4);
141                                         P0 = smgduan[i5];
142                                         Delayms(1);
143                                         shumaguan(5);
144                                         P0 = smgduan[i6];
145                                         Delayms(1);
146                                         shumaguan(6);
147                                         P0 = smgduan[i7];
148                                         Delayms(1);
149                                         shumaguan(7);
150                                         P0 = smgduan[i8];
151                                         Delayms(1);
152                                         P0 = 0x00;
153                                     }
154                                 }
155 }