| 程序功能:按鍵計(jì)數(shù)。每個(gè)鍵對(duì)應(yīng)數(shù)碼管一位。按鍵1位數(shù)加1,鍵2十位數(shù)加1,鍵3百位數(shù)加1,鍵4千位數(shù)加1。超過(guò)量程,清零。 程序源碼: #include<reg52.h> #define uchar unsigned char sbit s1 = P2^3; sbit s2 = P2^0; sbit s3 = P2^1; sbit s4 = P2^2; sbit en = P2^5; sbit Key1= P3^2; sbit Key2= P3^3; sbit Key3= P3^4; sbit Key4= P3^5; //各位計(jì)數(shù)變量 uchar ct1,ct2,ct3,ct4; //數(shù)碼管顯示 uchar dis[10]={ 0x84, // 0 0xBD, // 1 0xE0, // 2 0xB0, // 3 0x99, // 4 0x92, // 5 0x82, // 6 0xBC, // 7 0x80, // 8 0x90 // 9 }; void Delay(int m) { while(--m); } void display() { s1=0; P0=dis[ct1]; s1=1; Delay(100); s2=0; P0=dis[ct2]; s2=1; Delay(100); s3=0; P0=dis[ct3]; s3=1; Delay(100); s4=0; P0=dis[ct4]; s4=1; Delay(100); } void main() { s1=1; s2=1; s3=1; s4=1; Key1=1; Key2=1; Key3=1; Key4=1; en = 0; ct1=0; ct2=0; ct3=0; ct4=0; while(1) //循環(huán)執(zhí)行 { if(!Key1)//如果鍵1按下,下同 { while(!Key1); ++ct1; } if(!Key2) { while(!Key2); ++ct2; } if(!Key3) { while(!Key3); ++ct3; } if(!Key4) { while(!Key4); ++ct4; } if(ct1>9)//個(gè)位進(jìn)位,下同 { ct1=0; ++ct2; } if(ct2>9) { ct2=0; ++ct3; } if(ct3>9) { ct3=0; ++ct4; } if(ct4>9) { ct1=0; ct2=0; ct3=0; ct4=0; } display(); //只須調(diào)用顯示函數(shù) } }
|