EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
51单片机中定时器的小程序9 o+ G5 {. ~8 h D0 V" m! e0 j, B* |
: N5 E1 ` h$ M+ t7 W: B e
: f z- P- B- k; f8 t今天我开始了对51中的定时器进行了学习,并写了一个小程序。 #i nclude sbit P0_1=P0^1; //定义位变量 /********************************************************* * 延时子程序 * 16个循环为1秒 * **********************************************************/ void Delay(count) { int i; while(--count != 0) { for(i = 0; i < 125; i++); // ";" 表示空语句,CPU空转。 } // i 从0加到125,在12M晶体下CPU大概耗时1毫秒 } /********************************************************* 定时器中断子程序 ************************************************************/ void time0_server_(void) interrupt 2 {static time0_server_count; time0_server_count++; if(time0_server_count==16) {P0_1=~P0_1; time0_server_count=0; } TF0=0; Delay(20); } main() { TH0=0x0B; TL0=0xDC; P0_1=0; EA=1; ET0=1; TR0=1; while(1); } 该程序主要是用来了解定时器的运行规律,并对他进行必要的应用。 通过对其进行实验,我大致的对其有了一些了解,其主要用途有定时,记数,中断。等等。 2 A! ]* \8 V' e* X+ |
2 \2 Q% r1 X% l D5 n( o/ P, c
|