Hi all,
I was trying to program on KL02 microcontroller in keil-mdk using baremetal examples. I am finding trouble in using UART0 interrupt routines. I have written the following code for the initialization of UART interrupt as my UART buffer receives a character. Where am i going wrong?
void uart0_init (UART0_MemMapPtr uartch, int uart0clk, int baud)
{
register uint16 sbr;
uint8 temp;
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
UART0_C2_REG(uartch) &= ~(UART0_C2_TE_MASK
| UART0_C2_RE_MASK );
UART0_C2_REG(uartch) |= 0x20; // enabled Receiver Interrupt Enable for RDRF
/* Configure the uart for 8-bit mode, no parity */
UART0_C1_REG(uartch) = 0; /* We need all default settings, so entire register is cleared */
/* Calculate baud settings */
temp = UART0_C4;
temp = (temp & UART0_C4_OSR_MASK) + 1;
sbr = (uint16)((uart0clk)/(baud * (temp)));
/* Save off the current value of the uartx_BDH except for the SBR field */
temp = UART0_BDH_REG(uartch) & ~(UART0_BDH_SBR(0x1F));
UART0_BDH_REG(uartch) = temp | UART0_BDH_SBR(((sbr & 0x1F00) >> 8));
UART0_BDL_REG(uartch) = (uint8)(sbr & UART0_BDL_SBR_MASK);
/* Enable receiver and transmitter */
UART0_C2_REG(uartch) |= (UART0_C2_TE_MASK
| UART0_C2_RE_MASK );
}
Please Help...
已解决! 转到解答。
Hi Mridul Pandey,
Your initial code didn't enable the receive interrupt enable bit UART0_C2[RIE]. If you want get the receive interrupt, you should configure these points:
1. enable UART0_C2[RIE] bit :
UART0_C2 |= UART0_C2_RIE_MASK;
2 . you should configure the NVIC for interrupt mode of UART0 operation:
enable_irq(12);
3. Enable the global interrupt:
EnableInterrupts;
4. Add interrupt service function:
void UART0_IRQHandler(void)
{
if (UART0_S1&UART_S1_RDRF_MASK) // receive data
{
c = UART0_D;
}
}
Wish it helps you!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Mridul Pandey,
Your initial code didn't enable the receive interrupt enable bit UART0_C2[RIE]. If you want get the receive interrupt, you should configure these points:
1. enable UART0_C2[RIE] bit :
UART0_C2 |= UART0_C2_RIE_MASK;
2 . you should configure the NVIC for interrupt mode of UART0 operation:
enable_irq(12);
3. Enable the global interrupt:
EnableInterrupts;
4. Add interrupt service function:
void UART0_IRQHandler(void)
{
if (UART0_S1&UART_S1_RDRF_MASK) // receive data
{
c = UART0_D;
}
}
Wish it helps you!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------