AnsweredAssumed Answered

UART1 Garbage value problem in TWR-K20D50M Board using KEIL uvision 4

Question asked by Amit Kumar on Sep 11, 2013
Latest reply on Sep 13, 2013 by Kan_Li

Hello

I have made the simplest code for UART1 for just sending a character through the UART1 (PTE0, PTE1)  and receiving it back, Using TWR-K20D50M board. when I am sending any character I am receiving garbage values corresponding to each button press. for some characters I am getting exact values like for a, A, t, T but for others I am getting some other characters. I am using KEIL uvision4 MDK. I am posting the code below. I think the problem is of Baud rate but not very sure how to rectify it. I have changed the baud rate on both P&E virtual serial toolkit as well as in my code but it didn't worked.

 

/*****************************************************************************

*      PROJECT     :    UART 1

*      BOARD        :    TWR-K20D50M (Freescale)

*      AUTHOR      :    Amit Kumar

*      DATE           :    12/09/2013

*****************************************************************************/

 

#include<MK20D5.H>        // Header file 

 

/*****************************************************************************

* Function to initialize Gate Clock

******************************************************************************/

 

void GateClock_Init(void)

{

    SIM->SOPT5 |= SIM_SOPT5_UART1RXSRC(0);         // UART Rx pin for receiving

 

    SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;  // Enabling Clock gate control to PORT E

    

    PORTE->PCR[0] |= PORT_PCR_MUX(3);          // Selecting the alternative 3 of port C i.e UART1_TX

    PORTE->PCR[1] |= PORT_PCR_MUX(3);          // Selecting the alternative 3 of port C i.e UART1_RX

 

}

 

 

/*****************************************************************************

* Function to initialize UART 1

******************************************************************************/

 

void UART1_init (int sysclk, int baud)

{

    register uint16_t ubd, brfa;

    uint8_t temp;

 

        SIM->SCGC4 |= SIM_SCGC4_UART1_MASK;    // Enabling Clock gate control to UART 1

 

   /* Make sure that the transmitter and receiver are disabled while we

     * change settings.

     */

    UART1->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);  // Disabling transmitter and receiver

 

    /* Configure the UART for 8-bit mode, no parity */

    UART1->C1 = 0;   /* We need all default settings, so entire register is cleared */

 

    /* Calculate baud settings */

    ubd = (uint16_t)((sysclk*1000)/(baud * 16));

 

   /* Save off the current value of the UARTx_BDH except for the SBR */

    temp = UART1->BDH & ~(UART_BDH_SBR(0x1F));

 

    UART1->BDH = temp | UART_BDH_SBR(((ubd & 0x1F00) >> 8));

    UART1->BDL = (uint8_t)(ubd & UART_BDL_SBR_MASK);

 

   /* Determine if a fractional divider is needed to get closer to the baud rate */

    brfa = (((sysclk*32000)/(baud * 16)) - (ubd * 32));

 

   /* Save off the current value of the UARTx_C4 register except for the BRFA */

    temp = UART1->C4 & ~(UART_C4_BRFA(0x1F));

    

    UART1->C4 = temp | UART_C4_BRFA(brfa);

 

   /* Enable receiver and transmitter */

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

}

 

 

/*****************************************************************************

* Function to Send character to UART 1

******************************************************************************/

 

void uart_putchar (char ch)

{

   /* Wait until space is available in the FIFO */

    while(!(UART1->S1 & UART_S1_TDRE_MASK));

 

    /* Send the character */

    UART1->D = (uint8_t)ch;

}

 

 

/*****************************************************************************

* Function to Check for any byte recieved from UART 1

******************************************************************************/

 

int uart_getchar_present ()

{

    return (UART1->S1) & UART_S1_RDRF_MASK;

}

 

 

/*****************************************************************************

* Function to Recieve character from UART 1

******************************************************************************/

 

char uart_getchar ()

{

    /* Wait until character has been received */

    while (!(UART1->S1 & UART_S1_RDRF_MASK));

    /* Return the 8-bit data from the receiver */

    return UART1->D;

}

 

 

 

int main(void)

{

  GateClock_Init();

  UART1_init(50000, 38400);   // System clock frequency, Baud rate

  uart_putchar('A');

    while(1)

    {

        uart_putchar(uart_getchar());    // Sending the recieved data

    }

}

Outcomes