程序没问题,是软件问题,完整程序如下: /*P3控制4*4键盘,P0口控制数码管*/ #include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit duan=P2^6; sbit wei=P2^7; sbit BEEP=P2^2; uchar code DSY_CODE[]= {0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; uchar prekeyno=16,keyno=16;
void delay(uint z) { uchar t; while(z--)for(t=0;t<120;t++); } void key_scan() { uchar temp; P3=0x0f; delay(1); //取列值 temp=P3^0x0f; switch(temp) { case 1:keyno=0;break; case 2:keyno=1;break; case 4:keyno=2;break; case 8:keyno=3;break; default: keyno=16; } P3=0xf0; delay(1); //取行值 temp=P3>>4^0x0f; switch(temp) { case 1:keyno+=0;break; case 2:keyno+=4;break; case 4:keyno+=8;break; case 8:keyno+=12; } } void Beep() { uchar i; for(i=0;i<100;i++) { delay(1); BEEP=~BEEP; } BEEP=1; } void main() { P0=0x00; wei=1; wei=0; while(1) { P3=0x0f; if(P3!=0x0f)key_scan(); if(prekeyno!=keyno) { P0=DSY_CODE[keyno]; duan=1; duan=0; Beep(); prekeyno=keyno; } delay(50); } }
[此贴子已经被作者于2012-4-30 11:44:52编辑过]
|