#include <reg51.h> #define uchar unsigned char #define uint unsigned int sbit led1 = P2^0;//定義管腳; sbit led2 = P2^1; sbit led3 = P2^2; sbit led4 = P2^3; sbit led5 = P2^4; sbit led6 = P2^5; sbit key = P1^0; uchar tcount=0,count=0;//定義時間變量和按鍵計數變量; void Delay(uchar i)//機械延時; { while(--i); } void LedLight(void)//LED控制函數; { switch(count) { case 3 : led1 = 0;break;//3下時,LED1亮; case 5 : led2 = 0;break;//5下時,LED2亮; case 7 : led3 = 0;break;//7下時,LED3亮; case 9 : led4 = 0;break;//9下時,LED4亮; case 11: led5 = 0;break;//11下時,LED5亮; case 13: led6 = 0;break;//13下時,LED6亮; default: P2 = 0xff;break;//其余的都不亮; } } void InitTimer0(void)//定時器0初始化; { TMOD = 0x00; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; TR0 = 1; } void main(void)//主函數 { InitTimer0(); while(1)//無限循環; { if(key==0)//檢查按鍵是否按下; { Delay(10);//機械延時; if(key==0){count++;}//按鍵計數; while(key==0); } if(TF0==1)//復位定時器0; { TF0=0; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; tcount++; if(tcount==20)//1秒的時間; { LedLight(); Delay(100); count=0;//復位按鍵計數變量; } } } } |