C Program to Convert String to ASCII and Send Them Via UART in KL25Z

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C Program to Convert String to ASCII and Send Them Via UART in KL25Z

527 Views
ElectroDesi9n
Contributor II

Hi there,

I'm trying to design a communication system. My KL25Z is supposed to receive some data in UART, process them and transmit them again. Since I was facing some problems, I decided to isolate the project and found that the problem is from KL25Z side.  The characters that are shown on the serial terminal is garbage and unprintable; although the baud rates are set and both sides have 8N1 UART. I'll share the corresponding code parts below (I've attached the full codes as well if it helps):

// Transmit one byte via UART2
void UART2_Transmit(uint8_t data) {
      while (!(UART2->S1 & UART_S1_TDRE_MASK)) {} // Wait until transmit buffer is empty
      UART2->D = data;
      Blink_Blue();
}

// Function to write data to UART2
void UART_Write(const char* data) {
      while (*data) {
      UART2_Transmit((uint8_t) *data); // Transmit data via UART2
      data++;
      }
}

 

int main(void) {
    UART2_Init();
    Delay(20000);
    Blink_Red();
    Delay(20000);
    UART_Write("K");
    Delay(20000);
    UART_Write("L");
    Delay(20000);
    UART_Write("\n");

}

 

FRDM-KL25Z

Tags (4)
0 Kudos
Reply
1 Reply

466 Views
mccoder
Contributor II

I'm not super familiar with the KL25 family (but am with several other Kinetis devices). Here's a couple of things to check.

Remove this line:

UART2->C4 = (UART2->C4 & ~UARTLP_C4_OSR_MASK) | UARTLP_C4_OSR(15);

That is for the low power UART's and not UART's 1-2.

I also 'or' the masks just so there is no confusion of the order of operations:

UART2->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);



A scope would go a long ways in figuring out what is wrong  If you're seeing garbage in a terminal then it's likely a clock issue.  The bus clock is usually (not necessarily) half the CPU clock for slower devices.  If your CPU clock is running at 48 MHz you can look at the SIM->CLKDIV[OUTDIV4] register.  That sets the bus clock.  it should be 001 (for the OUTDIV4 section).

Also make sure both devices are using the same voltage. 

Hope this helps!

0 Kudos
Reply