UART problem with KL26Z128

cancel
Showing results for 
Search instead for 
Did you mean: 

UART problem with KL26Z128

300 Views
Contributor II

Hi,

I'm dealing with a UART problem, I can't transmit any data from UART1 (which share the same pin with OpenSDA on KL26Z128 board) to the serial port terminal. I must not use interrupt mode, only polling; and I'm testing the code forcing it to  transmit the character '5' (0x35 in ASCII), but without success. The UART_D buffer remains equal to 0x00 all the time. I thought that the problem could be the baud rate (115200 bps) , but  the UART_TX (PTC4) signal   is a normal UART signal.

The UART's code follows below:

void uart1_init (void){

     SIM_SCGC5 = SIM_SCGC5_PORTC_MASK;

     SIM_SCGC4 = SIM_SCGC4_UART1_MASK;

     SIM_SOPT5 |= SIM_SOPT5_UART1TXSRC(0);

  

     PORTC_PCR3 = PORT_PCR_MUX(0x3);

     PORTC_PCR4 = PORT_PCR_MUX(0x3);

  

     UART1_BDH = 0x00;

     UART1_BDL = 0x1A; /* SBR = 26 */

 

     UART1_C1 = 0x00;

     UART1_C3 = 0x00;

     UART1_C4 = 0x00;

     UART1_S2 = 0x00;

     UART1_C2 |= UART_C2_TE_MASK;

}

void uart1_send (int data){

     while(!(UART1_S1&UART_S1_TDRE_MASK) && !(UART1_S1&UART_S1_TC_MASK));

     UART1_D  = data;

}

int main (void){

     uart1_init();

while(1){

     uart1_send( 0x35 );

}

}

Is there any error on code?

Could someone give me a clue?

Thanks in advance,

Rafael

0 Kudos
1 Reply

34 Views
Senior Contributor II

Helo Rafael,

You need to enable the transmitter:

UART1_C2 |= UART0_C2_TE_MASK;

You can take a look into this thread​ to see how the baudrate is calculated, especially the information that I have copied below.

In the reference manual you can see that the baud rate is generated as follows:

pastedImage_0.png

So the SBR value can be calculated:

SBR[12:0] = UART Module Clock / (16 * Baud rate)

The UART Module Clock is the bus clock which does not seem to be configured. By default the Bus Clock is 20.97152 MHz, so:

SBR = 20.97152 MHz / (16 * 115200) = 11.3777

I suggest to change the bus clock to 48 MHz, it will produce a more accurate baud rate:

SBR = 48 MHz / (16 * 115200) = 26.0416

Best regards,

Earl.

0 Kudos