51单片机使用数码管和矩阵键盘一起用,每按矩阵键盘输出在动态数码管。
单片机源程序如下:
- #include<reg52.h>
- typedef unsigned char uint8;
- typedef unsigned int uint16;
- sbit a=P2^2;
- sbit b=P2^3;
- sbit c=P2^4;
- uint8 smg[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
- 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
- uint8 sum[8];
- uint8 m,button;
- void delay(uint16 i) //10ms
- {
- uint8 j,k;
- for(;i>0;i--)
- for(j=38;j>0;j--)
- for(k=130;k>0;k--);
- }
- void delay1(uint8 i) //1.3ms
- {
- uint8 j,k;
- for(;i>0;i--)
- for(j=10;j>0;j--)
- for(k=130;k>0;k--);
- }
- void keyscan()
- {
- uint8 value,h,l;
- P1=0x0f;
- l=P1&0x0f;
- if(l!=0x0f)
- {
- delay(1);
- if(l!=0x0f)
- {
- l=P1&0x0f;
- l=l|0xf0;
- P1=l;
- h=P1&0xf0;
- l=l&0x0f;
- value=h+l;
- button=1; //?????
- delay(50); //??????????,????????????????
- switch(value)
- {
- case 0xe7: m=0;break;
- case 0xeb: m=1;break;
- case 0xed: m=2;break;
- case 0xee: m=3;break;
- case 0xd7: m=4;break;
- case 0xdb: m=5;break;
- case 0xdd: m=6;break;
- case 0xde: m=7;break;
- case 0xb7: m=8;break;
- case 0xbb: m=9;break;
- case 0xbd: m=10;break;
- case 0xbe: m=11;break;
- case 0x77: m=12;break;
- case 0x7b: m=13;break;
- case 0x7d: m=14;break;
- case 0x7e: m=15;break;
- default : break;
- }
- }
- }
- }
- void display() //?????????
- {
- uint8 i;
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case 0: a=1,b=1,c=1;break;
- case 1: a=0,b=1,c=1;break;
- case 2: a=1,b=0,c=1;break;
- case 3: a=0,b=0,c=1;break;
- case 4: a=1,b=1,c=0;break;
- case 5: a=0,b=1,c=0;break;
- case 6: a=1,b=0,c=0;break;
- case 7: a=0,b=0,c=0;break;
- }
- P0=sum[i];;
- delay1(1);
- P0=0x00;
- }
- }
- /*
- void display() //?????????
- {
- uint8 i;
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case 0: a=0,b=0,c=0;break;
- case 1: a=1,b=0,c=0;break;
- case 2: a=0,b=1,c=0;break;
- case 3: a=1,b=1,c=0;break;
- case 4: a=0,b=0,c=1;break;
- case 5: a=1,b=0,c=1;break;
- case 6: a=0,b=1,c=1;break;
- case 7: a=1,b=1,c=1;break;
- }
- P0=sum[i];;
- delay1(1);
- P0=0x00;
- }
- }
- */
- void main()
- {
- button=0;
- while(1)
- {
- keyscan();
- if(button==1)
- {
- sum[7]=sum[6];
- sum[6]=sum[5];
- sum[5]=sum[4];
- sum[4]=sum[3];
- sum[3]=sum[2];
- sum[2]=sum[1];
- sum[1]=sum[0];
- sum[0]=smg[m];
- button=0;
- }
- display();
- }
- }
复制代码
|