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
  • I'd like to explain a little detail on the subject, which other readers will also benefit from.

    When you use 'polling', if you're waiting for a character from the uart and then sending a string of characters back, then you could be unlucky and miss out some characters now and then. You would be confused why.

    The reason is that while you're sending - say 16 characters - you're not checking for incoming data. The data arrives at the same speed as the data you're sending out, so since you're not reading while you're writing, you will lose up to N - FIFO_SIZE characters, where N is the number of characters you're sending and FIFO_SIZE is the size of the FIFO on the chip.

    The FIFO size may be 1, it may be 16 or 32, depending on your chip, but even if you're only sending two characters for every single character you receive, at some point, the FIFO will get full, and you'll lose half the transmission.

    Using interrupts will fix this problem, because the interrupts will transmit data you've scheduled for transmission, and it'll also be able to have a receive-buffer, which can be of a size of your choice, as long as there's enough RAM for it.

    In addition to these two benefits, your program will no longer need to stall in the part of your code that transmits data.

Reply
  • I'd like to explain a little detail on the subject, which other readers will also benefit from.

    When you use 'polling', if you're waiting for a character from the uart and then sending a string of characters back, then you could be unlucky and miss out some characters now and then. You would be confused why.

    The reason is that while you're sending - say 16 characters - you're not checking for incoming data. The data arrives at the same speed as the data you're sending out, so since you're not reading while you're writing, you will lose up to N - FIFO_SIZE characters, where N is the number of characters you're sending and FIFO_SIZE is the size of the FIFO on the chip.

    The FIFO size may be 1, it may be 16 or 32, depending on your chip, but even if you're only sending two characters for every single character you receive, at some point, the FIFO will get full, and you'll lose half the transmission.

    Using interrupts will fix this problem, because the interrupts will transmit data you've scheduled for transmission, and it'll also be able to have a receive-buffer, which can be of a size of your choice, as long as there's enough RAM for it.

    In addition to these two benefits, your program will no longer need to stall in the part of your code that transmits data.

Children