先上代码
//8bit count up , max 0xFF
void Ny8b072a_Timer0_Init(void)
{PCON1 = C_TMR0_Dis; // Disable Timer0//1 * (255 - 5) = 250usTMR0 = 5; // Load 0x00 to TMR0 (Initial Timer0 register)//16M 2T Div8 = 1usT0MD = C_PS0_TMR0 | C_PS0_Div8 ; // Prescaler0 is assigned to Timer0, Prescaler0 dividing rate = 1:2,clock source is instruction clockINTE |= C_INT_TMR0; // Enable Timer0 overflow interruptPCON1 = C_TMR0_En; // Enable Timer0
}//! interrupt service routine
void isr(void) __interrupt(0)
{ INTF = (unsigned char)~(C_INT_TMR0); // Clear T0IF flag bit
}
系统配置的是16M 2T,8分频,预装载系数为5,则定时器0会从5开始向上计数,直到超过0xFF发生中断,那么每次进入中断的时间为:
16 000 000 / 2 / 8 = 1 000 000HZ = 1us
1us *(255 - 5) = 250us
大家可以根据自己需求改变配置、分频和分频系数。但是不要把进中断的时间设置的过低(低于10us),因为这是8位机资源有限,过低容易出现意想不到的BUG