Hi, I've got a custom MKL15Z128VFT4 board and trying to use LPTMR0 as pulse counter. Here is my code:
unsigned int compare_value = 1;
serial.baud(115200);
/* Clock the timer */
SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK;
/* reset the counter */
LPTMR0->CSR = 0;
/* turn on portc */
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock
PORTC->PCR[5] = PORT_PCR_MUX(0x4); //Use ALT2 on PTC5
LPTMR0->PSR = LPTMR_PSR_PCS(0x1) | LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter
LPTMR0->CMR = LPTMR_CMR_COMPARE(compare_value); //Set compare value
/* set pulse mode */
LPTMR0->CSR = LPTMR_CSR_TPS(0x2) | LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)
/* start pulse counter */
LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;
while(1) {
if(LPTMR0->CNR != 0)
printf("Current value of pulse count register CNR is %d\r\n", LPTMR0->CNR);
}
I followed an example here http://peninsula.googlecode.com/svn/trunk/Kinetis512/kinetis-sc/src/projects/lptmr/lptmr_pulse.c
also tried to use PORT_PCR_MUX(0x3) and even PORT_PCR_MUX(0x2) with no luck - the value of LPTMR0_CNR is always 0.
Did I miss something? Will appreciate any help.
解決済! 解決策の投稿を見る。
Thanks to Kan_Li, but my problem got a different solution. According to reference manual, to read CNR register, you need to write something first where. What's why my value while printing was always zero.
Thanks to Kan_Li, but my problem got a different solution. According to reference manual, to read CNR register, you need to write something first where. What's why my value while printing was always zero.
Hi Yar,
Have you enabled the interrupt and clear TCF flag in the ISR? Because on overflow event, CNR is reset whenever TCF is set.
Hope that helps,
Have a great day,
Kan
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Kan,
Thanks for your reply, you were right, I didn't enable the interrupt and didn't clear the TFC flag. You ment TFC, not TCF, yeah? Because, according to reference manual, TFC flag is responsible for CNR reset, not TCF.
But there is still no luck and CNR is still remains zero =(
Here is the full code:
unsigned int compare_value = 1000;
serial.baud(115200);
__enable_irq();
NVIC_EnableIRQ(LPTimer_IRQn);
/* Clock the timer */
SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK;
/* reset the counter */
LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK;
/* turn on portc */
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock
PORTC->PCR[5] = PORT_PCR_MUX(0x4); //Use ALT2 on PTC5
LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter
LPTMR0->CMR = LPTMR_CMR_COMPARE(compare_value); //Set compare value
/* set pulse mode */
LPTMR0->CSR = LPTMR_CSR_TPS(0x2) | LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)
/* disable timer compare */
LPTMR0->CSR &= ~LPTMR_CSR_TFC_MASK;
/* Enable interrupt */
LPTMR0->CSR |= LPTMR_CSR_TIE_MASK;
/* start pulse counter */
LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;
while(1) {
if(LPTMR0->CNR != 0)
printf("Current value of pulse count register CNR is %d\r\n", LPTMR0->CNR);
}
Didn't I miss something?
Hi Yar,
The flag to be cleared in ISR is TCF bit, TFC bit is for selecting the case when CNR should be reset, you may fetch details from the RM like the following:
so you should clear TFC flag when initialize LPTMR, and in the ISR write one to TCF flag to let CNR continue to count instead of resetting.
Hope that helps,
Have a great day,
Kan
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------