I am trying to create time counting program in C for MCU kinetis KL05. The app should be counting time using RTC. The output of the program is shown on four 7 segment LED displays. I was able to light the displays and i made counter that counts in loop for testing purpose. What I am unable to do is configuring RTC. The problem is that i dont know why the RTC->TSR is not incrementing... I tried to insert different values into the TSR register, but the value never changes after i the counter is enabled.
Here is my code for the RTC configuration:
void initRtc(){
//Enable the internal reference clock. MCGIRCLK is active.
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
//Select the slow internal reference clock source.
MCG_C2 &= ~(MCG_C2_IRCS_MASK);
//Set PTA5 as RTC_CLKIN
PORTA_PCR5 |= (PORT_PCR_MUX(0x1));
// select 32 KHz clock source for the RTC module.
SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0b10);
//Set PTA15 as CLKOUT pin
SIM_SOPT2 |= SIM_SOPT2_CLKOUTSEL(0b100);
//selects the MCGIRCLK clock to output on the CLKOUT pin.
PORTA_PCR15 |= (PORT_PCR_MUX(0x5));
//Enable software access and interrupts to the RTC module.
SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
//Clear all RTC registers.
RTC_CR = RTC_CR_SWR_MASK;
RTC_CR &= ~RTC_CR_SWR_MASK;
RTC_CR |= RTC_CR_OSCE_MASK;
delay(100);
if (RTC_SR & RTC_SR_TIF_MASK){
RTC_TSR = 0x00000000;
}
RTC_TSR = 0xff;
// interrupt enable
RTC_IER |= RTC_IER_TSIE_MASK;
// counter enable
RTC_SR |= RTC_SR_TCE_MASK;
NVIC_ClearPendingIRQ(21);
NVIC_EnableIRQ(21);
}
void ports_init (void)
{
SIM->COPC = SIM_COPC_COPT(0x00); // Just disable the usage of WatchDog feature
SIM->SCGC5 = (SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK); // Turn on clocks for PORTA and PORTB
initRtc();
/* Set corresponding PORTA pins for GPIO functionality */
PORTA->PCR[8] = ( 0|PORT_PCR_MUX(0x01) ); // display DS4
PORTA->PCR[9] = ( 0|PORT_PCR_MUX(0x01) ); // display DS3
PORTA->PCR[10] = ( 0|PORT_PCR_MUX(0x01) ); // display DS2
PORTA->PCR[11] = ( 0|PORT_PCR_MUX(0x01) ); // display DS1
/* Set corresponding PORTA port pins as outputs */
PTA->PDDR = GPIO_PDDR_PDD( 0x0F00 ); // "1" configures given pin as an output
NVIC_DisableIRQ(31); // Disable the eventual generation of the interrupt caused by the control button
/* Set corresponding PORTB pins for GPIO functionality */
PORTB->PCR[0] = ( 0|PORT_PCR_MUX(0x01) ); // seg A
PORTB->PCR[1] = ( 0|PORT_PCR_MUX(0x01) ); // seg B
PORTB->PCR[2] = ( 0|PORT_PCR_MUX(0x01) ); // seg C
PORTB->PCR[3] = ( 0|PORT_PCR_MUX(0x01) ); // seg DP
PORTB->PCR[8] = ( 0|PORT_PCR_MUX(0x01) ); // seg D
PORTB->PCR[9] = ( 0|PORT_PCR_MUX(0x01) ); // seg E
PORTB->PCR[10] = ( 0|PORT_PCR_MUX(0x01) ); // seg F
PORTB->PCR[11] = ( 0|PORT_PCR_MUX(0x01) ); // seg G
/* Set corresponding PORTB port pins as outputs */
PTB->PDDR = GPIO_PDDR_PDD( 0x0F0F ); // "1" configures given pin as an outnput
PORTB->PCR[4] = ( 0 | PORT_PCR_ISF(1) | PORT_PCR_IRQC(0x0A) | PORT_PCR_MUX(0x01) |
PORT_PCR_PE(1) | PORT_PCR_PS(1)); // display SW1
/* Let's clear any previously pending interrupt on PORTB and allow its subsequent generation */
NVIC_ClearPendingIRQ(31);
NVIC_EnableIRQ(31);
}
I am not sure if there is anybody who can help or if this page is the right place to ask such a question, but i really dont know what to do... (btw i am coding it in KDS IDE 3.0) Any help would be appreaciated. :)
User contributions licensed under CC BY-SA 3.0