想要用LPTMR0做一个ms级别的系统基准时钟,想法是:
LPTMR0时钟源为1000HZ,LPTMR0->CMR为1000,这样子每隔1S中断一次,在每次中断中SystemTickSecond++; 获取系统时钟的方法就是SystemTickSecond + LPTMR0->CNR
原因:LPTMR0未初始化,未使能。在访问该寄存器前必须初始化。
原因:读取LPTMR0->CNR寄存器必须先有个写入。(The CNR returns the value of the LPTMR counter at the time this register was last written. Writing the CNR will latch the current value of the LPTMR for subsequent reading, the value written is ignored.) 最终读取系统基准时钟的函数为:
unsigned long SystemTimeMs(void){ unsigned long t; unsigned int ms; NVIC_DisableIRQ(LPTMR0_IRQn); LPTMR0->CNR = 0; //MUST WRITE ANY DATA ms = LPTMR0->CNR & 0x0000FFFF; t = SystemTickSecond * 1000; t += ms % 1000; NVIC_EnableIRQ(LPTMR0_IRQn); return t; }