|
注意:上图和c文件均为共阳极的七段数码管
单片机源程序如下:
- #include<reg51.h>
- sbit P24=P2^4;
- sbit P25=P2^5;
- sbit P26=P2^6;
- sbit P27=P2^7;
- unsigned char code Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
- unsigned char keyval;
- void led_delay(void)
- {
- unsigned int j;
- for(j=0;j<200;j++)
- ;
- }
- void display(unsigned char k)
- {
- P0=Tab[k/1];
- led_delay();
- }
- void delay30ms(void)
- {
- unsigned char i,j;
- for(i=0;i<100;i++)
- for(j=0;j<100;j++)
- ;
- }
- void main(void)
- {
- EA=1;
- ET0=1;
- TMOD=0x01;
- TH0=(65536-500)/256;
- TL0=(65536-500)%256;
- TR0=1;
- keyval=0x00;
- while(1)
- {
- display(keyval);
- }
- }
- void time0_interserve(void) interrupt 1 using 1
- {
- TR0=0;
- P2=0xf0;
- if((P2&0xf0)!=0xf0)
- delay30ms();
- if((P2&0xf0)!=0xf0)
- {
- P2=0xfe;
- if(P24==0)
- keyval=0;
- if(P25==0)
- keyval=1;
- if(P26==0)
- keyval=2;
- if(P27==0)
- keyval=3;
- P2=0xfd;
- if(P24==0)
- keyval=4;
- if(P25==0)
- keyval=5;
- if(P26==0)
- keyval=6;
- if(P27==0)
- keyval=7;
- P2=0xfb;
- if(P24==0)
- keyval=8;
- if(P25==0)
- keyval=9;
- if(P26==0)
- keyval=10;
- if(P27==0)
- keyval=11;
- P2=0xf7;
- if(P24==0)
- keyval=12;
- if(P25==0)
- keyval=13;
- if(P26==0)
- keyval=14;
- if(P27==0)
- keyval=15;
- }
- TR0=1;
- TH0=(65536-500)/256;
- TL0=(65536-500)%256;
- }
复制代码
全部资料51hei下载地址:
4x4矩阵式键盘.zip
(16.94 KB, 下载次数: 43)
|
|