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 void KeyTest(); 18 void smg(u8 n, u8 m); 19 void key_4x4(); 20 21 void main() 22 { 23 while(1) 24 { 25 key_4x4(); 26 } 27 } 28 29 // 不精確的延時函數 30 void Delayms(u16 ms) 31 { 32 unsigned int i, j; 33 for(i = ms; i > 0; i--) 34 for(j = 110; j > 0; j--); 35 return; 36 } 37 38 // 數碼管根據74HC138譯碼器選擇對應的段(選擇哪個數碼管顯示) 39 void shumaguan(u8 n) 40 { 41 switch(n) 42 { 43 case 0: 44 LSA = 0;LSB = 0;LSC = 0;break; 45 case 1: 46 LSA = 1;LSB = 0;LSC = 0;break; 47 case 2: 48 LSA = 0;LSB = 1;LSC = 0;break; 49 case 3: 50 LSA = 1;LSB = 1;LSC = 0;break; 51 case 4: 52 LSA = 0;LSB = 0;LSC = 1;break; 53 case 5: 54 LSA = 1;LSB = 0;LSC = 1;break; 55 case 6: 56 LSA = 0;LSB = 1;LSC = 1;break; 57 case 7: 58 LSA = 1;LSB = 1;LSC = 1;break; 59 } 60 } 61 62 // 數碼管顯示數字,並以十進制遞增 63 void DigDisplay() 64 { 65 u8 i1 = 0; 66 u8 i2 = 0; 67 u8 i3 = 0; 68 u8 i4 = 0; 69 u8 i5 = 0; 70 u8 i6 = 0; 71 u8 i7 = 0; 72 u8 i8 = 0; 73 74 75 76 77 for (i8 = 0; i8 < 10; i8++) 78 for (i7 = 0; i7 < 10; i7++) 79 for (i6 = 0; i6 < 10; i7++) 80 for (i5 = 0; i5 < 10; i5++) 81 for (i4 = 0; i4 < 10; i4++) 82 for (i3 = 0; i3 < 10; i3++) 83 for (i2 = 0; i2 < 10; i2++) 84 for (i1 = 0; i1 < 10; i1++) 85 { 86 u16 cnt = 10; 87 while (cnt--) 88 { 89 shumaguan(0); //選中第一個數碼管 90 P0 = smgduan[i1]; //給他送一個數字 91 Delayms(1); //稍微延時一下下 92 shumaguan(1); //然后切換到第二個數碼管。。。如此進行一直到最后一個數碼管。。。最后再改變送入數碼管的數值。。。再來一遍 93 P0 = smgduan[i2]; 94 Delayms(1); 95 shumaguan(2); 96 P0 = smgduan[i3]; 97 Delayms(1); 98 shumaguan(3); 99 P0 = smgduan[i4]; 100 Delayms(1); 101 shumaguan(4); 102 P0 = smgduan[i5]; 103 Delayms(1); 104 shumaguan(5); 105 P0 = smgduan[i6]; 106 Delayms(1); 107 shumaguan(6); 108 P0 = smgduan[i7]; 109 Delayms(1); 110 shumaguan(7); 111 P0 = smgduan[i8]; 112 Delayms(1); 113 P0 = 0x00; 114 } 115 } 116 } 117 118 // 測試獨立按鍵功能 119 void KeyTest() 120 { 121 while (1) 122 { 123 /* 124 * 不推薦此做法 125 * 此做法占用內存高 126 */ 127 if (!K1) 128 { 129 // 軟件延時消除抖動 130 Delayms(10); 131 if (!K1) 132 { 133 LED_1 = !LED_1; 134 } 135 // 確認按鍵已經按下 136 while(!K1); 137 } 138 if (!K2) 139 { 140 // 軟件延時消除抖動 141 Delayms(10); 142 if (!K2) 143 { 144 LED_2 = !LED_2; 145 } 146 while(!K2); 147 } 148 if (!K3) 149 { 150 // 軟件延時消除抖動 151 Delayms(10); 152 if (!K3) 153 { 154 LED_3 = !LED_3; 155 } 156 while(!K3); 157 } 158 if (!K4) 159 { 160 // 軟件延時消除抖動 161 Delayms(10); 162 if (!K4) 163 { 164 LED_4 = !LED_4; 165 } 166 while(!K4); 167 } 168 } 169 } 170 171 // 給指定的數碼管送入指定的數字 172 void smg(u8 n, u8 m) 173 { 174 if (1==n) 175 { 176 P0 = 0x00; 177 shumaguan(0); //選中第一個數碼管 178 P0 = smgduan[m]; //給他送一個數字 179 Delayms(10); 180 } 181 if (2==n) 182 { 183 P0 = 0x00; 184 shumaguan(1); //選中第一個數碼管 185 P0 = smgduan[m]; //給他送一個數字 186 Delayms(10); 187 } 188 if (3==n) 189 { 190 P0 = 0x00; 191 shumaguan(2); //選中第一個數碼管 192 P0 = smgduan[m]; //給他送一個數字 193 Delayms(10); 194 } 195 if (4==n) 196 { 197 P0 = 0x00; 198 shumaguan(3); //選中第一個數碼管 199 P0 = smgduan[m]; //給他送一個數字 200 Delayms(10); 201 } 202 if (5==n) 203 { 204 P0 = 0x00; 205 shumaguan(4); //選中第一個數碼管 206 P0 = smgduan[m]; //給他送一個數字 207 Delayms(10); 208 } 209 if (6==n) 210 { 211 P0 = 0x00; 212 shumaguan(5); //選中第一個數碼管 213 P0 = smgduan[m]; //給他送一個數字 214 Delayms(10); 215 } 216 if (7==n) 217 { 218 P0 = 0x00; 219 shumaguan(6); //選中第一個數碼管 220 P0 = smgduan[m]; //給他送一個數字 221 Delayms(10); 222 } 223 if (8==n) 224 { 225 P0 = 0x00; 226 shumaguan(7); //選中第一個數碼管 227 P0 = smgduan[m]; //給他送一個數字 228 Delayms(10); 229 } 230 } 231 232 // 可以定義按鍵按下的事件 233 void key_4x4() 234 { 235 // 每次選中其中一根地線的時候要消除其他地線的狀態,不然會混亂 236 // 此處選定最上一列為低電平(使能) 237 P1_7 = 0; 238 P1_6 = 1; 239 P1_5 = 1; 240 P1_4 = 1; 241 if (!P1_3) 242 { 243 // 軟件延時消除抖動 244 Delayms(10); 245 if (!P1_3) 246 { 247 smg(1,1); 248 } 249 // 確認按鍵已經按下 250 while(!P1_3); 251 } 252 if (!P1_2) 253 { 254 // 軟件延時消除抖動 255 Delayms(10); 256 if (!P1_2) 257 { 258 smg(1,2); 259 } 260 // 確認按鍵已經按下 261 while(!P1_2); 262 } 263 if (!P1_1) 264 { 265 // 軟件延時消除抖動 266 Delayms(10); 267 if (!P1_1) 268 { 269 smg(1,3); 270 } 271 // 確認按鍵已經按下 272 while(!P1_1); 273 } 274 if (!P1_0) 275 { 276 // 軟件延時消除抖動 277 Delayms(10); 278 if (!P1_0) 279 { 280 ; 281 } 282 // 確認按鍵已經按下 283 while(!P1_0); 284 } 285 286 P1_7 = 1; 287 P1_6 = 0; 288 P1_5 = 1; 289 P1_4 = 1; 290 if (!P1_3) 291 { 292 // 軟件延時消除抖動 293 Delayms(10); 294 if (!P1_3) 295 { 296 smg(1,4); 297 } 298 // 確認按鍵已經按下 299 while(!P1_3); 300 } 301 if (!P1_2) 302 { 303 // 軟件延時消除抖動 304 Delayms(10); 305 if (!P1_2) 306 { 307 smg(1,5); 308 } 309 // 確認按鍵已經按下 310 while(!P1_2); 311 } 312 if (!P1_1) 313 { 314 // 軟件延時消除抖動 315 Delayms(10); 316 if (!P1_1) 317 { 318 smg(1,6); 319 } 320 // 確認按鍵已經按下 321 while(!P1_1); 322 } 323 if (!P1_0) 324 { 325 // 軟件延時消除抖動 326 Delayms(10); 327 if (!P1_0) 328 { 329 ; 330 } 331 // 確認按鍵已經按下 332 while(!P1_0); 333 } 334 P1_7 = 1; 335 P1_6 = 1; 336 P1_5 = 0; 337 P1_4 = 1; 338 if (!P1_3) 339 { 340 // 軟件延時消除抖動 341 Delayms(10); 342 if (!P1_3) 343 { 344 smg(1,7); 345 } 346 // 確認按鍵已經按下 347 while(!P1_3); 348 } 349 if (!P1_2) 350 { 351 // 軟件延時消除抖動 352 Delayms(10); 353 if (!P1_2) 354 { 355 smg(1,8); 356 } 357 // 確認按鍵已經按下 358 while(!P1_2); 359 } 360 if (!P1_1) 361 { 362 // 軟件延時消除抖動 363 Delayms(10); 364 if (!P1_1) 365 { 366 smg(1,9); 367 } 368 // 確認按鍵已經按下 369 while(!P1_1); 370 } 371 if (!P1_0) 372 { 373 // 軟件延時消除抖動 374 Delayms(10); 375 if (!P1_0) 376 { 377 ; 378 } 379 // 確認按鍵已經按下 380 while(!P1_0); 381 } 382 P1_7 = 1; 383 P1_6 = 1; 384 P1_5 = 1; 385 P1_4 = 0; 386 if (!P1_3) 387 { 388 // 軟件延時消除抖動 389 Delayms(10); 390 if (!P1_3) 391 { 392 smg(1,1); 393 } 394 // 確認按鍵已經按下 395 while(!P1_3); 396 } 397 if (!P1_2) 398 { 399 // 軟件延時消除抖動 400 Delayms(10); 401 if (!P1_2) 402 { 403 smg(1,2); 404 } 405 // 確認按鍵已經按下 406 while(!P1_2); 407 } 408 if (!P1_1) 409 { 410 // 軟件延時消除抖動 411 Delayms(10); 412 if (!P1_1) 413 { 414 smg(1,3); 415 } 416 // 確認按鍵已經按下 417 while(!P1_1); 418 } 419 if (!P1_0) 420 { 421 // 軟件延時消除抖動 422 Delayms(10); 423 if (!P1_0) 424 { 425 ; 426 } 427 // 確認按鍵已經按下 428 while(!P1_0); 429 } 430 }