This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Receiving Data Using the UART CORTEX M0

Hi, I wanted to write a simple program that doesn’t use interrupts or check any of the possible error flags, but that shows a simple UART application for receiving data. This is a part of the code I wrote.


int uart0Getch(void)

{

  if (LPC_UART->LSR & LSR_RDR)                 // check if character is available

  { return LPC_UART->RBR;                       // return character

  }

  return -1;

}

#include "includes.h"

#include "uart.h"

int value;

extern int main( void )

{

 

  system_init();

screen1[0]='a';

  screen1[1]='b';

  screen1[2]='c';

  while(1)

 

  //KeyScanProc(buf,screen1);

  

  if ((value = uart0Getch()) >= 0)

  {

  send_to_uart(screen1,3);

  }

  }

  }

}

Parents
  • -Your uart0Getch() function seems to be fine. The problem is most likely that your baud rate has not been set correctly.

    Your baud rate must be set to the same on both ends; eg. both the transmitter and receiver.

    So if you're sending data from a computer, and you're using 1200 baud, set the baud rate to 1200 on both the computer and the microcontroller.

    Which microcontroller (vendor + model) are you using ?

    -If you are using a LPC11xx, then you might be able to use this snippet to set the baud rate, before trying to receive:

    void setupUART()

    {

        Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO1_6, IOCON_FUNC1); /* Pin 31: RxD */

        Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO1_7, IOCON_FUNC1); /* Pin 32: TxD */

        Chip_UART_Init(LPC_USART);

        Chip_UART_SetBaud(LPC_USART, 38400);

    }

    Note: You'll need to configure the first two lines, so they match your own setup. You'll of course also need to change the baud rate so it matches your needs.

    To send a character, you can use this function:

    void UART_sendCharacter(char aCharacter)

    {

        while(!(Chip_UART_ReadLineStatus(LPC_USART) & UART_LSR_THRE)){}

        Chip_UART_SendByte(LPC_USART, (uint8_t) aCharacter);

    }

    For completeness (you already have a function that resembles it); to wait for a character, you can use this:

    int16_t UART_receiveByte()

    {

      while(!(Chip_UART_ReadLineStatus(LPC_USART) & UART_LSR_RDR)){}

      return(Chip_UART_ReadByte(LPC_USART));

    }

    The 'sendCharacter' function can be rewritten to your style, if you need to; here I've chosen to send a 'byte' instead of a character:

    void UART_sendByte(uint8_t aByte)

    {

        while(!(LPC_USART->LSR & UART_LSR_THRE)){}

        LPC_USART->THR = aByte;

    }

Reply
  • -Your uart0Getch() function seems to be fine. The problem is most likely that your baud rate has not been set correctly.

    Your baud rate must be set to the same on both ends; eg. both the transmitter and receiver.

    So if you're sending data from a computer, and you're using 1200 baud, set the baud rate to 1200 on both the computer and the microcontroller.

    Which microcontroller (vendor + model) are you using ?

    -If you are using a LPC11xx, then you might be able to use this snippet to set the baud rate, before trying to receive:

    void setupUART()

    {

        Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO1_6, IOCON_FUNC1); /* Pin 31: RxD */

        Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO1_7, IOCON_FUNC1); /* Pin 32: TxD */

        Chip_UART_Init(LPC_USART);

        Chip_UART_SetBaud(LPC_USART, 38400);

    }

    Note: You'll need to configure the first two lines, so they match your own setup. You'll of course also need to change the baud rate so it matches your needs.

    To send a character, you can use this function:

    void UART_sendCharacter(char aCharacter)

    {

        while(!(Chip_UART_ReadLineStatus(LPC_USART) & UART_LSR_THRE)){}

        Chip_UART_SendByte(LPC_USART, (uint8_t) aCharacter);

    }

    For completeness (you already have a function that resembles it); to wait for a character, you can use this:

    int16_t UART_receiveByte()

    {

      while(!(Chip_UART_ReadLineStatus(LPC_USART) & UART_LSR_RDR)){}

      return(Chip_UART_ReadByte(LPC_USART));

    }

    The 'sendCharacter' function can be rewritten to your style, if you need to; here I've chosen to send a 'byte' instead of a character:

    void UART_sendByte(uint8_t aByte)

    {

        while(!(LPC_USART->LSR & UART_LSR_THRE)){}

        LPC_USART->THR = aByte;

    }

Children