|
这是单片机实验,在这里我想用全局变量count来控制显示,但是一次按下后就被判定为按下了多次,而且行扫描停止了,大佬求教,代码还是电路哪里有问题,电路图用的是proteus。真心求教,看了一下午还是没找出问题。
单片机源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- sbit P2_1=P2^1;
- sbit P2_0=P2^0;
- unsigned char discontrol[]={0xfe,0xfd,0xfb,0xf7};
- unsigned int buffer[]={-1,-1,-1,-1};
- unsigned code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- int count=0;
- void Delay(unsigned int x){
- unsigned char t;
- while(x--){
- for(t=0;t<60;t++);
- }
-
- }
- unsigned char Keyscan(void){
- int i,tmp,num=16;
- unsigned char cflag=1;//cycling flag
- for(i=0;i<4;i++){
- if(!cflag)
- break;
- P1=_crol_(0xfe,i);
- tmp=P1;
- tmp=tmp&0xf0;
- if(tmp!=0xf0){
- Delay(50);//prevent keyboard quiver
- tmp=P1;
- tmp=tmp&0xf0;
- if(tmp!=0xf0){//check angin
- tmp=P1;
- switch(tmp){
- case 0xee:num=0;break;
- case 0xde:num=1;break;
- case 0xbe:num=2;break;
- case 0x7e:num=3;break;
- case 0xed:num=4;break;
- case 0xdd:num=5;break;
- case 0xbd:num=6;break;
- case 0x7d:num=7;break;
- case 0xeb:num=8;break;
- case 0xdb:num=9;break;
- case 0xbb:num=10;break;
- case 0x7b:num=11;break;
- case 0xe7:num=12;break;
- case 0xd7:num=13;break;
- case 0xb7:num=14;break;
- case 0x77:num=15;break;
- default:break;
- }
- cflag=0;
- while((tmp&0xf0)!=0xf0){//judge the up
- tmp=P1;
- }
- }
-
- }
- }
- return num;
- }
- void main(){
- int num,i;
- while(1){
- num=keyscan();
- if(num<16){
- if(count<4)//update count number
- count++;
- for( i=count-1;i>0;i--){//update buffer
- buffer[i]=buffer[i-1];
- }
- buffer[0]=num;
- }
- for( i=0;i<count;i++){//display
- P2=discontrol[i];//the outport set 0,other set 1
- P0=table[0];
- Delay(500);
- }
- }
-
- }
复制代码
|
|