#ifdef FOSC_160000 // if Fsys = 16MHz
#define TIMER_DIV12_VALUE_10us 65536-13 //13*12/16000000 = 10 uS, // Timer divider = 12 for TM0/TM1
#define TIMER_DIV12_VALUE_100us 65536-130 //130*12/16000000 = 10 uS, // Timer divider = 12
#define TIMER_DIV12_VALUE_1ms 65536-1334 //1334*12/16000000 = 1 mS, // Timer divider = 12
16M时钟,12分频后定时器的周期为16/12 us,所以1ms=1000*16/12 约为1334 每个单位为1ms
void Timer0_Delay1ms(UINT32 u32CNT)
{
clr_T0M; //T0M=0, Timer0 Clock = Fsys/12 定时器0 12分频
TMOD |= 0x01; //Timer0 is 16-bit mode 16位定器/计数器
set_TR0; //Start Timer0 开启
while (u32CNT != 0)
{
TL0 = LOBYTE(TIMER_DIV12_VALUE_1ms); //Find define in "Function_define.h" "TIMER VALUE"
TH0 = HIBYTE(TIMER_DIV12_VALUE_1ms); //高8位和低8位设置相应值
while (TF0 != 1); //Check Timer0 Time-Out Flag 等待溢出
clr_TF0; //清除溢邮
u32CNT --;
}
clr_TR0; //Stop Timer0 定时器0停止
}
|