Hello,
I'm facing some problems using the KL05 FRDM board and i'm trying to toggle a pin using the LPTMR.
The LPTMR is clocked by the MCRIRCLK (32KHz) and i'm using a prescaler of 2 so the clock would be 16Khz. The result that i'm getting is 2msecs instead of 60uSecs.
Please find the code below:
#include "derivative.h" /* include peripheral declarations */
int main(void)
{
SIM_SOPT1 |= (0<<18); //32K Oscillator clock
SIM_SOPT1 |= (0<<19);
SIM_SOPT1 = 0x00000000;
SIM_SCGC5 |= (SIM_SCGC5_LPTMR_MASK | SIM_SCGC5_PORTB_MASK);// LPTMR gated PORTB
PORTB_PCR8 |= 0x00000100; // alternative pin mux selection //
GPIOB_PDDR |= 0x00000100; // pin direction //
asm(" CPSIE i");
NVIC_ICPR = 1 << (28);
NVIC_ISER = 1 << (28);
LPTMR0_CSR=0;
LPTMR0_CSR |= 0x00000040; // HEX timer interrupt Enable
/* Set the compare value to the number of ms to delay */
LPTMR0_PSR = LPTMR_PSR_PCS(1)|LPTMR_PSR_PBYP_MASK;
LPTMR0_CMR = 10; //Check what is the lowest we can get ?
/* Start the timer */
LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;
}
void LPTimer_IRQHandler(void){
LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK;
LPTMR0_CSR |= LPTMR_CSR_TCF_MASK;
GPIOB_PTOR |= (1<<8);
LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;
}
If anyone knows why this is happening please let me know.
Many Thanks
Filipe Branco
I think you are setting by error the LPO 1kHz instead of the System oscillator, I suggest you to comment or delete the lines:
SIM_SOPT1 |= (0<<18); //32K Oscillator clock
SIM_SOPT1 |= (0<<19);
Here you are setting to use the LPO (OSC32KSEL= 11 = LPO 1kHz) this perfectly explain your 2msec period
Have a nice day :P,
Perla
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi,
isint he already clearing the settings set by
SIM_SOPT1 |= (0<<18);
SIM_SOPT1 |= (0<<19);
by the third line
SIM_SOPT1 = 0x00000000; and selecting System oscillator (OSC32KCLK)??
Also, these lines
SIM_SOPT1 |= (0<<18);
SIM_SOPT1 |= (0<<19);
have no effect as 0 is being shifted so many times!!!
Am also struck with a similar problem. Any suggestions would be helpful..
Thanks in advance
Hi
SIM_SOPT1 is relevant for the RTC and not the LPTMR.
The LPTMR clock source is defined in LPTMR0_PSR.
If using the external oscillator as clock input, the external oscillator input connection must also be enabled in OSC0_CR (irrespective of whether the oscillator is running or not).
Therefore:
OSC0_CR |= (OSC_CR_ERCLKEN | OSC_CR_EREFSTEN); | // enable the external reference clock and keep it enabled in stop mode | |
LPTMR0_PSR = (LPTMR_PSR_PCS_OSC0ERCLK | LPTMR_PSR_PBYP); |
This will clock the LPTRM from the oscillator input.
Regards
Mark
Thanks Mark.. That explains a lot.