动态扫描显示.c
上传用户:schina
上传日期:2021-05-07
资源大小:39k
文件大小:3k
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define KEY_PORT P1
- #define dat P0
- sbit w1=P2^0;
- sbit w2=P2^1;
- sbit w3=P2^2;
- sbit w4=P2^3;
- uchar num,count,temp;
- uint t;
- uchar code table[]={ //共阴数码管码表
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71
- };
- //延时函数
- void delay(uint z)
- {
- uint x;
- uchar y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- //键盘扫描
- void keyscan()
- {
- KEY_PORT=0xfe; //扫描第一行
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
- {
- delay(5) ;
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- switch(temp)
- {
- case 0xee : num=0;
- break;
- case 0xde : num=1;
- break;
- case 0xbe : num=2;
- break;
- case 0x7e : num=3;//对键赋值,定义用户想要的值
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- temp=temp&0xf0; //松手检测,对temp赋新值,使跳出扫描循环
- }
-
- }
- }
- KEY_PORT=0xfd; //扫描第二行
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
- {
- delay(5) ;
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- switch(temp)
- {
- case 0xed : num=4;
- break;
- case 0xdd : num=5;
- break;
- case 0xbd : num=6;
- break;
- case 0x7d : num=7; //对键赋值,定义用户想要的值
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- temp=temp&0xf0; //松手检测,对temp赋新值,使跳出扫描循环
- }
-
- }
- }
- KEY_PORT=0xfb; //扫描第三行
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
- {
- delay(5) ;
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- switch(temp)
- {
- case 0xeb : num=8;
- break;
- case 0xdb : num=9;
- break;
- case 0xbb : num=10;
- break;
- case 0x7b : num=11; //对键赋值,定义用户想要的值
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- temp=temp&0xf0; //松手检测,对temp赋新值,使跳出扫描循环
- }
-
- }
- }
- KEY_PORT=0xf7; //扫描第四行
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
- {
- delay(5) ;
- temp=KEY_PORT;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- switch(temp)
- {
- case 0xe7 : num=12;
- break;
- case 0xd7 : num=13;
- break;
- case 0xb7 : num=14;
- break;
- case 0x77 : num=15; //对键赋值,定义用户想要的值
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY_PORT;
- temp=temp&0xf0; //松手检测,对temp赋新值,使跳出扫描循环
- }
-
- }
- }
- }
- //计数显示函数
- void display(uchar val)
- {
- w1=0;
- dat=table[val];
- delay(1);
- w1=1;
- }
- //按键显示函数
- void display2(uchar val)
- {
- w3=0;
- dat=table[val/10];
- delay(1);
- w3=1;
- w4=0;
- dat=table[val%10];
- delay(1);
- w4=1;
- }
- //主函数
- void main()
- {
- num=0;
- t=0;
- count=0;
- P2=0xff;
- TMOD=0x02;
- TH0=6;
- TL0=6;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- {
- keyscan();
- display(num);
- display2(count);
- }
- }
- //计时中断函数
- void t0() interrupt 1
- {
- t++;
- if(t==4000)
- {
- t=0;
- count++;
- if(count==100)
- count=0;
- }
- }