采用12MHZ時鐘頻率的單片機,通過一個開關(guān)輸入,觸發(fā)定時計數(shù)。按下開關(guān)10次,計算按鍵的平均時間,并通過數(shù)碼管顯示出來。
程序代碼:
#include<reg51.h>
sbit P1_0=P1^0;
unsigned char numcode[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88};
unsigned char sepcoed[4]={0x01,0x02,0x04,0x08};
unsigned int num=0;
void int1() interrupt 1
{
num++;
TL0=0xb0;
TH0=0x3c;
}
void DelayMS(unsigned int ms)
{
unsigned char i;
while(ms--) for(i=0;i<120;i++);
}