飞思卡尔KL17之LPTMR0的若干错误

    xiaoxiao2021-03-25  133

    想要用LPTMR0做一个ms级别的系统基准时钟,想法是:

    LPTMR0时钟源为1000HZ,LPTMR0->CMR为1000,这样子每隔1S中断一次,在每次中断中SystemTickSecond++; 获取系统时钟的方法就是SystemTickSecond + LPTMR0->CNR

    问题1:访问LPTMR0->CNR时硬件错误,必现。

    原因:LPTMR0未初始化,未使能。在访问该寄存器前必须初始化。

    问题2:LPTMR0中断 一次之后就不再计数,而且无法读取到ms的数值

    原因:读取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; }
    转载请注明原文地址: https://ju.6miu.com/read-9322.html

    最新回复(0)