仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #include "delay.h"
- #include "seg.h"
- #include "key.h"
- main()
- {
- while(1)
- {
- seg(1, (key() % 10));
- seg(2, (key() / 10));
- }
- }
复制代码- #include <reg51.h>
- #include "delay.h"
- unsigned char SEG[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
- void seg(unsigned char i,unsigned char z)
- {
- switch (i)
- {
- case 1:P3=0x01;break;
- case 2:P3=0x02;break;
- }
- P1=SEG[z];
- //delay(2);
- //P1=0xff;
- }
复制代码- #include <reg51.h>
- #include "delay.h"
- sbit key1=P2^4;
- sbit key2=P2^5;
- sbit key3=P2^6;
- sbit key4=P2^7;
- int key()
- {
- unsigned char kk,i;
- for(i=0;i<4;i++)
- {
- P2=~(0x01<<i);
- delay(2);
- if(key1==0){delay(2);while(key1==0);delay(2);kk=1+i*4;}
- if(key2==0){delay(2);while(key2==0);delay(2);kk=2+i*4;}
- if(key3==0){delay(2);while(key3==0);delay(2);kk=3+i*4;}
- if(key4==0){delay(2);while(key4==0);delay(2);kk=4+i*4;}
- }
- return kk;
- }
复制代码
Keil代码与Proteus8.13仿真下载:
8x8按键扫描.zip
(72.07 KB, 下载次数: 15)
|