1 //晶振的频率 2 #define OSC_FREQ (11059200UL) 3 4 //每个指令的振荡周期数(4,6,12),注意,这里一定要仔细查看芯片手册 5 //*12 - 早期的8051、8052及当前的绝大部分51单片机 6 //*6 - Philips 的芯片 7 //*4 - Dallas 8 #define OSC_PER_INST (12) 9 10 //TIME 为定时时间 单位MS11 #define PRELOAD1MS (65536 - 1*(UINT16)(OSC_FREQ/(OSC_PER_INST*1000)))12 #define PRELOAD5MS (65536 - 5*(UINT16)(OSC_FREQ/(OSC_PER_INST*1000)))13 14 void start_Timeout1ms(){15 TH0 = PRELOAD1MS / 256;16 TL0 = PRELOAD1MS % 256;17 TF0 = 0 ;//清溢出标志位18 19 ET0 = 0; //关定时器0的中断20 21 TR0 = 1; //启动定时器22 23 }24 25 void start_Timeout5ms(){26 //TMOD = ;27 28 TH0 = PRELOAD5MS / 256;29 TL0 = PRELOAD5MS % 256;30 TF0 = 0 ;//清溢出标志位31 32 ET0 = 0; //关定时器0的中断33 34 TR0 = 1; //启动定时器35 36 }37 38 void stop_Timeout(){39 TR0 = 0;40 }41 42 void Delay5ms(){43 start_Timeout5ms();44 while(!_testbit_(TF0));45 stop_Timeout();46 }47 48 /* 用法 49 start_Timeout1ms();50 while(!TF0); //等待T0超时51 stop_Timeout();52 */
以上为转载,没经过验证