51計數(shù)器進行計數(shù)實例
#include
bit flg= 0;//標志位
unsigned int counter = 0;//計數(shù)值
sbit LED = P1^1;
//T0初始化
void InitT0(void)
{
TMOD = 0x05;//T0計數(shù)工作方式1
TH0 = 0xff;
TL0 = 0xff;
EA= 1;
ET0 = 1;
TR0 = 1;
}
//T0中斷服務(wù)子函數(shù)
void Time0_Int(void) interrupt 1
{
TH0 = 0xff;
TL0 = 0xff;
flg = 1;
}
void delay(volatile unsigned long cnt)
{
while(cnt--);
}
void main(void)
{
InitT0();
while(1){
P1_1 = 1;
while(!flg);
flg = 0;
counter = 1;//計算計數(shù)值
while(counter >0)
{
counter--;
LED = 0;
delay(10000);
LED = 1;
}
}
}